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

Commit 31365bd

Browse files
committed
Refactor lifecycle helper method
1 parent 40fbc7d commit 31365bd

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

tests/lifecycle_wait_test.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
103103

104104
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
105105
withPingHandler(t, tb, tt.pingSlowness, nil)
106-
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
106+
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)
107107

108108
if tt.assertFunc != nil {
109109
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
@@ -284,7 +284,7 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
284284

285285
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
286286
withPingHandler(t, tb, tt.pingSlowness, nil)
287-
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
287+
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)
288288

289289
if tt.assertFunc != nil {
290290
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
@@ -376,7 +376,7 @@ func TestLifecycleReload(t *testing.T) {
376376

377377
withHomeHandler(t, tb, "reload_lifecycle.html")
378378
withPingHandler(t, tb, tt.pingSlowness, nil)
379-
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
379+
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)
380380

381381
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
382382
result := p.TextContent("#pingRequestText", nil)
@@ -470,7 +470,7 @@ func TestLifecycleGotoWithSubFrame(t *testing.T) {
470470
withHomeHandler(t, tb, "lifecycle_main_frame.html")
471471
withSubHandler(t, tb, "lifecycle_subframe.html")
472472
withPingHandler(t, tb, tt.pingSlowness, nil)
473-
withPingJSSubFrameHandler(t, tb, tt.pingJSSlow, nil)
473+
withPingJSHandler(t, tb, tt.pingJSSlow, nil, true)
474474

475475
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
476476
result := p.TextContent("#subFramePingRequestText", nil)
@@ -538,7 +538,7 @@ func TestLifecycleGoto(t *testing.T) {
538538

539539
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
540540
withPingHandler(t, tb, tt.pingSlowness, nil)
541-
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
541+
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)
542542

543543
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
544544
result := p.TextContent("#pingRequestText", nil)
@@ -573,7 +573,7 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
573573
`)
574574
})
575575

576-
withPingJSHandler(t, tb, false, nil)
576+
withPingJSHandler(t, tb, false, nil, false)
577577

578578
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
579579
result := p.TextContent("#pingJSText", nil)
@@ -592,7 +592,7 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
592592
withHomeHandler(t, tb, "prolonged_network_idle.html")
593593
ch := make(chan bool)
594594
withPingHandler(t, tb, time.Millisecond*50, ch)
595-
withPingJSHandler(t, tb, false, ch)
595+
withPingJSHandler(t, tb, false, ch, false)
596596

597597
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
598598
result := p.TextContent("#pingRequestText", nil)
@@ -659,39 +659,21 @@ func withPingHandler(t *testing.T, tb *testBrowser, slow time.Duration, ch chan
659659
})
660660
}
661661

662-
func withPingJSHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool) {
662+
func withPingJSHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool, withSubFrame bool) {
663663
t.Helper()
664664

665665
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
666666
script := `
667667
var pingJSTextOutput = document.getElementById("pingJSText");
668668
pingJSTextOutput.innerText = "ping.js loaded from server";
669669
`
670-
if slow {
671-
script = `
672-
await new Promise(resolve => setTimeout(resolve, 1000));
670+
if withSubFrame {
671+
script += `
673672
674-
` + script
675-
}
676-
fmt.Fprint(w, script)
677-
678-
if ch != nil {
679-
close(ch)
680-
}
681-
})
682-
}
683-
684-
func withPingJSSubFrameHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool) {
685-
t.Helper()
686-
687-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
688-
script := `
689-
var pingJSTextOutput = document.getElementById("pingJSText");
690673
var parentOutputServerMsg = window.parent.document.getElementById('subFramePingJSText');
691-
692-
pingJSTextOutput.innerText = "ping.js loaded from server";
693674
parentOutputServerMsg.innerText = pingJSTextOutput.innerText;
694-
`
675+
`
676+
}
695677
if slow {
696678
script = `
697679
await new Promise(resolve => setTimeout(resolve, 1000));

0 commit comments

Comments
 (0)