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

Commit 2be5854

Browse files
committed
Refactor the lifecycle tests
This is in preparation for the WaitForNavigation lifecycle tests.
1 parent fa3066c commit 2be5854

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

tests/lifecycle_wait_test.go

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
126126
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
127127

128128
if tt.assertFunc != nil {
129-
assertHome(t, tb, p, tt.waitUntil, func() { tt.assertFunc(p) })
129+
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
130+
tt.assertFunc(p)
131+
return testPromise{}
132+
}, nil)
130133
return
131134
}
132135

133-
assertHome(t, tb, p, tt.waitUntil, func() {
136+
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
134137
result := p.TextContent("#pingRequestText", nil)
135138
tt.pingRequestTextAssert(result)
136139

@@ -139,7 +142,9 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
139142

140143
// This shouldn't block and return after calling hasLifecycleEventFired.
141144
p.WaitForLoadState(tt.waitUntil.String(), nil)
142-
})
145+
146+
return testPromise{}
147+
}, nil)
143148
})
144149
}
145150
}
@@ -212,7 +217,7 @@ func TestLifecycleReload(t *testing.T) {
212217
withPingHandler(t, tb, tt.pingSlowness, nil)
213218
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
214219

215-
assertHome(t, tb, p, tt.waitUntil, func() {
220+
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
216221
result := p.TextContent("#pingRequestText", nil)
217222
tt.pingRequestTextAssert(result, 10)
218223

@@ -230,7 +235,9 @@ func TestLifecycleReload(t *testing.T) {
230235

231236
result = p.TextContent("#pingJSText", nil)
232237
tt.pingJSTextAssert(result)
233-
})
238+
239+
return testPromise{}
240+
}, nil)
234241
})
235242
}
236243
}
@@ -304,13 +311,15 @@ func TestLifecycleGotoWithSubFrame(t *testing.T) {
304311
withPingHandler(t, tb, tt.pingSlowness, nil)
305312
withPingJSSubFrameHandler(t, tb, tt.pingJSSlow, nil)
306313

307-
assertHome(t, tb, p, tt.waitUntil, func() {
314+
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
308315
result := p.TextContent("#subFramePingRequestText", nil)
309316
tt.pingRequestTextAssert(result)
310317

311318
result = p.TextContent("#subFramePingJSText", nil)
312319
tt.pingJSTextAssert(result)
313-
})
320+
321+
return testPromise{}
322+
}, nil)
314323
})
315324
}
316325
}
@@ -370,13 +379,15 @@ func TestLifecycleGoto(t *testing.T) {
370379
withPingHandler(t, tb, tt.pingSlowness, nil)
371380
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
372381

373-
assertHome(t, tb, p, tt.waitUntil, func() {
382+
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
374383
result := p.TextContent("#pingRequestText", nil)
375384
tt.pingRequestTextAssert(result)
376385

377386
result = p.TextContent("#pingJSText", nil)
378387
tt.pingJSTextAssert(result)
379-
})
388+
389+
return testPromise{}
390+
}, nil)
380391
})
381392
}
382393
}
@@ -403,10 +414,12 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
403414

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

406-
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() {
417+
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
407418
result := p.TextContent("#pingJSText", nil)
408419
assert.EqualValues(t, "ping.js loaded from server", result)
409-
})
420+
421+
return testPromise{}
422+
}, nil)
410423
})
411424

412425
t.Run("doesn't unblock wait for networkIdle too early", func(t *testing.T) {
@@ -420,13 +433,15 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
420433
withPingHandler(t, tb, time.Millisecond*50, ch)
421434
withPingJSHandler(t, tb, false, ch)
422435

423-
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() {
436+
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
424437
result := p.TextContent("#pingRequestText", nil)
425438
assert.EqualValues(t, "Waiting... pong 4 - for loop complete", result)
426439

427440
result = p.TextContent("#pingJSText", nil)
428441
assert.EqualValues(t, "ping.js loaded from server", result)
429-
})
442+
443+
return testPromise{}
444+
}, nil)
430445
})
431446

432447
t.Run("doesn't unblock wait on networkIdle early when load and domcontentloaded complete at once", func(t *testing.T) {
@@ -438,10 +453,12 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
438453
withHomeHandler(t, tb, "prolonged_network_idle_10.html")
439454
withPingHandler(t, tb, time.Millisecond*50, nil)
440455

441-
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() {
456+
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() testPromise {
442457
result := p.TextContent("#pingRequestText", nil)
443458
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
444-
})
459+
460+
return testPromise{}
461+
}, nil)
445462
})
446463
}
447464

@@ -530,10 +547,9 @@ func withPingJSSubFrameHandler(t *testing.T, tb *testBrowser, slow bool, ch chan
530547

531548
func assertHome(
532549
t *testing.T,
533-
tb *testBrowser,
534-
p api.Page,
550+
tb *testBrowser, p api.Page,
535551
waitUntil common.LifecycleEvent,
536-
check func(),
552+
check func() testPromise, secondCheck func(),
537553
) {
538554
t.Helper()
539555

@@ -543,15 +559,20 @@ func assertHome(
543559
WaitUntil: waitUntil,
544560
Timeout: 30 * time.Second,
545561
})
546-
tb.promise(p.Goto(tb.URL("/home"), opts)).then(
547-
func() {
548-
check()
562+
prm := tb.promise(p.Goto(tb.URL("/home"), opts)).then(
563+
func() testPromise {
549564
resolved = true
565+
return check()
550566
},
551567
func() {
552568
rejected = true
553569
},
554570
)
571+
if secondCheck != nil {
572+
prm.then(func() {
573+
secondCheck()
574+
})
575+
}
555576

556577
return nil
557578
})

0 commit comments

Comments
 (0)