@@ -20,7 +20,7 @@ const utils = require('./utils');
20
20
/**
21
21
* @type {BrowserTestSuite }
22
22
*/
23
- module . exports . describe = function ( { testRunner, expect, playwright, CHROMIUM , WEBKIT } ) {
23
+ module . exports . describe = function ( { testRunner, expect, playwright, CHROMIUM , FFOX , WEBKIT } ) {
24
24
const { describe, xdescribe, fdescribe} = testRunner ;
25
25
const { it, fit, xit, dit} = testRunner ;
26
26
const { beforeAll, beforeEach, afterAll, afterEach} = testRunner ;
@@ -292,4 +292,121 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, WE
292
292
await context . close ( ) ;
293
293
} ) ;
294
294
} ) ;
295
+
296
+ describe ( 'BrowserContext.pages()' , function ( ) {
297
+ it ( 'should return all of the pages' , async ( { browser, server} ) => {
298
+ const context = await browser . newContext ( ) ;
299
+ const page = await context . newPage ( ) ;
300
+ const second = await context . newPage ( ) ;
301
+ const allPages = await context . pages ( ) ;
302
+ expect ( allPages . length ) . toBe ( 2 ) ;
303
+ expect ( allPages ) . toContain ( page ) ;
304
+ expect ( allPages ) . toContain ( second ) ;
305
+ await context . close ( ) ;
306
+ } ) ;
307
+ } ) ;
308
+
309
+ describe ( 'Events.BrowserContext.Page' , function ( ) {
310
+ it ( 'should report when a new page is created and closed' , async ( { browser, server} ) => {
311
+ const context = await browser . newContext ( ) ;
312
+ const page = await context . newPage ( ) ;
313
+ const [ otherPage ] = await Promise . all ( [
314
+ new Promise ( r => context . once ( 'page' , async event => r ( await event . page ( ) ) ) ) ,
315
+ page . evaluate ( url => window . open ( url ) , server . CROSS_PROCESS_PREFIX + '/empty.html' ) . catch ( e => console . log ( 'eee = ' + e ) ) ,
316
+ ] ) ;
317
+ await otherPage . waitForLoadState ( ) ;
318
+ expect ( otherPage . url ( ) ) . toContain ( server . CROSS_PROCESS_PREFIX ) ;
319
+ expect ( await otherPage . evaluate ( ( ) => [ 'Hello' , 'world' ] . join ( ' ' ) ) ) . toBe ( 'Hello world' ) ;
320
+ expect ( await otherPage . $ ( 'body' ) ) . toBeTruthy ( ) ;
321
+
322
+ let allPages = await context . pages ( ) ;
323
+ expect ( allPages ) . toContain ( page ) ;
324
+ expect ( allPages ) . toContain ( otherPage ) ;
325
+
326
+ let closeEventReceived ;
327
+ otherPage . once ( 'close' , ( ) => closeEventReceived = true ) ;
328
+ await otherPage . close ( ) ;
329
+ expect ( closeEventReceived ) . toBeTruthy ( ) ;
330
+
331
+ allPages = await context . pages ( ) ;
332
+ expect ( allPages ) . toContain ( page ) ;
333
+ expect ( allPages ) . not . toContain ( otherPage ) ;
334
+ await context . close ( ) ;
335
+ } ) ;
336
+ it ( 'should not report uninitialized pages' , async ( { browser, server} ) => {
337
+ const context = await browser . newContext ( ) ;
338
+ const pagePromise = new Promise ( fulfill => context . once ( 'page' , async event => fulfill ( await event . page ( ) ) ) ) ;
339
+ context . newPage ( ) ;
340
+ const newPage = await pagePromise ;
341
+ expect ( newPage . url ( ) ) . toBe ( 'about:blank' ) ;
342
+
343
+ const popupPromise = new Promise ( fulfill => context . once ( 'page' , async event => fulfill ( await event . page ( ) ) ) ) ;
344
+ const evaluatePromise = newPage . evaluate ( ( ) => window . open ( 'about:blank' ) ) ;
345
+ const popup = await popupPromise ;
346
+ await popup . waitForLoadState ( ) ;
347
+ expect ( popup . url ( ) ) . toBe ( 'about:blank' ) ;
348
+ await evaluatePromise ;
349
+ await context . close ( ) ;
350
+ } ) ;
351
+ it ( 'should not crash while redirecting if original request was missed' , async ( { browser, server} ) => {
352
+ const context = await browser . newContext ( ) ;
353
+ const page = await context . newPage ( ) ;
354
+ let serverResponse = null ;
355
+ server . setRoute ( '/one-style.css' , ( req , res ) => serverResponse = res ) ;
356
+ // Open a new page. Use window.open to connect to the page later.
357
+ const [ newPage ] = await Promise . all ( [
358
+ new Promise ( fulfill => context . once ( 'page' , async event => fulfill ( await event . page ( ) ) ) ) ,
359
+ page . evaluate ( url => window . open ( url ) , server . PREFIX + '/one-style.html' ) ,
360
+ server . waitForRequest ( '/one-style.css' )
361
+ ] ) ;
362
+ // Issue a redirect.
363
+ serverResponse . writeHead ( 302 , { location : '/injectedstyle.css' } ) ;
364
+ serverResponse . end ( ) ;
365
+ // Wait for the new page to load.
366
+ await newPage . waitForLoadState ( ) ;
367
+ // Connect to the opened page.
368
+ expect ( newPage . url ( ) ) . toBe ( server . PREFIX + '/one-style.html' ) ;
369
+ // Cleanup.
370
+ await context . close ( ) ;
371
+ } ) ;
372
+ it . fail ( WEBKIT ) ( 'should have an opener' , async ( { browser, server} ) => {
373
+ const context = await browser . newContext ( ) ;
374
+ const page = await context . newPage ( ) ;
375
+ await page . goto ( server . EMPTY_PAGE ) ;
376
+ const [ popup ] = await Promise . all ( [
377
+ new Promise ( fulfill => context . once ( 'page' , async event => fulfill ( await event . page ( ) ) ) ) ,
378
+ page . goto ( server . PREFIX + '/popup/window-open.html' )
379
+ ] ) ;
380
+ await popup . waitForLoadState ( ) ;
381
+ expect ( popup . url ( ) ) . toBe ( server . PREFIX + '/popup/popup.html' ) ;
382
+ expect ( await popup . opener ( ) ) . toBe ( page ) ;
383
+ expect ( await page . opener ( ) ) . toBe ( null ) ;
384
+ await context . close ( ) ;
385
+ } ) ;
386
+ it ( 'should close all belonging targets once closing context' , async function ( { browser} ) {
387
+ const context = await browser . newContext ( ) ;
388
+ await context . newPage ( ) ;
389
+ expect ( ( await context . pages ( ) ) . length ) . toBe ( 1 ) ;
390
+
391
+ await context . close ( ) ;
392
+ expect ( ( await context . pages ( ) ) . length ) . toBe ( 0 ) ;
393
+ } ) ;
394
+ it ( 'should fire page lifecycle events' , async function ( { browser, server} ) {
395
+ const context = await browser . newContext ( ) ;
396
+ const events = [ ] ;
397
+ context . on ( 'page' , async event => {
398
+ const page = await event . page ( ) ;
399
+ events . push ( 'CREATED: ' + page . url ( ) ) ;
400
+ page . on ( 'close' , ( ) => events . push ( 'DESTROYED: ' + page . url ( ) ) ) ;
401
+ } ) ;
402
+ const page = await context . newPage ( ) ;
403
+ await page . goto ( server . EMPTY_PAGE ) ;
404
+ await page . close ( ) ;
405
+ expect ( events ) . toEqual ( [
406
+ 'CREATED: about:blank' ,
407
+ `DESTROYED: ${ server . EMPTY_PAGE } `
408
+ ] ) ;
409
+ await context . close ( ) ;
410
+ } ) ;
411
+ } ) ;
295
412
} ;
0 commit comments