| 
									
										
										
										
											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 path from 'path'; | 
					
						
							|  |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2021-03-15 08:07:57 -07:00
										 |  |  | const launchOptions = (channel: string) => { | 
					
						
							| 
									
										
										
										
											2023-12-15 12:05:13 -08:00
										 |  |  |   return channel ? `Channel = "${channel}",\n    Headless = false,` : `Headless = false,`; | 
					
						
							| 
									
										
										
										
											2021-03-15 08:07:57 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  | function capitalize(browserName: string): string { | 
					
						
							|  |  |  |   return browserName[0].toUpperCase() + browserName.slice(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should print the correct imports and context options', async ({ browserName, channel, runCLI }) => { | 
					
						
							| 
									
										
										
										
											2021-03-15 08:07:57 -07:00
										 |  |  |   const cli = runCLI(['--target=csharp', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-05-25 10:59:17 -07:00
										 |  |  |   const expectedResult = `using Microsoft.Playwright;
 | 
					
						
							|  |  |  | using System; | 
					
						
							|  |  |  | using System.Threading.Tasks; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  | using var playwright = await Playwright.CreateAsync(); | 
					
						
							|  |  |  | await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions | 
					
						
							| 
									
										
										
										
											2021-05-25 10:59:17 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  |     ${launchOptions(channel)} | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | var context = await browser.NewContextAsync();`;
 | 
					
						
							| 
									
										
										
										
											2023-04-12 16:37:24 +00:00
										 |  |  |   await cli.waitFor(expectedResult); | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  | test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI }) => { | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   const cli = runCLI([ | 
					
						
							|  |  |  |     '--color-scheme=dark', | 
					
						
							|  |  |  |     '--geolocation=37.819722,-122.478611', | 
					
						
							|  |  |  |     '--lang=es', | 
					
						
							|  |  |  |     '--proxy-server=http://myproxy:3128', | 
					
						
							|  |  |  |     '--timezone=Europe/Rome', | 
					
						
							|  |  |  |     '--user-agent=hardkodemium', | 
					
						
							|  |  |  |     '--viewport-size=1280,720', | 
					
						
							|  |  |  |     '--target=csharp', | 
					
						
							|  |  |  |     emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-05-20 15:47:14 -07:00
										 |  |  |   const expectedResult = `
 | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  | using var playwright = await Playwright.CreateAsync(); | 
					
						
							|  |  |  | await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ${launchOptions(channel)} | 
					
						
							|  |  |  |     Proxy = new ProxySettings | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Server = "http://myproxy:3128", | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | var context = await browser.NewContextAsync(new BrowserNewContextOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ColorScheme = ColorScheme.Dark, | 
					
						
							|  |  |  |     Geolocation = new Geolocation | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Latitude = 37.819722m, | 
					
						
							|  |  |  |         Longitude = -122.478611m, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     Locale = "es", | 
					
						
							|  |  |  |     Permissions = new[] { ContextPermission.Geolocation }, | 
					
						
							|  |  |  |     TimezoneId = "Europe/Rome", | 
					
						
							|  |  |  |     UserAgent = "hardkodemium", | 
					
						
							|  |  |  |     ViewportSize = new ViewportSize | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Height = 720, | 
					
						
							|  |  |  |         Width = 1280, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | });`;
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   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-03-15 08:07:57 -07:00
										 |  |  |   const cli = runCLI(['--device=Pixel 2', '--target=csharp', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-05-20 15:47:14 -07:00
										 |  |  |   const expectedResult = `
 | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  | using var playwright = await Playwright.CreateAsync(); | 
					
						
							|  |  |  | await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ${launchOptions(channel)} | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | var context = await browser.NewContextAsync(playwright.Devices["Pixel 2"]);`;
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   const cli = runCLI([ | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |     '--device=iPhone 11', | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |     '--color-scheme=dark', | 
					
						
							|  |  |  |     '--geolocation=37.819722,-122.478611', | 
					
						
							|  |  |  |     '--lang=es', | 
					
						
							|  |  |  |     '--proxy-server=http://myproxy:3128', | 
					
						
							|  |  |  |     '--timezone=Europe/Rome', | 
					
						
							|  |  |  |     '--user-agent=hardkodemium', | 
					
						
							|  |  |  |     '--viewport-size=1280,720', | 
					
						
							|  |  |  |     '--target=csharp', | 
					
						
							|  |  |  |     emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-05-20 15:47:14 -07:00
										 |  |  |   const expectedResult = `
 | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  | using var playwright = await Playwright.CreateAsync(); | 
					
						
							|  |  |  | await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ${launchOptions(channel)} | 
					
						
							|  |  |  |     Proxy = new ProxySettings | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Server = "http://myproxy:3128", | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | var context = await browser.NewContextAsync(new BrowserNewContextOptions(playwright.Devices["iPhone 11"]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ColorScheme = ColorScheme.Dark, | 
					
						
							|  |  |  |     Geolocation = new Geolocation | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Latitude = 37.819722m, | 
					
						
							|  |  |  |         Longitude = -122.478611m, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     Locale = "es", | 
					
						
							|  |  |  |     Permissions = new[] { ContextPermission.Geolocation }, | 
					
						
							|  |  |  |     TimezoneId = "Europe/Rome", | 
					
						
							|  |  |  |     UserAgent = "hardkodemium", | 
					
						
							|  |  |  |     ViewportSize = new ViewportSize | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Height = 720, | 
					
						
							|  |  |  |         Width = 1280, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | });`;
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  |   await cli.waitFor(expectedResult); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-03-15 08:07:57 -07:00
										 |  |  |   const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=csharp', emptyHTML]); | 
					
						
							| 
									
										
										
										
											2021-05-20 15:47:14 -07:00
										 |  |  |   const expectedResult1 = `
 | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  | using var playwright = await Playwright.CreateAsync(); | 
					
						
							|  |  |  | await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ${launchOptions(channel)} | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | var context = await browser.NewContextAsync(new BrowserNewContextOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     StorageStatePath = "${loadFileName.replace(/\\/g, '\\\\')}", | 
					
						
							|  |  |  | });`;
 | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   await cli.waitFor(expectedResult1); | 
					
						
							|  |  |  |   const expectedResult2 = `
 | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  | await context.StorageStateAsync(new BrowserContextStorageStateOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Path = "${saveFileName.replace(/\\/g, '\\\\')}" | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | `;
 | 
					
						
							| 
									
										
										
										
											2021-02-11 17:46:54 -08:00
										 |  |  |   await cli.waitFor(expectedResult2); | 
					
						
							| 
									
										
										
										
											2020-12-28 14:50:12 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-07-05 13:30:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('should work with --save-har', async ({ runCLI }, testInfo) => { | 
					
						
							|  |  |  |   const harFileName = testInfo.outputPath('har.har'); | 
					
						
							|  |  |  |   const expectedResult = `
 | 
					
						
							| 
									
										
										
										
											2023-12-15 10:24:26 -08:00
										 |  |  | var context = await browser.NewContextAsync(new BrowserNewContextOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     RecordHarMode = HarMode.Minimal, | 
					
						
							|  |  |  |     RecordHarPath = ${JSON.stringify(harFileName)}, | 
					
						
							|  |  |  |     ServiceWorkers = ServiceWorkerPolicy.Block, | 
					
						
							|  |  |  | });`;
 | 
					
						
							| 
									
										
										
										
											2023-04-12 16:37:24 +00:00
										 |  |  |   const cli = runCLI(['--target=csharp', `--save-har=${harFileName}`], { | 
					
						
							|  |  |  |     autoExitWhen: expectedResult, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await cli.waitForCleanExit(); | 
					
						
							| 
									
										
										
										
											2022-07-05 13:30:46 -07:00
										 |  |  |   const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8')); | 
					
						
							|  |  |  |   expect(json.log.creator.name).toBe('Playwright'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-08-25 11:58:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | for (const testFramework of ['nunit', 'mstest'] as const) { | 
					
						
							|  |  |  |   test(`should not print context options method override in ${testFramework} if no options were passed`, async ({ runCLI }) => { | 
					
						
							|  |  |  |     const cli = runCLI([`--target=csharp-${testFramework}`, emptyHTML]); | 
					
						
							|  |  |  |     await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`); | 
					
						
							|  |  |  |     expect(cli.text()).not.toContain('public override BrowserNewContextOptions ContextOptions()'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test(`should print context options method override in ${testFramework} if options were passed`, async ({ runCLI }) => { | 
					
						
							|  |  |  |     const cli = runCLI([`--target=csharp-${testFramework}`, '--color-scheme=dark', emptyHTML]); | 
					
						
							|  |  |  |     await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`); | 
					
						
							|  |  |  |     expect(cli.text()).toContain(`    public override BrowserNewContextOptions ContextOptions()
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new BrowserNewContextOptions | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ColorScheme = ColorScheme.Dark, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | `);
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test(`should print a valid basic program in mstest`, async ({ runCLI }) => { | 
					
						
							|  |  |  |   const cli = runCLI([`--target=csharp-mstest`, '--color-scheme=dark', emptyHTML]); | 
					
						
							|  |  |  |   await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`); | 
					
						
							|  |  |  |   const expected = `using Microsoft.Playwright.MSTest;
 | 
					
						
							|  |  |  | using Microsoft.Playwright; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [TestClass] | 
					
						
							|  |  |  | public class Tests : PageTest | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public override BrowserNewContextOptions ContextOptions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new BrowserNewContextOptions | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ColorScheme = ColorScheme.Dark, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 22:44:38 +02:00
										 |  |  |     [TestMethod] | 
					
						
							| 
									
										
										
										
											2022-08-25 11:58:58 +02:00
										 |  |  |     public async Task MyTest() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         await Page.GotoAsync("${emptyHTML}"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }`;
 | 
					
						
							|  |  |  |   expect(cli.text()).toContain(expected); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test(`should print a valid basic program in nunit`, async ({ runCLI }) => { | 
					
						
							|  |  |  |   const cli = runCLI([`--target=csharp-nunit`, '--color-scheme=dark', emptyHTML]); | 
					
						
							|  |  |  |   await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`); | 
					
						
							|  |  |  |   const expected = `using Microsoft.Playwright.NUnit;
 | 
					
						
							|  |  |  | using Microsoft.Playwright; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [Parallelizable(ParallelScope.Self)] | 
					
						
							| 
									
										
										
										
											2022-09-08 01:44:58 +02:00
										 |  |  | [TestFixture] | 
					
						
							| 
									
										
										
										
											2022-08-25 11:58:58 +02:00
										 |  |  | public class Tests : PageTest | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public override BrowserNewContextOptions ContextOptions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new BrowserNewContextOptions | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ColorScheme = ColorScheme.Dark, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 22:44:38 +02:00
										 |  |  |     [Test] | 
					
						
							| 
									
										
										
										
											2022-08-25 11:58:58 +02:00
										 |  |  |     public async Task MyTest() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         await Page.GotoAsync("${emptyHTML}"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }`;
 | 
					
						
							|  |  |  |   expect(cli.text()).toContain(expected); | 
					
						
							|  |  |  | }); |