| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { playwrightTest as it, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should reject all promises when browser is closed', async ({ browserType }) => { | 
					
						
							|  |  |  |   const browser = await browserType.launch(); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   const page = await (await browser.newContext()).newPage(); | 
					
						
							|  |  |  |   let error = null; | 
					
						
							|  |  |  |   const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e); | 
					
						
							|  |  |  |   await page.evaluate(() => new Promise(f => setTimeout(f, 0))); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  |   await neverResolves; | 
					
						
							| 
									
										
										
										
											2021-08-26 18:44:49 -07:00
										 |  |  |   // WebKit under task-set -c 1 is giving browser, rest are giving target.
 | 
					
						
							|  |  |  |   expect(error.message).toContain(' closed'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should throw if userDataDir option is passed', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   let waitError = null; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   await browserType.launch({ userDataDir: 'random-path' } as any).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2020-09-14 14:43:39 -07:00
										 |  |  |   expect(waitError.message).toContain('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 13:06:05 -08:00
										 |  |  | it('should throw if userDataDir is passed as an argument', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2021-03-15 16:58:46 -07:00
										 |  |  |   let waitError = null; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   await browserType.launch({ args: ['--user-data-dir=random-path', '--profile=random-path'] } as any).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2021-03-15 16:58:46 -07:00
										 |  |  |   expect(waitError.message).toContain('Pass userDataDir parameter to `browserType.launchPersistentContext'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should throw if port option is passed', async ({ browserType }) => { | 
					
						
							|  |  |  |   const error = await browserType.launch({ port: 1234 } as any).catch(e => e); | 
					
						
							| 
									
										
										
										
											2020-09-14 14:43:39 -07:00
										 |  |  |   expect(error.message).toContain('Cannot specify a port without launching as a server.'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should throw if port option is passed for persistent context', async ({ browserType }) => { | 
					
						
							|  |  |  |   const error = await browserType.launchPersistentContext('foo', { port: 1234 } as any).catch(e => e); | 
					
						
							| 
									
										
										
										
											2020-09-14 14:43:39 -07:00
										 |  |  |   expect(error.message).toContain('Cannot specify a port without launching as a server.'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 13:06:05 -08:00
										 |  |  | it('should throw if page argument is passed', async ({ browserType, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   it.skip(browserName === 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   let waitError = null; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   await browserType.launch({ args: ['http://example.com'] }).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   expect(waitError.message).toContain('can not specify page'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 13:06:05 -08:00
										 |  |  | it('should reject if launched browser fails immediately', async ({ mode, browserType, asset }) => { | 
					
						
							| 
									
										
										
										
											2023-02-07 18:25:33 -08:00
										 |  |  |   it.skip(mode === 'service'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   let waitError = null; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   await browserType.launch({ executablePath: asset('dummy_bad_browser_executable.js') }).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   expect(waitError.message).toContain('== logs =='); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 13:06:05 -08:00
										 |  |  | it('should reject if executable path is invalid', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   let waitError = null; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   await browserType.launch({ executablePath: 'random-invalid-path' }).catch(e => waitError = e); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   expect(waitError.message).toContain('Failed to launch'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should handle timeout', async ({ browserType, mode }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   it.skip(mode !== 'default'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const options: any = { timeout: 5000, __testHookBeforeCreateBrowser: () => new Promise(f => setTimeout(f, 6000)) }; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   const error = await browserType.launch(options).catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain(`browserType.launch: Timeout 5000ms exceeded.`); | 
					
						
							| 
									
										
										
										
											2020-08-17 14:12:31 -07:00
										 |  |  |   expect(error.message).toContain(`<launching>`); | 
					
						
							|  |  |  |   expect(error.message).toContain(`<launched> pid=`); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should handle exception', async ({ browserType, mode }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   it.skip(mode !== 'default'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   const e = new Error('Dummy'); | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const options = { __testHookBeforeCreateBrowser: () => { throw e; }, timeout: 9000 }; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   const error = await browserType.launch(options).catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('Dummy'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should report launch log', async ({ browserType, mode }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   it.skip(mode !== 'default'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   const e = new Error('Dummy'); | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const options = { __testHookBeforeCreateBrowser: () => { throw e; }, timeout: 9000 }; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   const error = await browserType.launch(options).catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('<launching>'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 13:06:05 -08:00
										 |  |  | it('should accept objects as options', async ({ mode,   browserType }) => { | 
					
						
							|  |  |  |   it.skip(mode === 'service'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |   // @ts-expect-error process is not a real option.
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const browser = await browserType.launch({ process }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   await browser.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should fire close event for all contexts', async ({ browserType }) => { | 
					
						
							|  |  |  |   const browser = await browserType.launch(); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   let closed = false; | 
					
						
							|  |  |  |   context.on('close', () => closed = true); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  |   expect(closed).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | it('should be callable twice', async ({ browserType }) => { | 
					
						
							|  |  |  |   const browser = await browserType.launch(); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     browser.close(), | 
					
						
							|  |  |  |     browser.close(), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  | }); |