Skip to content

Commit

Permalink
Refactor lifecycle helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Nov 11, 2022
1 parent 068a4c1 commit 99980ef
Showing 1 changed file with 12 additions and 30 deletions.
42 changes: 12 additions & 30 deletions tests/lifecycle_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestLifecycleWaitForNavigation(t *testing.T) {

withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
withPingHandler(t, tb, tt.pingSlowness, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

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

withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
withPingHandler(t, tb, tt.pingSlowness, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

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

withHomeHandler(t, tb, "reload_lifecycle.html")
withPingHandler(t, tb, tt.pingSlowness, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

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

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

withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
withPingHandler(t, tb, tt.pingSlowness, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
withPingJSHandler(t, tb, tt.pingJSSlow, nil, false)

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

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

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

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

func withPingJSHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool) {
func withPingJSHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool, withSubFrame bool) {
t.Helper()

tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
script := `
var pingJSTextOutput = document.getElementById("pingJSText");
pingJSTextOutput.innerText = "ping.js loaded from server";
`
if slow {
script = `
await new Promise(resolve => setTimeout(resolve, 1000));
if withSubFrame {
script += `
` + script
}
fmt.Fprint(w, script)

if ch != nil {
close(ch)
}
})
}

func withPingJSSubFrameHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool) {
t.Helper()

tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
script := `
var pingJSTextOutput = document.getElementById("pingJSText");
var parentOutputServerMsg = window.parent.document.getElementById('subFramePingJSText');
pingJSTextOutput.innerText = "ping.js loaded from server";
parentOutputServerMsg.innerText = pingJSTextOutput.innerText;
`
`
}
if slow {
script = `
await new Promise(resolve => setTimeout(resolve, 1000));
Expand Down

0 comments on commit 99980ef

Please sign in to comment.