| 
									
										
										
										
											2020-12-28 14:50:12 -08: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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-11 06:36:15 -08:00
										 |  |  | import fs from 'fs'; | 
					
						
							|  |  |  | import path from 'path'; | 
					
						
							| 
									
										
										
										
											2021-05-08 17:45:04 -07:00
										 |  |  | import { test, expect } from './inspectorTest'; | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | const emptyHTML = new URL('file://' + path.join(__dirname, '..', '..', 'assets', 'empty.html')).toString(); | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 08:07:57 -07:00
										 |  |  | const launchOptions = (channel: string) => { | 
					
						
							| 
									
										
										
										
											2022-07-05 13:30:46 -07:00
										 |  |  |   return channel ? `channel: '${channel}',\n    headless: false` : 'headless: false'; | 
					
						
							| 
									
										
										
										
											2021-03-15 08:07:57 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should print the correct imports and context options', async ({ browserName, channel, runCLI }) => { | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  |   const cli = runCLI(['--target=javascript', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2023-11-13 16:56:27 -08:00
										 |  |  |   const expectedResult = `const { ${browserName} } = require('playwright');
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   const browser = await ${browserName}.launch({ | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |     ${launchOptions(channel)} | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const context = await browser.newContext();`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI }) => { | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  |   const cli = runCLI(['--color-scheme=light', '--target=javascript', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2023-11-13 16:56:27 -08:00
										 |  |  |   const expectedResult = `const { ${browserName} } = require('playwright');
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   const browser = await ${browserName}.launch({ | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |     ${launchOptions(channel)} | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const context = await browser.newContext({ | 
					
						
							|  |  |  |     colorScheme: 'light' | 
					
						
							|  |  |  |   });`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should print the correct context options when using a device', async ({ browserName, channel, runCLI }) => { | 
					
						
							| 
									
										
										
										
											2021-03-11 20:22:50 -08:00
										 |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  |   const cli = runCLI(['--device=Pixel 2', '--target=javascript', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2023-11-13 16:56:27 -08:00
										 |  |  |   const expectedResult = `const { chromium, devices } = require('playwright');
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							|  |  |  |   const browser = await chromium.launch({ | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |     ${launchOptions(channel)} | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const context = await browser.newContext({ | 
					
						
							|  |  |  |     ...devices['Pixel 2'], | 
					
						
							|  |  |  |   });`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should print the correct context options when using a device and additional options', async ({ browserName, channel, runCLI }) => { | 
					
						
							| 
									
										
										
										
											2021-03-11 20:22:50 -08:00
										 |  |  |   test.skip(browserName !== 'webkit'); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  |   const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=javascript', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2023-11-13 16:56:27 -08:00
										 |  |  |   const expectedResult = `const { webkit, devices } = require('playwright');
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   const browser = await webkit.launch({ | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |     ${launchOptions(channel)} | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const context = await browser.newContext({ | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |     ...devices['iPhone 11'], | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |     colorScheme: 'light' | 
					
						
							|  |  |  |   });`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should save the codegen output to a file if specified', async ({ browserName, channel, runCLI }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   const tmpFile = testInfo.outputPath('script.js'); | 
					
						
							| 
									
										
										
										
											2023-04-12 16:37:24 +00:00
										 |  |  |   const cli = runCLI(['--output', tmpFile, '--target=javascript', emptyHTML], { | 
					
						
							|  |  |  |     autoExitWhen: 'await page.goto', // We have to wait for the initial navigation to be recorded.
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await cli.waitForCleanExit(); | 
					
						
							| 
									
										
										
										
											2021-01-13 12:52:03 -08:00
										 |  |  |   const content = fs.readFileSync(tmpFile); | 
					
						
							| 
									
										
										
										
											2023-11-13 16:56:27 -08:00
										 |  |  |   expect(content.toString()).toBe(`const { ${browserName} } = require('playwright');
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   const browser = await ${browserName}.launch({ | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |     ${launchOptions(channel)} | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto('${emptyHTML}'); | 
					
						
							|  |  |  |   await page.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // ---------------------
 | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  | })();`);
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should print load/save storageState', async ({ browserName, channel, runCLI }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   const loadFileName = testInfo.outputPath('load.json'); | 
					
						
							|  |  |  |   const saveFileName = testInfo.outputPath('save.json'); | 
					
						
							|  |  |  |   await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8'); | 
					
						
							| 
									
										
										
										
											2021-06-10 16:52:59 -07:00
										 |  |  |   const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=javascript', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2023-11-13 16:56:27 -08:00
										 |  |  |   const expectedResult1 = `const { ${browserName} } = require('playwright');
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   const browser = await ${browserName}.launch({ | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |     ${launchOptions(channel)} | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const context = await browser.newContext({ | 
					
						
							| 
									
										
										
										
											2021-08-23 22:38:50 -04:00
										 |  |  |     storageState: '${loadFileName.replace(/\\/g, '\\\\')}' | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   });`;
 | 
					
						
							|  |  |  |   await cli.waitFor(expectedResult1); | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   const expectedResult2 = `
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   // ---------------------
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:38:50 -04:00
										 |  |  |   await context.storageState({ path: '${saveFileName.replace(/\\/g, '\\\\')}' }); | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   await context.close(); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  | })();`;
 | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   await cli.waitFor(expectedResult2); | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | }); |