@@ -7,12 +7,163 @@ import (
7
7
"testing"
8
8
"time"
9
9
10
- "github.com/grafana/xk6-browser/api"
11
- "github.com/grafana/xk6-browser/common"
12
10
"github.com/stretchr/testify/assert"
13
11
"github.com/stretchr/testify/require"
12
+
13
+ "github.com/grafana/xk6-browser/api"
14
+ "github.com/grafana/xk6-browser/common"
14
15
)
15
16
17
+ func TestLifecycleReloadLoad (t * testing.T ) {
18
+ t .Parallel ()
19
+
20
+ tb := newTestBrowser (t , withFileServer ())
21
+ p := tb .NewPage (nil )
22
+ tb .withHandler ("/home" , func (w http.ResponseWriter , r * http.Request ) {
23
+ http .Redirect (w , r , tb .staticURL ("reload_lifecycle.html" ), http .StatusMovedPermanently )
24
+ })
25
+
26
+ var counter int64
27
+ var counterMu sync.Mutex
28
+ tb .withHandler ("/ping" , func (w http.ResponseWriter , _ * http.Request ) {
29
+ counterMu .Lock ()
30
+ defer counterMu .Unlock ()
31
+
32
+ time .Sleep (time .Millisecond * 100 )
33
+
34
+ counter ++
35
+ fmt .Fprintf (w , "pong %d" , counter )
36
+ })
37
+
38
+ tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
39
+ fmt .Fprintf (w , `
40
+ var pingJSTextOutput = document.getElementById("pingJSText");
41
+ pingJSTextOutput.innerText = "ping.js loaded from server";
42
+ ` )
43
+ })
44
+
45
+ waitUntil := common .LifecycleEventLoad
46
+ assertHome (t , tb , p , waitUntil , func () {
47
+ result := p .TextContent ("#pingRequestText" , nil )
48
+ assert .NotEqualValues (t , "Waiting... pong 10 - for loop complete" , result )
49
+
50
+ result = p .TextContent ("#pingJSText" , nil )
51
+ assert .EqualValues (t , "ping.js loaded from server" , result )
52
+
53
+ opts := tb .toGojaValue (common.PageReloadOptions {
54
+ WaitUntil : waitUntil ,
55
+ Timeout : 30 * time .Second ,
56
+ })
57
+ p .Reload (opts )
58
+
59
+ result = p .TextContent ("#pingRequestText" , nil )
60
+ assert .NotEqualValues (t , "Waiting... pong 20 - for loop complete" , result )
61
+
62
+ result = p .TextContent ("#pingJSText" , nil )
63
+ assert .EqualValues (t , "ping.js loaded from server" , result )
64
+ })
65
+ }
66
+
67
+ func TestLifecycleReloadDOMContentLoaded (t * testing.T ) {
68
+ t .Parallel ()
69
+
70
+ tb := newTestBrowser (t , withFileServer ())
71
+ p := tb .NewPage (nil )
72
+ tb .withHandler ("/home" , func (w http.ResponseWriter , r * http.Request ) {
73
+ http .Redirect (w , r , tb .staticURL ("reload_lifecycle.html" ), http .StatusMovedPermanently )
74
+ })
75
+
76
+ var counter int64
77
+ var counterMu sync.Mutex
78
+ tb .withHandler ("/ping" , func (w http.ResponseWriter , _ * http.Request ) {
79
+ counterMu .Lock ()
80
+ defer counterMu .Unlock ()
81
+
82
+ time .Sleep (time .Millisecond * 100 )
83
+
84
+ counter ++
85
+ fmt .Fprintf (w , "pong %d" , counter )
86
+ })
87
+
88
+ tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
89
+ fmt .Fprintf (w , `
90
+ await new Promise(resolve => setTimeout(resolve, 1000));
91
+
92
+ var pingJSTextOutput = document.getElementById("pingJSText");
93
+ pingJSTextOutput.innerText = "ping.js loaded from server";
94
+ ` )
95
+ })
96
+
97
+ waitUntil := common .LifecycleEventDOMContentLoad
98
+ assertHome (t , tb , p , waitUntil , func () {
99
+ result := p .TextContent ("#pingRequestText" , nil )
100
+ assert .NotEqualValues (t , "Waiting... pong 10 - for loop complete" , result )
101
+
102
+ result = p .TextContent ("#pingJSText" , nil )
103
+ assert .EqualValues (t , "Waiting..." , result )
104
+
105
+ opts := tb .toGojaValue (common.PageReloadOptions {
106
+ WaitUntil : waitUntil ,
107
+ Timeout : 30 * time .Second ,
108
+ })
109
+ p .Reload (opts )
110
+
111
+ result = p .TextContent ("#pingRequestText" , nil )
112
+ assert .NotEqualValues (t , "Waiting... pong 20 - for loop complete" , result )
113
+
114
+ result = p .TextContent ("#pingJSText" , nil )
115
+ assert .EqualValues (t , "Waiting..." , result )
116
+ })
117
+ }
118
+
119
+ func TestLifecycleReloadNetworkIdle (t * testing.T ) {
120
+ t .Parallel ()
121
+
122
+ tb := newTestBrowser (t , withFileServer ())
123
+ p := tb .NewPage (nil )
124
+ tb .withHandler ("/home" , func (w http.ResponseWriter , r * http.Request ) {
125
+ http .Redirect (w , r , tb .staticURL ("reload_lifecycle.html" ), http .StatusMovedPermanently )
126
+ })
127
+
128
+ var counter int64
129
+ var counterMu sync.Mutex
130
+ tb .withHandler ("/ping" , func (w http.ResponseWriter , _ * http.Request ) {
131
+ counterMu .Lock ()
132
+ defer counterMu .Unlock ()
133
+
134
+ counter ++
135
+ fmt .Fprintf (w , "pong %d" , counter )
136
+ })
137
+
138
+ tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
139
+ fmt .Fprintf (w , `
140
+ var pingJSTextOutput = document.getElementById("pingJSText");
141
+ pingJSTextOutput.innerText = "ping.js loaded from server";
142
+ ` )
143
+ })
144
+
145
+ waitUntil := common .LifecycleEventNetworkIdle
146
+ assertHome (t , tb , p , waitUntil , func () {
147
+ result := p .TextContent ("#pingRequestText" , nil )
148
+ assert .EqualValues (t , "Waiting... pong 10 - for loop complete" , result )
149
+
150
+ result = p .TextContent ("#pingJSText" , nil )
151
+ assert .EqualValues (t , "ping.js loaded from server" , result )
152
+
153
+ opts := tb .toGojaValue (common.PageReloadOptions {
154
+ WaitUntil : waitUntil ,
155
+ Timeout : 30 * time .Second ,
156
+ })
157
+ p .Reload (opts )
158
+
159
+ result = p .TextContent ("#pingRequestText" , nil )
160
+ assert .EqualValues (t , "Waiting... pong 20 - for loop complete" , result )
161
+
162
+ result = p .TextContent ("#pingJSText" , nil )
163
+ assert .EqualValues (t , "ping.js loaded from server" , result )
164
+ })
165
+ }
166
+
16
167
func TestLifecycleNetworkIdle (t * testing.T ) {
17
168
t .Parallel ()
18
169
@@ -40,7 +191,7 @@ func TestLifecycleNetworkIdle(t *testing.T) {
40
191
` )
41
192
})
42
193
43
- assertHome (t , tb , p , "/home" , common .LifecycleEventNetworkIdle , func () {
194
+ assertHome (t , tb , p , common .LifecycleEventNetworkIdle , func () {
44
195
result := p .TextContent ("#pingJSText" , nil )
45
196
assert .EqualValues (t , "ping.js loaded from server" , result )
46
197
})
@@ -57,10 +208,10 @@ func TestLifecycleNetworkIdle(t *testing.T) {
57
208
58
209
var counter int64
59
210
ch := make (chan bool )
211
+ var counterMu sync.Mutex
60
212
tb .withHandler ("/ping" , func (w http.ResponseWriter , _ * http.Request ) {
61
213
<- ch
62
214
63
- var counterMu sync.Mutex
64
215
counterMu .Lock ()
65
216
defer counterMu .Unlock ()
66
217
@@ -78,7 +229,7 @@ func TestLifecycleNetworkIdle(t *testing.T) {
78
229
close (ch )
79
230
})
80
231
81
- assertHome (t , tb , p , "/home" , common .LifecycleEventNetworkIdle , func () {
232
+ assertHome (t , tb , p , common .LifecycleEventNetworkIdle , func () {
82
233
result := p .TextContent ("#pingRequestText" , nil )
83
234
assert .EqualValues (t , "Waiting... pong 4 - for loop complete" , result )
84
235
@@ -108,14 +259,20 @@ func TestLifecycleNetworkIdle(t *testing.T) {
108
259
fmt .Fprintf (w , "pong %d" , counter )
109
260
})
110
261
111
- assertHome (t , tb , p , "/home" , common .LifecycleEventNetworkIdle , func () {
262
+ assertHome (t , tb , p , common .LifecycleEventNetworkIdle , func () {
112
263
result := p .TextContent ("#pingRequestText" , nil )
113
264
assert .EqualValues (t , "Waiting... pong 10 - for loop complete" , result )
114
265
})
115
266
})
116
267
}
117
268
118
- func assertHome (t * testing.T , tb * testBrowser , p api.Page , gotoURL string , waitUntil common.LifecycleEvent , check func ()) {
269
+ func assertHome (
270
+ t * testing.T ,
271
+ tb * testBrowser ,
272
+ p api.Page ,
273
+ waitUntil common.LifecycleEvent ,
274
+ check func (),
275
+ ) {
119
276
t .Helper ()
120
277
121
278
var resolved , rejected bool
@@ -124,7 +281,7 @@ func assertHome(t *testing.T, tb *testBrowser, p api.Page, gotoURL string, waitU
124
281
WaitUntil : waitUntil ,
125
282
Timeout : 30 * time .Second ,
126
283
})
127
- tb .promise (p .Goto (tb .URL (gotoURL ), opts )).then (
284
+ tb .promise (p .Goto (tb .URL ("/home" ), opts )).then (
128
285
func () {
129
286
check ()
130
287
resolved = true
0 commit comments