@@ -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 TestLifecycleWaitForNavigation (t * testing.T ) {
18
37
// Test description
19
38
//
@@ -119,24 +138,61 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
119
138
}
120
139
}
121
140
122
- // General guidelines on lifecycle events:
123
- //
124
- // - load: This lifecycle event is emitted by the browser once:
125
- // 1. The HTML is loaded;
126
- // 2. The async scripts have loaded;
127
- // It does not wait for the other network requests to
128
- // complete.
129
- //
130
- // - domcontentloaded: This lifecycle event is emitted by the
131
- // browser once:
132
- // 1. The HTML is loaded;
133
- // It does not wait for the async scripts or
134
- // the other network requests to complete.
135
- //
136
- // - networkidle: This lifecycle event is emitted by the browser once:
137
- // 1. The HTML is loaded;
138
- // 2. The async scripts have loaded;
139
- // 3. All other network requests have completed;
141
+ func TestLifecycleWaitForNavigationTimeout (t * testing.T ) {
142
+ t .Parallel ()
143
+
144
+ // Test description
145
+ //
146
+ // 1. goto /home and wait for the networkidle lifecycle event.
147
+ // 2. use WaitForNavigation with networkidle.
148
+ //
149
+ // Success criteria: Time out reached after navigation completed and
150
+ // wait for lifecycle event set, to signify that
151
+ // WaitForNavigation must be set before we navigate
152
+ // to a new page.
153
+
154
+ tb := newTestBrowser (t , withFileServer ())
155
+ p := tb .NewPage (nil )
156
+
157
+ withHomeHandler (t , tb , "prolonged_network_idle_10.html" )
158
+ withPingHandler (t , tb , 0 , nil )
159
+
160
+ waitUntil := common .LifecycleEventNetworkIdle
161
+ var resolved , rejected bool
162
+ err := tb .await (func () error {
163
+ opts := tb .toGojaValue (common.FrameGotoOptions {
164
+ WaitUntil : waitUntil ,
165
+ Timeout : 30 * time .Second ,
166
+ })
167
+ prm := tb .promise (p .Goto (tb .URL ("/home" ), opts )).then (
168
+ func () testPromise {
169
+ result := p .TextContent ("#pingRequestText" , nil )
170
+ assert .EqualValues (t , "Waiting... pong 10 - for loop complete" , result )
171
+
172
+ waitForNav := p .WaitForNavigation (tb .toGojaValue (& common.FrameWaitForNavigationOptions {
173
+ Timeout : 1000 ,
174
+ WaitUntil : waitUntil ,
175
+ }))
176
+
177
+ return tb .promise (waitForNav )
178
+ },
179
+ )
180
+ prm .then (
181
+ func () {
182
+ resolved = true
183
+ },
184
+ func () {
185
+ rejected = true
186
+ },
187
+ )
188
+
189
+ return nil
190
+ })
191
+ require .NoError (t , err )
192
+
193
+ assert .False (t , resolved )
194
+ assert .True (t , rejected )
195
+ }
140
196
141
197
func TestLifecycleWaitForLoadState (t * testing.T ) {
142
198
t .Parallel ()
0 commit comments