@@ -14,6 +14,25 @@ import (
14
14
"github.com/grafana/xk6-browser/common"
15
15
)
16
16
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
+
17
36
func TestLifecycleWaitForLoadStateLoad (t * testing.T ) {
18
37
// Test description
19
38
//
@@ -27,15 +46,12 @@ func TestLifecycleWaitForLoadStateLoad(t *testing.T) {
27
46
// (which is when load is fired). We also want
28
47
// to ensure that the load event is stored
29
48
// internally, and we don't block on WaitForLoadState.
30
-
31
49
t .Parallel ()
32
50
33
51
tb := newTestBrowser (t , withFileServer ())
34
52
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
- })
38
53
54
+ withHomeHandler (t , tb , "wait_for_nav_lifecycle.html" )
39
55
withPingHandler (t , tb , time .Millisecond * 100 , nil )
40
56
withPingJSHandler (t , tb , false , nil )
41
57
@@ -70,10 +86,8 @@ func TestLifecycleWaitForLoadStateDOMContentLoaded(t *testing.T) {
70
86
71
87
tb := newTestBrowser (t , withFileServer ())
72
88
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
- })
76
89
90
+ withHomeHandler (t , tb , "wait_for_nav_lifecycle.html" )
77
91
withPingHandler (t , tb , time .Millisecond * 100 , nil )
78
92
withPingJSHandler (t , tb , true , nil )
79
93
@@ -106,10 +120,8 @@ func TestLifecycleWaitForLoadStateNetworkIdle(t *testing.T) {
106
120
107
121
tb := newTestBrowser (t , withFileServer ())
108
122
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
- })
112
123
124
+ withHomeHandler (t , tb , "wait_for_nav_lifecycle.html" )
113
125
withPingHandler (t , tb , 0 , nil )
114
126
withPingJSHandler (t , tb , false , nil )
115
127
@@ -141,10 +153,8 @@ func TestLifecycleWaitForLoadStateDOMContentLoadedThenNetworkIdle(t *testing.T)
141
153
142
154
tb := newTestBrowser (t , withFileServer ())
143
155
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
- })
147
156
157
+ withHomeHandler (t , tb , "wait_for_nav_lifecycle.html" )
148
158
withPingHandler (t , tb , time .Millisecond * 100 , nil )
149
159
withPingJSHandler (t , tb , false , nil )
150
160
@@ -164,10 +174,8 @@ func TestLifecycleReloadLoad(t *testing.T) {
164
174
165
175
tb := newTestBrowser (t , withFileServer ())
166
176
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
- })
170
177
178
+ withHomeHandler (t , tb , "reload_lifecycle.html" )
171
179
withPingHandler (t , tb , time .Millisecond * 100 , nil )
172
180
withPingJSHandler (t , tb , false , nil )
173
181
@@ -198,10 +206,8 @@ func TestLifecycleReloadDOMContentLoaded(t *testing.T) {
198
206
199
207
tb := newTestBrowser (t , withFileServer ())
200
208
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
- })
204
209
210
+ withHomeHandler (t , tb , "reload_lifecycle.html" )
205
211
withPingHandler (t , tb , time .Millisecond * 100 , nil )
206
212
withPingJSHandler (t , tb , true , nil )
207
213
@@ -232,10 +238,8 @@ func TestLifecycleReloadNetworkIdle(t *testing.T) {
232
238
233
239
tb := newTestBrowser (t , withFileServer ())
234
240
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
- })
238
241
242
+ withHomeHandler (t , tb , "reload_lifecycle.html" )
239
243
withPingHandler (t , tb , 0 , nil )
240
244
withPingJSHandler (t , tb , false , nil )
241
245
@@ -294,10 +298,8 @@ func TestLifecycleNetworkIdle(t *testing.T) {
294
298
295
299
tb := newTestBrowser (t , withFileServer ())
296
300
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
- })
300
301
302
+ withHomeHandler (t , tb , "prolonged_network_idle.html" )
301
303
ch := make (chan bool )
302
304
withPingHandler (t , tb , time .Millisecond * 50 , ch )
303
305
withPingJSHandler (t , tb , false , ch )
@@ -316,10 +318,8 @@ func TestLifecycleNetworkIdle(t *testing.T) {
316
318
317
319
tb := newTestBrowser (t , withFileServer ())
318
320
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
- })
322
321
322
+ withHomeHandler (t , tb , "prolonged_network_idle_10.html" )
323
323
withPingHandler (t , tb , time .Millisecond * 50 , nil )
324
324
325
325
assertHome (t , tb , p , common .LifecycleEventNetworkIdle , func () {
@@ -329,6 +329,14 @@ func TestLifecycleNetworkIdle(t *testing.T) {
329
329
})
330
330
}
331
331
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
+
332
340
func withPingHandler (t * testing.T , tb * testBrowser , slow time.Duration , ch chan bool ) {
333
341
t .Helper ()
334
342
0 commit comments