| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  | const path = require('path'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  | const { makeUserDataDir, removeUserDataDir } = require('./utils'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 13:20:13 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @type {TestSuite} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  | module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, browserType, playwrightPath, product, CHROMIUM, FFOX, WEBKIT, WIN}) { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   const {describe, xdescribe, fdescribe} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-12-19 15:47:35 -08:00
										 |  |  |   const {it, fit, xit, dit} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Playwright', function() { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |     describe('browserType.launch', function() { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       it('should reject all promises when browser is closed', async() => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const browser = await browserType.launch(defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |         const page = await (await browser.newContext()).newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         let error = null; | 
					
						
							|  |  |  |         const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e); | 
					
						
							|  |  |  |         await browser.close(); | 
					
						
							|  |  |  |         await neverResolves; | 
					
						
							|  |  |  |         expect(error.message).toContain('Protocol error'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-02-12 19:32:23 -08:00
										 |  |  |       it('should throw if userDataDir option is passed', async() => { | 
					
						
							|  |  |  |         let waitError = null; | 
					
						
							|  |  |  |         const options = Object.assign({}, defaultBrowserOptions, {userDataDir: 'random-path'}); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         await browserType.launch(options).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2020-03-12 01:10:48 +00:00
										 |  |  |         expect(waitError.message).toContain('launchPersistentContext'); | 
					
						
							| 
									
										
										
										
											2020-02-12 19:32:23 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-02-27 14:09:24 -08:00
										 |  |  |       it('should throw if page argument is passed', async() => { | 
					
						
							|  |  |  |         let waitError = null; | 
					
						
							|  |  |  |         const options = Object.assign({}, defaultBrowserOptions, { args: ['http://example.com'] }); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         await browserType.launch(options).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2020-02-27 14:09:24 -08:00
										 |  |  |         expect(waitError.message).toContain('can not specify page'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       it('should reject if executable path is invalid', async({server}) => { | 
					
						
							|  |  |  |         let waitError = null; | 
					
						
							|  |  |  |         const options = Object.assign({}, defaultBrowserOptions, {executablePath: 'random-invalid-path'}); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         await browserType.launch(options).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(waitError.message).toContain('Failed to launch'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |     describe('browserType.launchPersistentContext', function() { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:20:13 -08:00
										 |  |  |       it('should have default URL when launching browser', async function() { | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |         const userDataDir = await makeUserDataDir(); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const browserContext = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-03-13 11:33:33 -07:00
										 |  |  |         const urls = browserContext.pages().map(page => page.url()); | 
					
						
							|  |  |  |         expect(urls).toEqual(['about:blank']); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |         await browserContext.close(); | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |         await removeUserDataDir(userDataDir); | 
					
						
							| 
									
										
										
										
											2019-12-10 14:02:48 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       it('should have custom URL when launching browser', async function({server}) { | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |         const userDataDir = await makeUserDataDir(); | 
					
						
							| 
									
										
										
										
											2019-12-10 14:02:48 -08:00
										 |  |  |         const options = Object.assign({}, defaultBrowserOptions); | 
					
						
							|  |  |  |         options.args = [server.EMPTY_PAGE].concat(options.args || []); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const browserContext = await browserType.launchPersistentContext(userDataDir, options); | 
					
						
							| 
									
										
										
										
											2020-03-13 11:33:33 -07:00
										 |  |  |         const pages = browserContext.pages(); | 
					
						
							| 
									
										
										
										
											2019-12-10 14:02:48 -08:00
										 |  |  |         expect(pages.length).toBe(1); | 
					
						
							|  |  |  |         const page = pages[0]; | 
					
						
							| 
									
										
										
										
											2019-12-17 17:33:06 -08:00
										 |  |  |         if (page.url() !== server.EMPTY_PAGE) { | 
					
						
							| 
									
										
										
										
											2019-12-10 14:02:48 -08:00
										 |  |  |           await page.waitForNavigation(); | 
					
						
							| 
									
										
										
										
											2019-12-17 17:33:06 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-10 14:02:48 -08:00
										 |  |  |         expect(page.url()).toBe(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |         await browserContext.close(); | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |         await removeUserDataDir(userDataDir); | 
					
						
							| 
									
										
										
										
											2019-12-10 14:02:48 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |     describe('browserType.launchServer', function() { | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  |       it('should return child_process instance', async () => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |         expect(browserServer.process().pid).toBeGreaterThan(0); | 
					
						
							|  |  |  |         await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       it('should fire close event', async () => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |         await Promise.all([ | 
					
						
							|  |  |  |           utils.waitEvent(browserServer, 'close'), | 
					
						
							|  |  |  |           browserServer.close(), | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |     describe('browserType.executablePath', function() { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       it('should work', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const executablePath = browserType.executablePath(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(fs.existsSync(executablePath)).toBe(true); | 
					
						
							|  |  |  |         expect(fs.realpathSync(executablePath)).toBe(executablePath); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |     describe('browserType.name', function() { | 
					
						
							| 
									
										
										
										
											2020-01-28 18:09:07 -08:00
										 |  |  |       it('should work', async({server}) => { | 
					
						
							|  |  |  |         if (WEBKIT) | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |           expect(browserType.name()).toBe('webkit'); | 
					
						
							| 
									
										
										
										
											2020-01-28 18:09:07 -08:00
										 |  |  |         else if (FFOX) | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |           expect(browserType.name()).toBe('firefox'); | 
					
						
							| 
									
										
										
										
											2020-01-28 18:09:07 -08:00
										 |  |  |         else if (CHROMIUM) | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |           expect(browserType.name()).toBe('chromium'); | 
					
						
							| 
									
										
										
										
											2020-01-28 18:09:07 -08:00
										 |  |  |         else | 
					
						
							|  |  |  |           throw new Error('Unknown browser'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 17:17:53 -07:00
										 |  |  |   describe('Top-level requires', function() { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     it('should require top-level Errors', async() => { | 
					
						
							| 
									
										
										
										
											2020-01-13 17:36:46 -08:00
										 |  |  |       const Errors = require(path.join(utils.projectRoot(), '/lib/errors.js')); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       expect(Errors.TimeoutError).toBe(playwright.errors.TimeoutError); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     it('should require top-level DeviceDescriptors', async() => { | 
					
						
							| 
									
										
										
										
											2020-01-13 17:36:46 -08:00
										 |  |  |       const Devices = require(path.join(utils.projectRoot(), '/lib/deviceDescriptors.js')).DeviceDescriptors; | 
					
						
							| 
									
										
										
										
											2020-01-27 17:21:39 -08:00
										 |  |  |       expect(Devices['iPhone 6']).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       expect(Devices['iPhone 6']).toBe(playwright.devices['iPhone 6']); | 
					
						
							| 
									
										
										
										
											2020-01-27 17:21:39 -08:00
										 |  |  |       expect(Devices['iPhone 6']).toBe(require(playwrightPath).devices['iPhone 6']); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('Browser.isConnected', () => { | 
					
						
							|  |  |  |     it('should set the browser connected state', async () => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer({...defaultBrowserOptions }); | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       expect(remote.isConnected()).toBe(true); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  |       await remote.close(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       expect(remote.isConnected()).toBe(false); | 
					
						
							| 
									
										
										
										
											2020-03-30 13:49:52 -07:00
										 |  |  |       await browserServer._checkLeaks(); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-23 10:36:13 -08:00
										 |  |  |     it('should throw when used after isConnected returns false', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer({...defaultBrowserOptions }); | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page = await remote.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 10:36:13 -08:00
										 |  |  |       await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |         browserServer.close(), | 
					
						
							| 
									
										
										
										
											2020-01-23 10:36:13 -08:00
										 |  |  |         new Promise(f => remote.once('disconnected', f)), | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       expect(remote.isConnected()).toBe(false); | 
					
						
							|  |  |  |       const error = await page.evaluate('1 + 1').catch(e => e); | 
					
						
							|  |  |  |       expect(error.message).toContain('has been closed'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Browser.disconnect', function() { | 
					
						
							|  |  |  |     it('should reject navigation when browser closes', async({server}) => { | 
					
						
							|  |  |  |       server.setRoute('/one-style.css', () => {}); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer({...defaultBrowserOptions }); | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page = await remote.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       const navigationPromise = page.goto(server.PREFIX + '/one-style.html', {timeout: 60000}).catch(e => e); | 
					
						
							|  |  |  |       await server.waitForRequest('/one-style.css'); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  |       await remote.close(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       const error = await navigationPromise; | 
					
						
							| 
									
										
										
										
											2020-02-10 18:35:47 -08:00
										 |  |  |       expect(error.message).toContain('Navigation failed because browser has disconnected!'); | 
					
						
							| 
									
										
										
										
											2020-03-30 13:49:52 -07:00
										 |  |  |       await browserServer._checkLeaks(); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-06 15:02:42 -08:00
										 |  |  |     it('should reject waitForSelector when browser closes', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       server.setRoute('/empty.html', () => {}); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer({...defaultBrowserOptions }); | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page = await remote.newPage(); | 
					
						
							| 
									
										
										
										
											2020-03-06 15:02:42 -08:00
										 |  |  |       const watchdog = page.waitForSelector('div', { timeout: 60000 }).catch(e => e); | 
					
						
							| 
									
										
										
										
											2020-02-11 18:52:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 15:02:42 -08:00
										 |  |  |       // Make sure the previous waitForSelector has time to make it to the browser before we disconnect.
 | 
					
						
							|  |  |  |       await page.waitForSelector('body'); | 
					
						
							| 
									
										
										
										
											2020-01-30 14:19:30 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  |       await remote.close(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       const error = await watchdog; | 
					
						
							|  |  |  |       expect(error.message).toContain('Protocol error'); | 
					
						
							| 
									
										
										
										
											2020-03-30 13:49:52 -07:00
										 |  |  |       await browserServer._checkLeaks(); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-23 10:36:13 -08:00
										 |  |  |     it('should throw if used after disconnect', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer({...defaultBrowserOptions }); | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page = await remote.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  |       await remote.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 10:36:13 -08:00
										 |  |  |       const error = await page.evaluate('1 + 1').catch(e => e); | 
					
						
							|  |  |  |       expect(error.message).toContain('has been closed'); | 
					
						
							| 
									
										
										
										
											2020-03-30 13:49:52 -07:00
										 |  |  |       await browserServer._checkLeaks(); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-02-11 10:27:19 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     it('should emit close events on pages and contexts', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer({...defaultBrowserOptions }); | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-11 10:27:19 -08:00
										 |  |  |       const context = await remote.newContext(); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       let pageClosed = false; | 
					
						
							|  |  |  |       page.on('close', e => pageClosed = true); | 
					
						
							| 
									
										
										
										
											2020-02-11 15:19:43 -08:00
										 |  |  |       await Promise.all([ | 
					
						
							|  |  |  |         new Promise(f => context.on('close', f)), | 
					
						
							|  |  |  |         browserServer.close() | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2020-02-11 10:27:19 -08:00
										 |  |  |       expect(pageClosed).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2020-01-23 10:36:13 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Browser.close', function() { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:20:13 -08:00
										 |  |  |     it('should terminate network waiters', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer({...defaultBrowserOptions }); | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const newPage = await remote.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       const results = await Promise.all([ | 
					
						
							|  |  |  |         newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e), | 
					
						
							|  |  |  |         newPage.waitForResponse(server.EMPTY_PAGE).catch(e => e), | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |         browserServer.close() | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       ]); | 
					
						
							|  |  |  |       for (let i = 0; i < 2; i++) { | 
					
						
							|  |  |  |         const message = results[i].message; | 
					
						
							|  |  |  |         expect(message).toContain('Target closed'); | 
					
						
							|  |  |  |         expect(message).not.toContain('Timeout'); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-09 16:53:33 -07:00
										 |  |  |     it('should fire close event for all contexts', async(state, test) => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browser = await browserType.launch(defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-03-05 17:22:57 -08:00
										 |  |  |       const context = await browser.newContext(); | 
					
						
							|  |  |  |       let closed = false; | 
					
						
							|  |  |  |       context.on('close', () => closed = true); | 
					
						
							|  |  |  |       await browser.close(); | 
					
						
							|  |  |  |       expect(closed).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |   describe('browserType.launch |webSocket| option', function() { | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |     it('should support the webSocket option', async() => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							|  |  |  |       const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const browserContext = await browser.newContext(); | 
					
						
							| 
									
										
										
										
											2020-03-13 11:33:33 -07:00
										 |  |  |       expect(browserContext.pages().length).toBe(0); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       expect(browserServer.wsEndpoint()).not.toBe(null); | 
					
						
							|  |  |  |       const page = await browserContext.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |       expect(await page.evaluate('11 * 11')).toBe(121); | 
					
						
							|  |  |  |       await page.close(); | 
					
						
							| 
									
										
										
										
											2020-03-30 13:49:52 -07:00
										 |  |  |       await browser.close(); | 
					
						
							|  |  |  |       await browserServer._checkLeaks(); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     it('should fire "disconnected" when closing with webSocket', async() => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							|  |  |  |       const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |       const disconnectedEventPromise = new Promise(resolve => browser.once('disconnected', resolve)); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       browserServer.kill(); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |       await disconnectedEventPromise; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |   describe('browserType.connect', function() { | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |     it.slow()('should be able to reconnect to a browser', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  |         const browserContext = await browser.newContext(); | 
					
						
							|  |  |  |         const page = await browserContext.newPage(); | 
					
						
							|  |  |  |         await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |         await browser.close(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |         const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  |         const browserContext = await browser.newContext(); | 
					
						
							|  |  |  |         const page = await browserContext.newPage(); | 
					
						
							|  |  |  |         await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |         await browser.close(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-03-30 13:49:52 -07:00
										 |  |  |       await browserServer._checkLeaks(); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |   describe('browserType.launchPersistentContext', function() { | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |     it('userDataDir option', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       const userDataDir = await makeUserDataDir(); | 
					
						
							|  |  |  |       const options = Object.assign(defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserContext = await browserType.launchPersistentContext(userDataDir, options); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       // Open a page to make sure its functional.
 | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0); | 
					
						
							|  |  |  |       // This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       await removeUserDataDir(userDataDir); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-22 22:16:39 -07:00
										 |  |  |     it.slow()('userDataDir option should restore state', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       const userDataDir = await makeUserDataDir(); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserContext = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page = await browserContext.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await page.evaluate(() => localStorage.hey = 'hello'); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserContext2 = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page2 = await browserContext2.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       await page2.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await page2.evaluate(() => localStorage.hey)).toBe('hello'); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext2.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       const userDataDir2 = await makeUserDataDir(); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserContext3 = await browserType.launchPersistentContext(userDataDir2, defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page3 = await browserContext3.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       await page3.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await page3.evaluate(() => localStorage.hey)).not.toBe('hello'); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext3.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       await removeUserDataDir(userDataDir); | 
					
						
							|  |  |  |       await removeUserDataDir(userDataDir2); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-28 18:04:51 -08:00
										 |  |  |     // See https://github.com/microsoft/playwright/issues/717
 | 
					
						
							| 
									
										
										
										
											2020-03-23 14:59:29 -07:00
										 |  |  |     it.slow().fail(WIN && CHROMIUM)('userDataDir option should restore cookies', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       const userDataDir = await makeUserDataDir(); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserContext = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page = await browserContext.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await page.evaluate(() => document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserContext2 = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page2 = await browserContext2.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       await page2.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true'); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext2.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       const userDataDir2 = await makeUserDataDir(); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |       const browserContext3 = await browserType.launchPersistentContext(userDataDir2, defaultBrowserOptions); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       const page3 = await browserContext3.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |       await page3.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await page3.evaluate(() => localStorage.hey)).not.toBe('doSomethingOnlyOnce=true'); | 
					
						
							| 
									
										
										
										
											2020-02-05 12:41:55 -08:00
										 |  |  |       await browserContext3.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       await removeUserDataDir(userDataDir); | 
					
						
							|  |  |  |       await removeUserDataDir(userDataDir2); | 
					
						
							| 
									
										
										
										
											2020-01-23 12:18:41 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | }; |