| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import fs from 'fs'; | 
					
						
							|  |  |  | import path from 'path'; | 
					
						
							|  |  |  | import { test, expect } from './inspectorTest'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | const emptyHTML = new URL('file://' + path.join(__dirname, '..', '..', 'assets', 'empty.html')).toString(); | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('should print the correct imports and context options', async ({ runCLI }) => { | 
					
						
							|  |  |  |   const cli = runCLI([emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-11-05 19:01:54 -07:00
										 |  |  |   const expectedResult = `import { test, expect } from '@playwright/test';
 | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('test', async ({ page }) => { | 
					
						
							|  |  |  | });`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  |   expect(cli.text()).toContain(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI }) => { | 
					
						
							|  |  |  |   const cli = runCLI(['--color-scheme=light', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-11-05 19:01:54 -07:00
										 |  |  |   const expectedResult = `import { test, expect } from '@playwright/test';
 | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test.use({ | 
					
						
							|  |  |  |   colorScheme: 'light' | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('test', async ({ page }) => {`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  |   expect(cli.text()).toContain(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('should print the correct context options when using a device', async ({ browserName, channel, runCLI }) => { | 
					
						
							|  |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const cli = runCLI(['--device=Pixel 2', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-11-05 19:01:54 -07:00
										 |  |  |   const expectedResult = `import { test, expect, devices } from '@playwright/test';
 | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test.use({ | 
					
						
							|  |  |  |   ...devices['Pixel 2'], | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('test', async ({ page }) => {`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  |   expect(cli.text()).toContain(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('should print the correct context options when using a device and additional options', async ({ browserName, channel, runCLI }) => { | 
					
						
							|  |  |  |   test.skip(browserName !== 'webkit'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-11-05 19:01:54 -07:00
										 |  |  |   const expectedResult = `import { test, expect, devices } from '@playwright/test';
 | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test.use({ | 
					
						
							|  |  |  |   ...devices['iPhone 11'], | 
					
						
							|  |  |  |   colorScheme: 'light' | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('test', async ({ page }) => {`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  |   expect(cli.text()).toContain(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('should print load storageState', async ({ browserName, channel, runCLI }, testInfo) => { | 
					
						
							|  |  |  |   const loadFileName = testInfo.outputPath('load.json'); | 
					
						
							|  |  |  |   await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8'); | 
					
						
							|  |  |  |   const cli = runCLI([`--load-storage=${loadFileName}`, emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-11-05 19:01:54 -07:00
										 |  |  |   const expectedResult = `import { test, expect } from '@playwright/test';
 | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test.use({ | 
					
						
							| 
									
										
										
										
											2021-08-23 22:38:50 -04:00
										 |  |  |   storageState: '${loadFileName.replace(/\\/g, '\\\\')}' | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('test', async ({ page }) => {`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-07-05 13:30:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('should work with --save-har', async ({ runCLI }, testInfo) => { | 
					
						
							|  |  |  |   const harFileName = testInfo.outputPath('har.har'); | 
					
						
							| 
									
										
										
										
											2022-10-25 12:55:20 -04:00
										 |  |  |   const cli = runCLI(['--target=playwright-test', `--save-har=${harFileName}`]); | 
					
						
							| 
									
										
										
										
											2022-07-05 13:30:46 -07:00
										 |  |  |   const expectedResult = `
 | 
					
						
							|  |  |  |   recordHar: { | 
					
						
							|  |  |  |     mode: 'minimal', | 
					
						
							|  |  |  |     path: '${harFileName.replace(/\\/g, '\\\\')}' | 
					
						
							|  |  |  |   }`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult).catch(e => e); | 
					
						
							|  |  |  |   expect(cli.text()).toContain(expectedResult); | 
					
						
							|  |  |  |   await cli.exited; | 
					
						
							|  |  |  |   const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8')); | 
					
						
							|  |  |  |   expect(json.log.creator.name).toBe('Playwright'); | 
					
						
							|  |  |  | }); |