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

Commit 616400f

Browse files
committed
Refactor lifecycle test ping.js handler
Deduplicate the ping.js handler in the lifecycle tests.
1 parent 26a0687 commit 616400f

File tree

1 file changed

+31
-67
lines changed

1 file changed

+31
-67
lines changed

tests/lifecycle_wait_test.go

Lines changed: 31 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ func TestLifecycleWaitForLoadStateLoad(t *testing.T) {
3737
})
3838

3939
withPingHandler(t, tb, time.Millisecond*100, nil)
40-
41-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
42-
fmt.Fprintf(w, `
43-
var pingJSTextOutput = document.getElementById("pingJSText");
44-
pingJSTextOutput.innerText = "ping.js loaded from server";
45-
`)
46-
})
40+
withPingJSHandler(t, tb, false, nil)
4741

4842
waitUntil := common.LifecycleEventLoad
4943
assertHome(t, tb, p, waitUntil, func() {
@@ -81,15 +75,7 @@ func TestLifecycleWaitForLoadStateDOMContentLoaded(t *testing.T) {
8175
})
8276

8377
withPingHandler(t, tb, time.Millisecond*100, nil)
84-
85-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
86-
fmt.Fprintf(w, `
87-
await new Promise(resolve => setTimeout(resolve, 1000));
88-
89-
var pingJSTextOutput = document.getElementById("pingJSText");
90-
pingJSTextOutput.innerText = "ping.js loaded from server";
91-
`)
92-
})
78+
withPingJSHandler(t, tb, true, nil)
9379

9480
waitUntil := common.LifecycleEventDOMContentLoad
9581
assertHome(t, tb, p, waitUntil, func() {
@@ -125,13 +111,7 @@ func TestLifecycleWaitForLoadStateNetworkIdle(t *testing.T) {
125111
})
126112

127113
withPingHandler(t, tb, 0, nil)
128-
129-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
130-
fmt.Fprintf(w, `
131-
var pingJSTextOutput = document.getElementById("pingJSText");
132-
pingJSTextOutput.innerText = "ping.js loaded from server";
133-
`)
134-
})
114+
withPingJSHandler(t, tb, false, nil)
135115

136116
waitUntil := common.LifecycleEventNetworkIdle
137117
assertHome(t, tb, p, waitUntil, func() {
@@ -166,13 +146,7 @@ func TestLifecycleWaitForLoadStateDOMContentLoadedThenNetworkIdle(t *testing.T)
166146
})
167147

168148
withPingHandler(t, tb, time.Millisecond*100, nil)
169-
170-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
171-
fmt.Fprintf(w, `
172-
var pingJSTextOutput = document.getElementById("pingJSText");
173-
pingJSTextOutput.innerText = "ping.js loaded from server";
174-
`)
175-
})
149+
withPingJSHandler(t, tb, false, nil)
176150

177151
assertHome(t, tb, p, common.LifecycleEventDOMContentLoad, func() {
178152
p.WaitForLoadState(common.LifecycleEventNetworkIdle.String(), nil)
@@ -195,13 +169,7 @@ func TestLifecycleReloadLoad(t *testing.T) {
195169
})
196170

197171
withPingHandler(t, tb, time.Millisecond*100, nil)
198-
199-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
200-
fmt.Fprintf(w, `
201-
var pingJSTextOutput = document.getElementById("pingJSText");
202-
pingJSTextOutput.innerText = "ping.js loaded from server";
203-
`)
204-
})
172+
withPingJSHandler(t, tb, false, nil)
205173

206174
waitUntil := common.LifecycleEventLoad
207175
assertHome(t, tb, p, waitUntil, func() {
@@ -235,15 +203,7 @@ func TestLifecycleReloadDOMContentLoaded(t *testing.T) {
235203
})
236204

237205
withPingHandler(t, tb, time.Millisecond*100, nil)
238-
239-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
240-
fmt.Fprintf(w, `
241-
await new Promise(resolve => setTimeout(resolve, 1000));
242-
243-
var pingJSTextOutput = document.getElementById("pingJSText");
244-
pingJSTextOutput.innerText = "ping.js loaded from server";
245-
`)
246-
})
206+
withPingJSHandler(t, tb, true, nil)
247207

248208
waitUntil := common.LifecycleEventDOMContentLoad
249209
assertHome(t, tb, p, waitUntil, func() {
@@ -277,13 +237,7 @@ func TestLifecycleReloadNetworkIdle(t *testing.T) {
277237
})
278238

279239
withPingHandler(t, tb, 0, nil)
280-
281-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
282-
fmt.Fprintf(w, `
283-
var pingJSTextOutput = document.getElementById("pingJSText");
284-
pingJSTextOutput.innerText = "ping.js loaded from server";
285-
`)
286-
})
240+
withPingJSHandler(t, tb, false, nil)
287241

288242
waitUntil := common.LifecycleEventNetworkIdle
289243
assertHome(t, tb, p, waitUntil, func() {
@@ -327,12 +281,7 @@ func TestLifecycleNetworkIdle(t *testing.T) {
327281
`)
328282
})
329283

330-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
331-
fmt.Fprintf(w, `
332-
var pingJSTextOutput = document.getElementById("pingJSText");
333-
pingJSTextOutput.innerText = "ping.js loaded from server";
334-
`)
335-
})
284+
withPingJSHandler(t, tb, false, nil)
336285

337286
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() {
338287
result := p.TextContent("#pingJSText", nil)
@@ -351,14 +300,7 @@ func TestLifecycleNetworkIdle(t *testing.T) {
351300

352301
ch := make(chan bool)
353302
withPingHandler(t, tb, time.Millisecond*50, ch)
354-
355-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
356-
fmt.Fprintf(w, `
357-
var pingJSTextOutput = document.getElementById("pingJSText");
358-
pingJSTextOutput.innerText = "ping.js loaded from server";
359-
`)
360-
close(ch)
361-
})
303+
withPingJSHandler(t, tb, false, ch)
362304

363305
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() {
364306
result := p.TextContent("#pingRequestText", nil)
@@ -407,6 +349,28 @@ func withPingHandler(t *testing.T, tb *testBrowser, slow time.Duration, ch chan
407349
})
408350
}
409351

352+
func withPingJSHandler(t *testing.T, tb *testBrowser, slow bool, ch chan bool) {
353+
t.Helper()
354+
355+
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
356+
script := `
357+
var pingJSTextOutput = document.getElementById("pingJSText");
358+
pingJSTextOutput.innerText = "ping.js loaded from server";
359+
`
360+
if slow {
361+
script = `
362+
await new Promise(resolve => setTimeout(resolve, 1000));
363+
364+
` + script
365+
}
366+
fmt.Fprint(w, script)
367+
368+
if ch != nil {
369+
close(ch)
370+
}
371+
})
372+
}
373+
410374
func assertHome(
411375
t *testing.T,
412376
tb *testBrowser,

0 commit comments

Comments
 (0)