Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Refactor test to check promise states
Browse files Browse the repository at this point in the history
The test now checks the state of the promise before passing the test.

Resolves: #467 (comment)
  • Loading branch information
ankur22 committed Aug 8, 2022
1 parent 886eda5 commit 331465f
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions tests/frame_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/grafana/xk6-browser/common"

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,7 +20,7 @@ func TestWaitForFrameNavigationWithinDocument(t *testing.T) {
}
t.Parallel()

var timeout time.Duration = 200
timeout := 2000 * time.Millisecond
if os.Getenv("CI") == "true" {
// Increase the timeout on underprovisioned CI machines to minimize
// chances of intermittent failures.
Expand All @@ -37,33 +39,40 @@ func TestWaitForFrameNavigationWithinDocument(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

errc := make(chan error, 1)
errc := make(chan error)
go func() {
tb := newTestBrowser(t, withFileServer())
p := tb.NewPage(nil)

resp := p.Goto(tb.staticURL("/nav_in_doc.html"), nil)
require.NotNil(t, resp)

// A click right away could possibly trigger navigation before we
// had a chance to call WaitForNavigation below, so give it some
// time to simulate the JS overhead, waiting for XHR
// response, etc.
<-time.After(timeout * time.Millisecond) //nolint:durationcheck

// if one of the promises panics, err will contain the error
errc <- tb.await(func() error {
_ = p.Click(tc.selector, nil)
_ = p.WaitForNavigation(tb.toGojaValue(&common.FrameWaitForNavigationOptions{
Timeout: 3 * timeout, // interpreted as ms
// Callbacks that are initiated internally by click and WaitForNavigation
// need to be called from the event loop itself, otherwise the callback
// doesn't work. The await below needs to first return before the callback
// will resolve/reject.
var wfnPromise, cPromise *goja.Promise
err := tb.await(func() error {
wfnPromise = p.WaitForNavigation(tb.toGojaValue(&common.FrameWaitForNavigationOptions{
Timeout: timeout, // interpreted as ms
}))
cPromise = p.Click(tc.selector, nil)
return nil
})
if err != nil {
errc <- err
}

assert.Equal(t, goja.PromiseStateFulfilled, wfnPromise.State())
assert.Equal(t, goja.PromiseStateFulfilled, cPromise.State())

errc <- nil
}()

select {
case err := <-errc:
require.NoError(t, err)
case <-time.After(5 * timeout * time.Millisecond): //nolint:durationcheck
assert.NoError(t, err)
case <-time.After(timeout):
t.Fatal("Test timed out")
}
})
Expand Down

0 comments on commit 331465f

Please sign in to comment.