Skip to content

Commit

Permalink
Remove await method from testBrowser (#723)
Browse files Browse the repository at this point in the history
Now that promises have been moved to a level above the business
logic, we can remove the need to run things in promises and assume
(in most cases) that things will run synchronously. Where there's
still a need to run things in parallel we can use the run method on
testBrowser.
  • Loading branch information
ankur22 authored Jan 23, 2023
1 parent 68ddfb4 commit e433cb7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 55 deletions.
5 changes: 1 addition & 4 deletions tests/launch_options_slowmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ func TestLaunchOptionsSlowMo(t *testing.T) {
t.Parallel()
tb := newTestBrowser(t, withFileServer())
testPageSlowMoImpl(t, tb, func(_ *testBrowser, p api.Page) {
err := tb.await(func() error {
p.Goto("about:blank", nil)
return nil
})
_, err := p.Goto("about:blank", nil)
require.NoError(t, err)
})
})
Expand Down
79 changes: 36 additions & 43 deletions tests/lifecycle_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
)

// TODO
Expand Down Expand Up @@ -71,7 +70,7 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
waitUntil common.LifecycleEvent
pingRequestTextAssert func(result string, pingCount int)
pingJSTextAssert func(result string)
assertFunc func(tb *testBrowser, p api.Page) testPromise
assertFunc func(tb *testBrowser, p api.Page) error
wantError string
}{
{
Expand Down Expand Up @@ -126,21 +125,19 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
pingSlowness: 0,
pingJSSlow: false,
waitUntil: common.LifecycleEventNetworkIdle,
assertFunc: func(tb *testBrowser, p api.Page) testPromise {
assertFunc: func(tb *testBrowser, p api.Page) error {
result := p.TextContent("#pingRequestText", nil)
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)

opts := tb.toGojaValue(&common.FrameWaitForNavigationOptions{
Timeout: 1000,
WaitUntil: common.LifecycleEventNetworkIdle,
})
waitForNav := k6ext.Promise(tb.vu.Context(), func() (result any, reason error) {
return p.WaitForNavigation(opts)
})
_, err := p.WaitForNavigation(opts)

return tb.promise(waitForNav)
return err
},
wantError: "Uncaught (in promise) waiting for navigation: timed out after 1s",
wantError: "waiting for navigation: timed out after 1s",
},
}

Expand All @@ -157,13 +154,13 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

if tt.assertFunc != nil {
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
assertHome(t, tb, p, tt.waitUntil, func() error {
return tt.assertFunc(tb, p)
}, nil, tt.wantError)
return
}

