Skip to content

Commit

Permalink
Merge pull request #935 from grafana/refactor/test-browser
Browse files Browse the repository at this point in the history
Refactor integration test browser
  • Loading branch information
inancgumus authored Jun 16, 2023
2 parents 4fdce45 + 48d9d7f commit 45b3e8b
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 367 deletions.
4 changes: 2 additions & 2 deletions common/network_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func newTestNetworkManager(t *testing.T, k6opts k6lib.Options) (*NetworkManager,
})

vu := k6test.NewVU(t)
vu.MoveToVUContext()
vu.ActivateVU()
st := vu.State()
st.Options = k6opts
logger := log.New(st.Logger, "")
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestNetworkManagerEmitRequestResponseMetricsTimingSkew(t *testing.T) {
vu = k6test.NewVU(t)
nm = &NetworkManager{ctx: vu.Context(), vu: vu, customMetrics: k6m}
)
vu.MoveToVUContext()
vu.ActivateVU()

req, err := NewRequest(vu.Context(), NewRequestParams{
event: &network.EventRequestWillBeSent{
Expand Down
13 changes: 7 additions & 6 deletions k6ext/k6test/vu.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ type VU struct {
// ToGojaValue is a convenience method for converting any value to a goja value.
func (v *VU) ToGojaValue(i any) goja.Value { return v.Runtime().ToValue(i) }

// MoveToVUContext moves the VU to VU context, adding a predefined k6 lib State and nilling the InitEnv
// to simulate how that is done in the real k6.
func (v *VU) MoveToVUContext() {
// ActivateVU mimicks activation of the VU as in k6.
// It transitions the VU from the init stage to the execution stage by
// setting the VU's state to the state that was passed to NewVU.
func (v *VU) ActivateVU() {
v.VU.StateField = v.toBeState
v.VU.InitEnvField = nil
}
Expand All @@ -53,9 +54,9 @@ func (v *VU) AssertSamples(assertSample func(s k6metrics.Sample)) int {
return n
}

// WithSamplesListener is used to indicate we want to use a bidirectional channel
// WithSamples is used to indicate we want to use a bidirectional channel
// so that the test can read the metrics being emitted to the channel.
type WithSamplesListener chan k6metrics.SampleContainer
type WithSamples chan k6metrics.SampleContainer

// NewVU returns a mock k6 VU.
//
Expand All @@ -71,7 +72,7 @@ func NewVU(tb testing.TB, opts ...any) *VU {
)
for _, opt := range opts {
switch opt := opt.(type) {
case WithSamplesListener:
case WithSamples:
samples = opt
case env.LookupFunc:
lookupFunc = opt
Expand Down
2 changes: 1 addition & 1 deletion tests/browser_context_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestBrowserContextOptionsExtraHTTPHeaders(t *testing.T) {
require.NoError(t, err)

err = tb.awaitWithTimeout(time.Second*5, func() error {
resp, err := p.Goto(tb.URL("/get"), nil)
resp, err := p.Goto(tb.url("/get"), nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBrowserContextAddCookies(t *testing.T) {
url: "%v"
}
];
`, testCookieName, testCookieValue, tb.URL(""))
`, testCookieName, testCookieValue, tb.url(""))
require.NoError(t, err)

bc.AddCookies(cookies)
Expand Down
16 changes: 10 additions & 6 deletions tests/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func TestTmpDirCleanup(t *testing.T) {

const tmpDirPath = "./"

b := newTestBrowser(t, withSkipClose(), env.ConstLookup("TMPDIR", tmpDirPath))
b := newTestBrowser(
t,
withSkipClose(),
withEnvLookup(env.ConstLookup("TMPDIR", tmpDirPath)),
)
p := b.NewPage(nil)
err := p.Close(nil)
require.NoError(t, err)
Expand All @@ -98,7 +102,7 @@ func TestBrowserOn(t *testing.T) {
t.Parallel()

b := newTestBrowser(t)
require.NoError(t, b.vu.Runtime().Set("b", b.Browser))
require.NoError(t, b.runtime().Set("b", b.Browser))

_, err := b.runJavaScript(script, "wrongevent")
require.Error(t, err)
Expand All @@ -110,7 +114,7 @@ func TestBrowserOn(t *testing.T) {

var (
b = newTestBrowser(t, withSkipClose())
rt = b.vu.Runtime()
rt = b.runtime()
log []string
)

Expand All @@ -128,14 +132,14 @@ func TestBrowserOn(t *testing.T) {

var (
b = newTestBrowser(t)
rt = b.vu.Runtime()
rt = b.runtime()
log []string
)

require.NoError(t, rt.Set("b", b.Browser))
require.NoError(t, rt.Set("log", func(s string) { log = append(log, s) }))

time.AfterFunc(100*time.Millisecond, b.Cancel)
time.AfterFunc(100*time.Millisecond, b.cancelContext)
_, err := b.runJavaScript(script, "disconnected")
assert.ErrorContains(t, err, "browser.on promise rejected: context canceled")
})
Expand Down Expand Up @@ -172,7 +176,7 @@ func TestBrowserCrashErr(t *testing.T) {
jsMod, ok := mod.Exports().Default.(*browser.JSModule)
require.Truef(t, ok, "unexpected default mod export type %T", mod.Exports().Default)

vu.MoveToVUContext()
vu.ActivateVU()

rt := vu.Runtime()
require.NoError(t, rt.Set("browser", jsMod.Browser))
Expand Down
6 changes: 3 additions & 3 deletions tests/browser_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestBrowserTypeConnect(t *testing.T) {
tb := newTestBrowser(t)
vu := k6test.NewVU(t)
bt := chromium.NewBrowserType(vu)
vu.MoveToVUContext()
vu.ActivateVU()

b, err := bt.Connect(context.Background(), tb.wsURL)
require.NoError(t, err)
Expand All @@ -37,14 +37,14 @@ func TestBrowserTypeLaunchToConnect(t *testing.T) {
// We have to call launch method through JS API in Goja
// to take mapping layer into account, instead of calling
// BrowserType.Launch method directly
rt := vu.Runtime()
root := browser.New()
mod := root.NewModuleInstance(vu)
jsMod, ok := mod.Exports().Default.(*browser.JSModule)
require.Truef(t, ok, "unexpected default mod export type %T", mod.Exports().Default)

vu.MoveToVUContext()
vu.ActivateVU()

rt := vu.Runtime()
require.NoError(t, rt.Set("browser", jsMod.Browser))
_, err := rt.RunString(`
const p = browser.newPage();
Expand Down
3 changes: 1 addition & 2 deletions tests/element_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/common"

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestElementHandleBoundingBoxSVG(t *testing.T) {
}`
var r api.Rect
webBbox := p.Evaluate(tb.toGojaValue(pageFn), tb.toGojaValue(element))
wb, _ := webBbox.(goja.Value)
wb := tb.asGojaValue(webBbox)
err = tb.runtime().ExportTo(wb, &r)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion tests/frame_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestWaitForFrameNavigation(t *testing.T) {
WaitUntil: common.LifecycleEventNetworkIdle,
Timeout: common.DefaultTimeout,
})
_, err := p.Goto(tb.URL("/first"), opts)
_, err := p.Goto(tb.url("/first"), opts)
require.NoError(t, err)

waitForNav := func() error {
Expand Down
6 changes: 5 additions & 1 deletion tests/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func TestFrameNoPanicWithEmbeddedIFrame(t *testing.T) {
}

// run the browser in headfull mode.
tb := newTestBrowser(t, withFileServer(), env.ConstLookup(env.BrowserHeadless, "0"))
tb := newTestBrowser(
t,
withFileServer(),
withEnvLookup(env.ConstLookup(env.BrowserHeadless, "0")),
)

p := tb.NewPage(nil)
_, err := p.Goto(
Expand Down
Loading

0 comments on commit 45b3e8b

Please sign in to comment.