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

Commit b5d61b6

Browse files
committed
Refactor reload lifecycle lifecycle test
Deduplicate them and convert them to a table test.
1 parent 9837fa5 commit b5d61b6

File tree

1 file changed

+91
-83
lines changed

1 file changed

+91
-83
lines changed

tests/lifecycle_wait_test.go

Lines changed: 91 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -157,100 +157,108 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
157157
}
158158
}
159159

160-
func TestLifecycleReloadLoad(t *testing.T) {
160+
func TestLifecycleReload(t *testing.T) {
161161
t.Parallel()
162162

163-
tb := newTestBrowser(t, withFileServer())
164-
p := tb.NewPage(nil)
165-
166-
withHomeHandler(t, tb, "reload_lifecycle.html")
167-
withPingHandler(t, tb, time.Millisecond*100, nil)
168-
withPingJSHandler(t, tb, false, nil)
169-
170-
waitUntil := common.LifecycleEventLoad
171-
assertHome(t, tb, p, waitUntil, func() {
172-
result := p.TextContent("#pingRequestText", nil)
173-
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
174-
175-
result = p.TextContent("#pingJSText", nil)
176-
assert.EqualValues(t, "ping.js loaded from server", result)
177-
178-
opts := tb.toGojaValue(common.PageReloadOptions{
179-
WaitUntil: waitUntil,
180-
Timeout: 30 * time.Second,
181-
})
182-
p.Reload(opts)
183-
184-
result = p.TextContent("#pingRequestText", nil)
185-
assert.NotEqualValues(t, "Waiting... pong 20 - for loop complete", result)
186-
187-
result = p.TextContent("#pingJSText", nil)
188-
assert.EqualValues(t, "ping.js loaded from server", result)
189-
})
190-
}
191-
192-
func TestLifecycleReloadDOMContentLoaded(t *testing.T) {
193-
t.Parallel()
194-
195-
tb := newTestBrowser(t, withFileServer())
196-
p := tb.NewPage(nil)
197-
198-
withHomeHandler(t, tb, "reload_lifecycle.html")
199-
withPingHandler(t, tb, time.Millisecond*100, nil)
200-
withPingJSHandler(t, tb, true, nil)
201-
202-
waitUntil := common.LifecycleEventDOMContentLoad
203-
assertHome(t, tb, p, waitUntil, func() {
204-
result := p.TextContent("#pingRequestText", nil)
205-
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
206-
207-
result = p.TextContent("#pingJSText", nil)
208-
assert.EqualValues(t, "Waiting...", result)
209-
210-
opts := tb.toGojaValue(common.PageReloadOptions{
211-
WaitUntil: waitUntil,
212-
Timeout: 30 * time.Second,
213-
})
214-
p.Reload(opts)
163+
tests := []struct {
164+
name string
165+
pingSlowness time.Duration
166+
pingJSSlow bool
167+
waitUntil common.LifecycleEvent
168+
pingRequestTextAssert func(result string, pingCount int)
169+
pingJSTextAssert func(result string)
170+
}{
171+
{
172+
// Test description
173+
//
174+
// 1. goto /home and wait for the load lifecycle event.
175+
// 2. reload the page and wait with for the load lifecycle event.
176+
//
177+
// Success criteria: The resulting page after reload is the same as
178+
// the initial navigation with goto.
179+
name: "load",
180+
pingSlowness: time.Millisecond * 100,
181+
pingJSSlow: false,
182+
waitUntil: common.LifecycleEventLoad,
183+
pingRequestTextAssert: func(result string, pingCount int) {
184+
assert.NotEqualValues(t, fmt.Sprintf("Waiting... pong %d - for loop complete", pingCount), result)
185+
},
186+
pingJSTextAssert: func(result string) {
187+
assert.EqualValues(t, "ping.js loaded from server", result)
188+
},
189+
},
190+
{
191+
// Test description
192+
//
193+
// 1. goto /home and wait for the domcontentloaded lifecycle event.
194+
// 2. reload the page and wait with for the domcontentloaded lifecycle event.
195+
//
196+
// Success criteria: The resulting page after reload is the same as
197+
// the initial navigation with goto.
198+
name: "domcontentloaded",
199+
pingSlowness: time.Millisecond * 100,
200+
pingJSSlow: true,
201+
waitUntil: common.LifecycleEventDOMContentLoad,
202+
pingRequestTextAssert: func(result string, pingCount int) {
203+
assert.NotEqualValues(t, fmt.Sprintf("Waiting... pong %d - for loop complete", pingCount), result)
204+
},
205+
pingJSTextAssert: func(result string) {
206+
assert.EqualValues(t, "Waiting...", result)
207+
},
208+
},
209+
{
210+
// Test description
211+
//
212+
// 1. goto /home and wait for the networkidle lifecycle event.
213+
// 2. reload the page and wait with for the networkidle lifecycle event.
214+
//
215+
// Success criteria: The resulting page after reload is the same as
216+
// the initial navigation with goto.
217+
name: "networkidle",
218+
pingSlowness: 0,
219+
pingJSSlow: false,
220+
waitUntil: common.LifecycleEventNetworkIdle,
221+
pingRequestTextAssert: func(result string, pingCount int) {
222+
assert.EqualValues(t, fmt.Sprintf("Waiting... pong %d - for loop complete", pingCount), result)
223+
},
224+
pingJSTextAssert: func(result string) {
225+
assert.EqualValues(t, "ping.js loaded from server", result)
226+
},
227+
},
228+
}
215229

216-
result = p.TextContent("#pingRequestText", nil)
217-
assert.NotEqualValues(t, "Waiting... pong 20 - for loop complete", result)
230+
for _, tt := range tests {
231+
t.Run(tt.name, func(t *testing.T) {
232+
t.Parallel()
218233

219-
result = p.TextContent("#pingJSText", nil)
220-
assert.EqualValues(t, "Waiting...", result)
221-
})
222-
}
234+
tb := newTestBrowser(t, withFileServer())
235+
p := tb.NewPage(nil)
223236

224-
func TestLifecycleReloadNetworkIdle(t *testing.T) {
225-
t.Parallel()
237+
withHomeHandler(t, tb, "reload_lifecycle.html")
238+
withPingHandler(t, tb, tt.pingSlowness, nil)
239+
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
226240

227-
tb := newTestBrowser(t, withFileServer())
228-
p := tb.NewPage(nil)
241+
assertHome(t, tb, p, tt.waitUntil, func() {
242+
result := p.TextContent("#pingRequestText", nil)
243+
tt.pingRequestTextAssert(result, 10)
229244

230-
withHomeHandler(t, tb, "reload_lifecycle.html")
231-
withPingHandler(t, tb, 0, nil)
232-
withPingJSHandler(t, tb, false, nil)
245+
result = p.TextContent("#pingJSText", nil)
246+
tt.pingJSTextAssert(result)
233247

234-
waitUntil := common.LifecycleEventNetworkIdle
235-
assertHome(t, tb, p, waitUntil, func() {
236-
result := p.TextContent("#pingRequestText", nil)
237-
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
248+
opts := tb.toGojaValue(common.PageReloadOptions{
249+
WaitUntil: tt.waitUntil,
250+
Timeout: 30 * time.Second,
251+
})
252+
p.Reload(opts)
238253

239-
result = p.TextContent("#pingJSText", nil)
240-
assert.EqualValues(t, "ping.js loaded from server", result)
254+
result = p.TextContent("#pingRequestText", nil)
255+
tt.pingRequestTextAssert(result, 20)
241256

242-
opts := tb.toGojaValue(common.PageReloadOptions{
243-
WaitUntil: waitUntil,
244-
Timeout: 30 * time.Second,
257+
result = p.TextContent("#pingJSText", nil)
258+
tt.pingJSTextAssert(result)
259+
})
245260
})
246-
p.Reload(opts)
247-
248-
result = p.TextContent("#pingRequestText", nil)
249-
assert.EqualValues(t, "Waiting... pong 20 - for loop complete", result)
250-
251-
result = p.TextContent("#pingJSText", nil)
252-
assert.EqualValues(t, "ping.js loaded from server", result)
253-
})
261+
}
254262
}
255263

256264
func TestLifecycleNetworkIdle(t *testing.T) {

0 commit comments

Comments
 (0)