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

Commit 40fbc7d

Browse files
committed
Add a WaitForNavigation timeout test
This test locks in the behaviour which highlights that it will timeout if the function call is made after the navigation has ended.
1 parent e21c08c commit 40fbc7d

File tree

1 file changed

+74
-18
lines changed

1 file changed

+74
-18
lines changed

tests/lifecycle_wait_test.go

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ import (
1414
"github.com/grafana/xk6-browser/common"
1515
)
1616

17+
// General guidelines on lifecycle events:
18+
//
19+
// - load: This lifecycle event is emitted by the browser once:
20+
// 1. The HTML is loaded;
21+
// 2. The async scripts have loaded;
22+
// It does not wait for the other network requests to
23+
// complete.
24+
//
25+
// - domcontentloaded: This lifecycle event is emitted by the
26+
// browser once:
27+
// 1. The HTML is loaded;
28+
// It does not wait for the async scripts or
29+
// the other network requests to complete.
30+
//
31+
// - networkidle: This lifecycle event is emitted by the browser once:
32+
// 1. The HTML is loaded;
33+
// 2. The async scripts have loaded;
34+
// 3. All other network requests have completed;
35+
1736
func TestLifecycleWaitForNavigation(t *testing.T) {
1837
// Test description
1938
//
@@ -119,24 +138,61 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
119138
}
120139
}
121140

122-
// General guidelines on lifecycle events:
123-
//
124-
// - load: This lifecycle event is emitted by the browser once:
125-
// 1. The HTML is loaded;
126-
// 2. The async scripts have loaded;
127-
// It does not wait for the other network requests to
128-
// complete.
129-
//
130-
// - domcontentloaded: This lifecycle event is emitted by the
131-
// browser once:
132-
// 1. The HTML is loaded;
133-
// It does not wait for the async scripts or
134-
// the other network requests to complete.
135-
//
136-
// - networkidle: This lifecycle event is emitted by the browser once:
137-
// 1. The HTML is loaded;
138-
// 2. The async scripts have loaded;
139-
// 3. All other network requests have completed;
141+
func TestLifecycleWaitForNavigationTimeout(t *testing.T) {
142+
t.Parallel()
143+
144+
// Test description
145+
//
146+
// 1. goto /home and wait for the networkidle lifecycle event.
147+
// 2. use WaitForNavigation with networkidle.
148+
//
149+
// Success criteria: Time out reached after navigation completed and
150+
// wait for lifecycle event set, to signify that
151+
// WaitForNavigation must be set before we navigate
152+
// to a new page.
153+
154+
tb := newTestBrowser(t, withFileServer())
155+
p := tb.NewPage(nil)
156+
157+
withHomeHandler(t, tb, "prolonged_network_idle_10.html")
158+
withPingHandler(t, tb, 0, nil)
159+
160+
waitUntil := common.LifecycleEventNetworkIdle
161+
var resolved, rejected bool
162+
err := tb.await(func() error {
163+
opts := tb.toGojaValue(common.FrameGotoOptions{
164+
WaitUntil: waitUntil,
165+
Timeout: 30 * time.Second,
166+
})
167+
prm := tb.promise(p.Goto(tb.URL("/home"), opts)).then(
168+
func() testPromise {
169+
result := p.TextContent("#pingRequestText", nil)
170+
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
171+
172+
waitForNav := p.WaitForNavigation(tb.toGojaValue(&common.FrameWaitForNavigationOptions{
173+
Timeout: 1000,
174+
WaitUntil: waitUntil,
175+
}))
176+
177+
return tb.promise(waitForNav)
178+
},
179+
)
180+
prm.then(
181+
func() {
182+
resolved = true
183+
},
184+
func() {
185+
rejected = true
186+
},
187+
)
188+
189+
return nil
190+
})
191+
require.NoError(t, err)
192+
193+
assert.False(t, resolved)
194+
assert.True(t, rejected)
195+
}
140196

141197
func TestLifecycleWaitForLoadState(t *testing.T) {
142198
t.Parallel()

0 commit comments

Comments
 (0)