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

Commit fa3066c

Browse files
committed
Add goto lifecycle with subframe tests
These tests ensure that we receive the subframe events before the lifecycle events for the main/root frame. This will ensure that the waitUntil only unblocks once all frames are ready.
1 parent 760acfa commit fa3066c

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

tests/lifecycle_wait_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,86 @@ func TestLifecycleReload(t *testing.T) {
235235
}
236236
}
237237

238+
func TestLifecycleGotoWithSubFrame(t *testing.T) {
239+
t.Parallel()
240+
241+
// Test description
242+
//
243+
// 1. goto /home (with iframe to /sub) and wait for the specified lifecycle event.
244+
//
245+
// Success criteria: The web page (all frames) is in the expected state
246+
// once we receive the specified lifecycle event from
247+
// the browser.
248+
249+
tests := []struct {
250+
name string
251+
pingSlowness time.Duration
252+
pingJSSlow bool
253+
waitUntil common.LifecycleEvent
254+
pingRequestTextAssert func(result string)
255+
pingJSTextAssert func(result string)
256+
}{
257+
{
258+
name: "load",
259+
pingSlowness: time.Millisecond * 100,
260+
pingJSSlow: false,
261+
waitUntil: common.LifecycleEventLoad,
262+
pingRequestTextAssert: func(result string) {
263+
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
264+
},
265+
pingJSTextAssert: func(result string) {
266+
assert.EqualValues(t, "ping.js loaded from server", result)
267+
},
268+
},
269+
{
270+
name: "domcontentloaded",
271+
pingSlowness: time.Millisecond * 100,
272+
pingJSSlow: true,
273+
waitUntil: common.LifecycleEventDOMContentLoad,
274+
pingRequestTextAssert: func(result string) {
275+
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
276+
},
277+
pingJSTextAssert: func(result string) {
278+
assert.EqualValues(t, "Waiting...", result)
279+
},
280+
},
281+
{
282+
name: "networkidle",
283+
pingSlowness: 0,
284+
pingJSSlow: false,
285+
waitUntil: common.LifecycleEventNetworkIdle,
286+
pingRequestTextAssert: func(result string) {
287+
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
288+
},
289+
pingJSTextAssert: func(result string) {
290+
assert.EqualValues(t, "ping.js loaded from server", result)
291+
},
292+
},
293+
}
294+
295+
for _, tt := range tests {
296+
t.Run(tt.name, func(t *testing.T) {
297+
t.Parallel()
298+
299+
tb := newTestBrowser(t, withFileServer())
300+
p := tb.NewPage(nil)
301+
302+
withHomeHandler(t, tb, "lifecycle_main_frame.html")
303+
withSubHandler(t, tb, "lifecycle_subframe.html")
304+
withPingHandler(t, tb, tt.pingSlowness, nil)
305+
withPingJSSubFrameHandler(t, tb, tt.pingJSSlow, nil)
306+
307+
assertHome(t, tb, p, tt.waitUntil, func() {
308+
result := p.TextContent("#subFramePingRequestText", nil)
309+
tt.pingRequestTextAssert(result)
310+
311+
result = p.TextContent("#subFramePingJSText", nil)
312+
tt.pingJSTextAssert(result)
313+
})
314+
})
315+
}
316+
}
317+
238318
func TestLifecycleGoto(t *testing.T) {
239319
t.Parallel()
240320

@@ -373,6 +453,14 @@ func withHomeHandler(t *testing.T, tb *testBrowser, htmlFile string) {
373453
})
374454
}
375455

456+
func withSubHandler(t *testing.T, tb *testBrowser, htmlFile string) {
457+
t.Helper()
458+
459+
tb.withHandler("/sub", func(w http.ResponseWriter, r *http.Request) {
460+
http.Redirect(w, r, tb.staticURL(htmlFile), http.StatusMovedPermanently)
461+
})
462+
}
463+
376464
func withPingHandler(t *testing.T, tb *testBrowser, slow time.Duration, ch chan bool) {
377465
t.Helper()
378466

@@ -415,6 +503,31 @@ func withPingJSHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool) {
415503
})
416504
}
417505

506+
func withPingJSSubFrameHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool) {
507+
t.Helper()
508+
509+
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
510+
script := `
511+
var pingJSTextOutput = document.getElementById("pingJSText");
512+
var parentOutputServerMsg = window.parent.document.getElementById('subFramePingJSText');
513+
514+
pingJSTextOutput.innerText = "ping.js loaded from server";
515+
parentOutputServerMsg.innerText = pingJSTextOutput.innerText;
516+
`
517+
if slow {
518+
script = `
519+
await new Promise(resolve => setTimeout(resolve, 1000));
520+
521+
` + script
522+
}
523+
fmt.Fprint(w, script)
524+
525+
if ch != nil {
526+
close(ch)
527+
}
528+
})
529+
}
530+
418531
func assertHome(
419532
t *testing.T,
420533
tb *testBrowser,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
3+
<head></head>
4+
5+
<body>
6+
<div id="frameType">main</div>
7+
<div id="subFramePingRequestText">Waiting...</div>
8+
<div id="subFramePingJSText">Waiting...</div>
9+
<iframe src="/sub"></iframe>
10+
</body>
11+
12+
</html>

tests/static/lifecycle_subframe.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<html>
2+
3+
<head></head>
4+
5+
<body>
6+
<div id="pingRequestText">Waiting...</div>
7+
<div id="pingJSText">Waiting...</div>
8+
9+
<script>
10+
var pingRequestTextOutput = document.getElementById("pingRequestText");
11+
var parentOutput = window.parent.document.getElementById('subFramePingRequestText');
12+
13+
var p = pingRequestText();
14+
p.then(() => {
15+
pingRequestTextOutput.innerText += ' - for loop complete';
16+
if (parentOutput) {
17+
parentOutput.innerText = pingRequestTextOutput.innerText;
18+
}
19+
})
20+
21+
async function pingRequestText() {
22+
for (var i = 0; i < 10; i++) {
23+
await fetch('/ping')
24+
.then(response => response.text())
25+
.then((data) => {
26+
pingRequestTextOutput.innerText = 'Waiting... ' + data;
27+
if (parentOutput) {
28+
parentOutput.innerText = pingRequestTextOutput.innerText;
29+
}
30+
});
31+
}
32+
}
33+
</script>
34+
<script src="/ping.js" async></script>
35+
</body>
36+
37+
</html>

0 commit comments

Comments
 (0)