assertHome(t, tb, p, tt.waitUntil, func() testPromise {
assertHome(t, tb, p, tt.waitUntil, func() error {
result := p.TextContent("#pingRequestText", nil)
tt.pingRequestTextAssert(result, 10)

Expand All @@ -181,13 +178,11 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
click := func() error {
return p.Click(`a`, nil)
}
runAll := k6ext.Promise(tb.ctx, func() (result any, reason error) {
ctx, cancel := context.WithTimeout(tb.ctx, 5*time.Second)
defer cancel()
return nil, tb.run(ctx, waitForNav, click)
})

return tb.promise(runAll)
ctx, cancel := context.WithTimeout(tb.ctx, 5*time.Second)
defer cancel()

return tb.run(ctx, waitForNav, click)
}, func() {
result := p.TextContent("#pingRequestText", nil)
tt.pingRequestTextAssert(result, 20)
Expand Down Expand Up @@ -297,14 +292,14 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

if tt.assertFunc != nil {
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
assertHome(t, tb, p, tt.waitUntil, func() error {
tt.assertFunc(p)
return testPromise{}
return nil
}, nil, "")
return
}

assertHome(t, tb, p, tt.waitUntil, func() testPromise {
assertHome(t, tb, p, tt.waitUntil, func() error {
result := p.TextContent("#pingRequestText", nil)
tt.pingRequestTextAssert(result)

Expand All @@ -314,7 +309,7 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
// This shouldn't block and return after calling hasLifecycleEventFired.
p.WaitForLoadState(tt.waitUntil.String(), nil)

return testPromise{}
return nil
}, nil, "")
})
}
Expand Down Expand Up @@ -391,7 +386,7 @@ func TestLifecycleReload(t *testing.T) {
withPingHandler(t, tb, tt.pingSlowness, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

assertHome(t, tb, p, tt.waitUntil, func() testPromise {
assertHome(t, tb, p, tt.waitUntil, func() error {
result := p.TextContent("#pingRequestText", nil)
tt.pingRequestTextAssert(result, 10)

Expand All @@ -410,7 +405,7 @@ func TestLifecycleReload(t *testing.T) {
result = p.TextContent("#pingJSText", nil)
tt.pingJSTextAssert(result)

return testPromise{}
return nil
}, nil, "")
})
}
Expand Down Expand Up @@ -488,14 +483,14 @@ func TestLifecycleGotoWithSubFrame(t *testing.T) {
withPingHandler(t, tb, tt.pingSlowness, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil, true)

assertHome(t, tb, p, tt.waitUntil, func() testPromise {
assertHome(t, tb, p, tt.waitUntil, func() error {
result := p.TextContent("#subFramePingRequestText", nil)
tt.pingRequestTextAssert(result)

result = p.TextContent("#subFramePingJSText", nil)
tt.pingJSTextAssert(result)

return testPromise{}
return nil
}, nil, "")
})
}
Expand Down Expand Up @@ -559,14 +554,14 @@ func TestLifecycleGoto(t *testing.T) {
withPingHandler(t, tb, tt.pingSlowness, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

assertHome(t, tb, p, tt.waitUntil, func() testPromise {
assertHome(t, tb, p, tt.waitUntil, func() error {
result := p.TextContent("#pingRequestText", nil)
tt.pingRequestTextAssert(result)

result = p.TextContent("#pingJSText", nil)
tt.pingJSTextAssert(result)

return testPromise{}
return nil
}, nil, "")
})
}
Expand Down Expand Up @@ -594,11 +589,11 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {

withPingJSHandler(t, tb, false, nil, false)

assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() error {
result := p.TextContent("#pingJSText", nil)
assert.EqualValues(t, "ping.js loaded from server", result)

return testPromise{}
return nil
}, nil, "")
})

Expand All @@ -613,14 +608,14 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
withPingHandler(t, tb, 50*time.Millisecond, ch)
withPingJSHandler(t, tb, false, ch, false)

assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() error {
result := p.TextContent("#pingRequestText", nil)
assert.EqualValues(t, "Waiting... pong 4 - for loop complete", result)

result = p.TextContent("#pingJSText", nil)
assert.EqualValues(t, "ping.js loaded from server", result)

return testPromise{}
return nil
}, nil, "")
})

Expand All @@ -633,11 +628,11 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
withHomeHandler(t, tb, "lifecycle_no_ping_js.html")
withPingHandler(t, tb, 50*time.Millisecond, nil)

assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() error {
result := p.TextContent("#pingRequestText", nil)
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)

return testPromise{}
return nil
}, nil, "")
})
}
Expand Down Expand Up @@ -711,12 +706,12 @@ func assertHome(
t *testing.T,
tb *testBrowser, p api.Page,
waitUntil common.LifecycleEvent,
check func() testPromise, secondCheck func(), wantError string,
check func() error, secondCheck func(), wantError string,
) {
t.Helper()

var resolved, rejected bool
err := tb.await(func() error {
err := func() error {
opts := tb.toGojaValue(common.FrameGotoOptions{
WaitUntil: waitUntil,
Timeout: 30 * time.Second,
Expand All @@ -728,19 +723,17 @@ func assertHome(
rejected = true
}

// TODO
// remove this once we have finished our work on the mapping layer.
// for now: provide a fake promise
fakePromise := k6ext.Promise(tb.vu.Context(), func() (result any, reason error) {
return nil, nil
})
prm := tb.promise(fakePromise).then(check)
err = check()
if err != nil {
return err
}

if secondCheck != nil {
prm.then(secondCheck)
secondCheck()
}

return nil
})
}()

if wantError != "" {
require.EqualError(t, err, wantError)
Expand Down
9 changes: 1 addition & 8 deletions tests/test_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,6 @@ func (b *testBrowser) runJavaScript(s string, args ...any) (goja.Value, error) {
return b.runtime().RunString(fmt.Sprintf(s, args...))
}

// await runs fn in the event loop and awaits its return.
// Note: Do not confuse the method name with await in JavaScript.
func (b *testBrowser) await(fn func() error) error {
b.t.Helper()
return b.vu.Loop.Start(fn)
}

// Run the given functions in parallel and waits for them to finish.
func (b *testBrowser) run(ctx context.Context, fs ...func() error) error { //nolint:unused,deadcode
b.t.Helper()
Expand Down Expand Up @@ -271,7 +264,7 @@ func (b *testBrowser) awaitWithTimeout(timeout time.Duration, fn func() error) e
errC := make(chan error)
go func() {
defer close(errC)
errC <- b.await(fn)
errC <- fn()
}()

// use timer instead of time.After to not leak time.After for the duration of the timeout
Expand Down

0 comments on commit e433cb7

Please sign in to comment.