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

Commit 038e596

Browse files
committed
Refactor lifecycle tests home handler
Deduplicate the home handler in the lifecycle tests.
1 parent 616400f commit 038e596

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

tests/lifecycle_wait_test.go

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ import (
1414
"github.com/grafana/xk6-browser/common"
1515
)
1616

17+
// General guidelines on lifecycle events:
18+
//
19+
// - load: This lifecycle event is emitted by the browser once:
20+
// 1. The HTML is loaded;
21+
// 2. The async scripts have loaded;
22+
// It does not wait for the other network requests to
23+
// complete.
24+
//
25+
// - domcontentloaded: This lifecycle event is emitted by the
26+
// browser once:
27+
// 1. The HTML is loaded;
28+
// It does not wait for the async scripts or
29+
// the other network requests to complete.
30+
//
31+
// - networkidle: This lifecycle event is emitted by the browser once:
32+
// 1. The HTML is loaded;
33+
// 2. The async scripts have loaded;
34+
// 3. All other network requests have completed;
35+
1736
func TestLifecycleWaitForLoadStateLoad(t *testing.T) {
1837
// Test description
1938
//
@@ -27,15 +46,12 @@ func TestLifecycleWaitForLoadStateLoad(t *testing.T) {
2746
// (which is when load is fired). We also want
2847
// to ensure that the load event is stored
2948
// internally, and we don't block on WaitForLoadState.
30-
3149
t.Parallel()
3250

3351
tb := newTestBrowser(t, withFileServer())
3452
p := tb.NewPage(nil)
35-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
36-
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
37-
})
3853

54+
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
3955
withPingHandler(t, tb, time.Millisecond*100, nil)
4056
withPingJSHandler(t, tb, false, nil)
4157

@@ -70,10 +86,8 @@ func TestLifecycleWaitForLoadStateDOMContentLoaded(t *testing.T) {
7086

7187
tb := newTestBrowser(t, withFileServer())
7288
p := tb.NewPage(nil)
73-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
74-
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
75-
})
7689

90+
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
7791
withPingHandler(t, tb, time.Millisecond*100, nil)
7892
withPingJSHandler(t, tb, true, nil)
7993

@@ -106,10 +120,8 @@ func TestLifecycleWaitForLoadStateNetworkIdle(t *testing.T) {
106120

107121
tb := newTestBrowser(t, withFileServer())
108122
p := tb.NewPage(nil)
109-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
110-
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
111-
})
112123

124+
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
113125
withPingHandler(t, tb, 0, nil)
114126
withPingJSHandler(t, tb, false, nil)
115127

@@ -141,10 +153,8 @@ func TestLifecycleWaitForLoadStateDOMContentLoadedThenNetworkIdle(t *testing.T)
141153

142154
tb := newTestBrowser(t, withFileServer())
143155
p := tb.NewPage(nil)
144-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
145-
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
146-
})
147156

157+
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
148158
withPingHandler(t, tb, time.Millisecond*100, nil)
149159
withPingJSHandler(t, tb, false, nil)
150160

@@ -164,10 +174,8 @@ func TestLifecycleReloadLoad(t *testing.T) {
164174

165175
tb := newTestBrowser(t, withFileServer())
166176
p := tb.NewPage(nil)
167-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
168-
http.Redirect(w, r, tb.staticURL("reload_lifecycle.html"), http.StatusMovedPermanently)
169-
})
170177

178+
withHomeHandler(t, tb, "reload_lifecycle.html")
171179
withPingHandler(t, tb, time.Millisecond*100, nil)
172180
withPingJSHandler(t, tb, false, nil)
173181

@@ -198,10 +206,8 @@ func TestLifecycleReloadDOMContentLoaded(t *testing.T) {
198206

199207
tb := newTestBrowser(t, withFileServer())
200208
p := tb.NewPage(nil)
201-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
202-
http.Redirect(w, r, tb.staticURL("reload_lifecycle.html"), http.StatusMovedPermanently)
203-
})
204209

210+
withHomeHandler(t, tb, "reload_lifecycle.html")
205211
withPingHandler(t, tb, time.Millisecond*100, nil)
206212
withPingJSHandler(t, tb, true, nil)
207213

@@ -232,10 +238,8 @@ func TestLifecycleReloadNetworkIdle(t *testing.T) {
232238

233239
tb := newTestBrowser(t, withFileServer())
234240
p := tb.NewPage(nil)
235-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
236-
http.Redirect(w, r, tb.staticURL("reload_lifecycle.html"), http.StatusMovedPermanently)
237-
})
238241

242+
withHomeHandler(t, tb, "reload_lifecycle.html")
239243
withPingHandler(t, tb, 0, nil)
240244
withPingJSHandler(t, tb, false, nil)
241245

@@ -294,10 +298,8 @@ func TestLifecycleNetworkIdle(t *testing.T) {
294298

295299
tb := newTestBrowser(t, withFileServer())
296300
p := tb.NewPage(nil)
297-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
298-
http.Redirect(w, r, tb.staticURL("prolonged_network_idle.html"), http.StatusMovedPermanently)
299-
})
300301

302+
withHomeHandler(t, tb, "prolonged_network_idle.html")
301303
ch := make(chan bool)
302304
withPingHandler(t, tb, time.Millisecond*50, ch)
303305
withPingJSHandler(t, tb, false, ch)
@@ -316,10 +318,8 @@ func TestLifecycleNetworkIdle(t *testing.T) {
316318

317319
tb := newTestBrowser(t, withFileServer())
318320
p := tb.NewPage(nil)
319-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
320-
http.Redirect(w, r, tb.staticURL("prolonged_network_idle_10.html"), http.StatusMovedPermanently)
321-
})
322321

322+
withHomeHandler(t, tb, "prolonged_network_idle_10.html")
323323
withPingHandler(t, tb, time.Millisecond*50, nil)
324324

325325
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() {
@@ -329,6 +329,14 @@ func TestLifecycleNetworkIdle(t *testing.T) {
329329
})
330330
}
331331

332+
func withHomeHandler(t *testing.T, tb *testBrowser, htmlFile string) {
333+
t.Helper()
334+
335+
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
336+
http.Redirect(w, r, tb.staticURL(htmlFile), http.StatusMovedPermanently)
337+
})
338+
}
339+
332340
func withPingHandler(t *testing.T, tb *testBrowser, slow time.Duration, ch chan bool) {
333341
t.Helper()
334342

0 commit comments

Comments
 (0)