@@ -183,4 +183,126 @@ describe('Launcher', () => {
183
183
const chromeInstance = new Launcher ( { chromePath : '' } ) ;
184
184
chromeInstance . launch ( ) . catch ( ( ) => done ( ) ) ;
185
185
} ) ;
186
+
187
+ describe ( 'waitForTarget method' , ( ) => {
188
+ function getChromeInstance ( targetList : unknown , waitForInspectableTarget ?: number ) {
189
+ const chromeInstance = new Launcher ( { waitForInspectableTarget : typeof waitForInspectableTarget === 'number' ? waitForInspectableTarget : 100 } ) ;
190
+ const getTargetListStub = stub ( chromeInstance , 'getTargetList' ) . returns ( Promise . resolve ( targetList ) ) ;
191
+
192
+ return {
193
+ chromeInstance,
194
+ getTargetListStub
195
+ } ;
196
+ }
197
+
198
+ it ( 'returns promise that is resolved with the same value as promise returned by getTargetList method' , ( ) => {
199
+ const response = {
200
+ body : [ 'test' , 'list' ]
201
+ } ;
202
+ const { chromeInstance, getTargetListStub} = getChromeInstance ( response ) ;
203
+
204
+ return chromeInstance . waitForTarget ( ) . then ( ( result ) => {
205
+ assert . ok ( getTargetListStub . calledOnce ) ;
206
+ assert . strictEqual ( result , response ) ;
207
+ } ) ;
208
+ } ) ;
209
+
210
+ it ( 'returns promise that is resolved with the same value as promise returned by getTargetList method after interval specified in waitForInspectableTarget option' , ( ) => {
211
+ const response = {
212
+ body : [ ]
213
+ } ;
214
+ const waitTime = 90 ;
215
+ const { chromeInstance, getTargetListStub} = getChromeInstance ( response , waitTime ) ;
216
+ chromeInstance . getTargetRetryTimeout = 50 ;
217
+ const startTime = new Date ( ) . getTime ( ) ;
218
+
219
+ return chromeInstance . waitForTarget ( ) . then ( ( result ) => {
220
+ assert . ok ( getTargetListStub . callCount === 3 ) ;
221
+ assert . ok ( new Date ( ) . getTime ( ) - startTime > waitTime ) ;
222
+ assert . strictEqual ( result , response ) ;
223
+ } ) ;
224
+ } ) ;
225
+
226
+ it ( 'returns promise that is rejected with the same value as promise returned by getTargetList method after interval specified in waitForInspectableTarget option' , ( ) => {
227
+ const reason = 'No target' ;
228
+ const waitTime = 100 ;
229
+ const { chromeInstance, getTargetListStub} = getChromeInstance ( Promise . reject ( reason ) , waitTime ) ;
230
+ chromeInstance . getTargetRetryTimeout = 40 ;
231
+ const startTime = new Date ( ) . getTime ( ) ;
232
+
233
+ return chromeInstance . waitForTarget ( ) . catch ( ( result ) => {
234
+ assert . ok ( getTargetListStub . callCount === 4 ) ;
235
+ assert . ok ( new Date ( ) . getTime ( ) - startTime > waitTime ) ;
236
+ assert . strictEqual ( result , reason ) ;
237
+ } ) ;
238
+ } ) ;
239
+ } ) ;
240
+
241
+ describe ( 'waitForInspectableTarget option' , ( ) => {
242
+ function getChromeInstance ( options ?: Options ) {
243
+ const chromeInstance = new Launcher ( options ) ;
244
+ stub ( chromeInstance , 'isDebuggerReady' ) . returns ( Promise . resolve ( ) ) ;
245
+
246
+ return chromeInstance ;
247
+ }
248
+
249
+ it ( 'waitUntilReady does not call waitForTarget method when the option is not set' , ( ) => {
250
+ const chromeInstance = getChromeInstance ( ) ;
251
+ const waitForTargetSpy = spy ( chromeInstance , 'waitForTarget' ) ;
252
+
253
+ return chromeInstance . waitUntilReady ( ) . then ( ( ) => {
254
+ assert . ok ( waitForTargetSpy . notCalled ) ;
255
+ } ) ;
256
+ } ) ;
257
+
258
+ it ( 'waitUntilReady does not call waitForTarget method when 0 is set for the option' , ( ) => {
259
+ const chromeInstance = getChromeInstance ( { waitForInspectableTarget : 0 } ) ;
260
+ const waitForTargetSpy = spy ( chromeInstance , 'waitForTarget' ) ;
261
+
262
+ return chromeInstance . waitUntilReady ( ) . then ( ( ) => {
263
+ assert . ok ( waitForTargetSpy . notCalled ) ;
264
+ } ) ;
265
+ } ) ;
266
+
267
+ it ( 'waitUntilReady does not call waitForTarget method when negative value is set for the option' , ( ) => {
268
+ const chromeInstance = getChromeInstance ( { waitForInspectableTarget : - 1 } ) ;
269
+ const waitForTargetSpy = spy ( chromeInstance , 'waitForTarget' ) ;
270
+
271
+ return chromeInstance . waitUntilReady ( ) . then ( ( ) => {
272
+ assert . ok ( waitForTargetSpy . notCalled ) ;
273
+ } ) ;
274
+ } ) ;
275
+
276
+ it ( 'waitUntilReady calls waitForTarget method when the option is set' , ( ) => {
277
+ const chromeInstance = getChromeInstance ( { waitForInspectableTarget : 1000 } ) ;
278
+ const response = {
279
+ body : [ {
280
+ description : '' ,
281
+ devtoolsFrontendUrl : '/devtools/inspector.html?ws=127.0.0.1:54321/devtools/page/1C2C62A45591F2DECB9CC50E7C3B1FA5' ,
282
+ id : '1C2C62A45591F2DECB9CC50E7C3B1FA5' ,
283
+ title : '' ,
284
+ type : 'page' ,
285
+ url : 'about:blank' ,
286
+ webSocketDebuggerUrl : 'ws://127.0.0.1:54321/devtools/page/1C2C62A45591F2DECB9CC50E7C3B1FA5'
287
+ } ]
288
+ } ;
289
+ const waitForTargetStub = stub ( chromeInstance , 'waitForTarget' ) . returns ( Promise . resolve ( response ) ) ;
290
+
291
+ return chromeInstance . waitUntilReady ( ) . then ( ( result ) => {
292
+ assert . ok ( waitForTargetStub . calledOnce ) ;
293
+ assert . deepEqual ( result , response . body ) ;
294
+ } ) ;
295
+ } ) ;
296
+
297
+ it ( 'waitUntilReady rejects when waitForTarget method returns rejected promise' , ( ) => {
298
+ const chromeInstance = getChromeInstance ( { waitForInspectableTarget : 1 } ) ;
299
+ const reason = 'No targets' ;
300
+ const waitForTargetStub = stub ( chromeInstance , 'waitForTarget' ) . returns ( Promise . reject ( reason ) ) ;
301
+
302
+ return chromeInstance . waitUntilReady ( ) . catch ( ( result ) => {
303
+ assert . ok ( waitForTargetStub . calledOnce ) ;
304
+ assert . strictEqual ( result , reason ) ;
305
+ } ) ;
306
+ } ) ;
307
+ } ) ;
186
308
} ) ;
0 commit comments