Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: make csharp library codegen more csharp like (#28663)

Max Schmitt44c3ad5cebd6
diff --git a/packages/playwright-core/src/server/recorder/csharp.ts b/packages/playwright-core/src/server/recorder/csharp.ts
index edd288d6d8..1dcc6a331a 100644
--- a/packages/playwright-core/src/server/recorder/csharp.ts
+++ b/packages/playwright-core/src/server/recorder/csharp.ts
@@ -53,7 +53,7 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
generateAction(actionInContext: ActionInContext): string {
const action = this._generateActionInner(actionInContext);
if (action)
- return action + '\n';
+ return action;
return '';
}
@@ -64,7 +64,7 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
let pageAlias = actionInContext.frame.pageAlias;
if (this._mode !== 'library')
pageAlias = pageAlias.replace('page', 'Page');
- const formatter = new CSharpFormatter(8);
+ const formatter = new CSharpFormatter(this._mode === 'library' ? 0 : 8);
if (action.name === 'openPage') {
formatter.add(`var ${pageAlias} = await context.NewPageAsync();`);
@@ -184,13 +184,9 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
using System;
using System.Threading.Tasks;
- class Program
- {
- public static async Task Main()
- {
- using var playwright = await Playwright.CreateAsync();
- await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')});
- var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
+ using var playwright = await Playwright.CreateAsync();
+ await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')});
+ var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
formatter.newLine();
return formatter.format();
}
@@ -220,9 +216,11 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
}
generateFooter(saveStorage: string | undefined): string {
- const storageStateLine = saveStorage ? `\n await context.StorageStateAsync(new BrowserContextStorageStateOptions\n {\n Path = ${quote(saveStorage)}\n });\n` : '';
- return `${storageStateLine} }
-}\n`;
+ const offset = this._mode === 'library' ? '' : ' ';
+ let storageStateLine = saveStorage ? `\n${offset}await context.StorageStateAsync(new BrowserContextStorageStateOptions\n${offset}{\n${offset} Path = ${quote(saveStorage)}\n${offset}});\n` : '';
+ if (this._mode !== 'library')
+ storageStateLine += ` }\n}\n`;
+ return storageStateLine;
}
}
diff --git a/tests/library/inspector/cli-codegen-1.spec.ts b/tests/library/inspector/cli-codegen-1.spec.ts
index a3f837fa82..3688bb2053 100644
--- a/tests/library/inspector/cli-codegen-1.spec.ts
+++ b/tests/library/inspector/cli-codegen-1.spec.ts
@@ -46,7 +46,7 @@ test.describe('cli codegen', () => {
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();`);
+await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();`);
expect(message.text()).toBe('click');
});
@@ -144,14 +144,14 @@ test.describe('cli codegen', () => {
.setPosition(250, 250));`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator("canvas").ClickAsync(new LocatorClickOptions
- {
- Position = new Position
- {
- X = 250,
- Y = 250,
- },
- });`);
+await page.Locator("canvas").ClickAsync(new LocatorClickOptions
+{
+ Position = new Position
+ {
+ X = 250,
+ Y = 250,
+ },
+});`);
expect(message.text()).toBe('click 250 250');
});
@@ -188,7 +188,7 @@ test.describe('cli codegen', () => {
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();`);
+await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();`);
expect(message.text()).toBe('click');
});
@@ -249,7 +249,7 @@ test.describe('cli codegen', () => {
await page.locator("#input").fill(\"John\")`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator("#input").FillAsync(\"John\");`);
+await page.Locator("#input").FillAsync(\"John\");`);
expect(message.text()).toBe('John');
});
@@ -283,7 +283,7 @@ test.describe('cli codegen', () => {
await page.locator("#input").fill(\"てすと\")`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator("#input").FillAsync(\"てすと\");`);
+await page.Locator("#input").FillAsync(\"てすと\");`);
expect(message.text()).toBe('てすと');
});
@@ -366,7 +366,7 @@ test.describe('cli codegen', () => {
await page.get_by_role("textbox").press("Shift+Enter")`);
expect(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Textbox).PressAsync("Shift+Enter");`);
+await page.GetByRole(AriaRole.Textbox).PressAsync("Shift+Enter");`);
expect(messages[0].text()).toBe('press');
});
@@ -474,7 +474,7 @@ test.describe('cli codegen', () => {
await page.locator("#checkbox").check()`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator("#checkbox").CheckAsync();`);
+await page.Locator("#checkbox").CheckAsync();`);
expect(message.text()).toBe('true');
});
@@ -544,7 +544,7 @@ test.describe('cli codegen', () => {
await page.locator("#checkbox").uncheck()`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator("#checkbox").UncheckAsync();`);
+await page.Locator("#checkbox").UncheckAsync();`);
expect(message.text()).toBe('false');
});
@@ -576,7 +576,7 @@ test.describe('cli codegen', () => {
await page.locator("#age").select_option("2")`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator("#age").SelectOptionAsync(new[] { "2" });`);
+await page.Locator("#age").SelectOptionAsync(new[] { "2" });`);
expect(message.text()).toBe('2');
});
@@ -617,7 +617,7 @@ test.describe('cli codegen', () => {
await page.locator(\"#age\").select_option(\"2\")`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator(\"#age\").SelectOptionAsync(new[] { \"2\" });`);
+await page.Locator(\"#age\").SelectOptionAsync(new[] { \"2\" });`);
expect(message.text()).toBe('2');
});
@@ -656,10 +656,10 @@ test.describe('cli codegen', () => {
page1 = await page1_info.value`);
expect.soft(sources.get('C#')!.text).toContain(`
- var page1 = await page.RunAndWaitForPopupAsync(async () =>
- {
- await page.GetByRole(AriaRole.Link, new() { Name = "link" }).ClickAsync();
- });`);
+var page1 = await page.RunAndWaitForPopupAsync(async () =>
+{
+ await page.GetByRole(AriaRole.Link, new() { Name = "link" }).ClickAsync();
+});`);
expect(popup.url()).toBe('about:blank');
});
@@ -696,7 +696,7 @@ test.describe('cli codegen', () => {
page.get_by_text("link").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByText("link").ClickAsync();`);
+await page.GetByText("link").ClickAsync();`);
expect(page.url()).toContain('about:blank#foo');
});
@@ -741,9 +741,9 @@ test.describe('cli codegen', () => {
.setButton(MouseButton.MIDDLE));`);
expect(sources.get('C#')!.text).toContain(`
- await page.GetByText("Click me").ClickAsync(new LocatorClickOptions
- {
- Button = MouseButton.Middle,
- });`);
+await page.GetByText("Click me").ClickAsync(new LocatorClickOptions
+{
+ Button = MouseButton.Middle,
+});`);
});
});
diff --git a/tests/library/inspector/cli-codegen-2.spec.ts b/tests/library/inspector/cli-codegen-2.spec.ts
index 1f1b673b36..246a9876d7 100644
--- a/tests/library/inspector/cli-codegen-2.spec.ts
+++ b/tests/library/inspector/cli-codegen-2.spec.ts
@@ -40,7 +40,7 @@ test.describe('cli codegen', () => {
page = await context.new_page()`);
expect(sources.get('C#')!.text).toContain(`
- var page = await context.NewPageAsync();`);
+var page = await context.NewPageAsync();`);
});
test('should contain second page', async ({ openRecorder, page }) => {
@@ -63,7 +63,7 @@ test.describe('cli codegen', () => {
page1 = await context.new_page()`);
expect(sources.get('C#')!.text).toContain(`
- var page1 = await context.NewPageAsync();`);
+var page1 = await context.NewPageAsync();`);
});
test('should contain close page', async ({ openRecorder, page }) => {
@@ -87,7 +87,7 @@ test.describe('cli codegen', () => {
await page.close()`);
expect(sources.get('C#')!.text).toContain(`
- await page.CloseAsync();`);
+await page.CloseAsync();`);
});
test('should not lead to an error if html gets clicked', async ({ page, openRecorder }) => {
@@ -132,7 +132,7 @@ test.describe('cli codegen', () => {
await page.get_by_role("textbox").set_input_files(\"file-to-upload.txt\")`);
expect(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Textbox).SetInputFilesAsync(new[] { \"file-to-upload.txt\" });`);
+await page.GetByRole(AriaRole.Textbox).SetInputFilesAsync(new[] { \"file-to-upload.txt\" });`);
});
test('should upload multiple files', async ({ page, openRecorder, browserName, asset, isLinux }) => {
@@ -163,7 +163,7 @@ test.describe('cli codegen', () => {
await page.get_by_role("textbox").set_input_files([\"file-to-upload.txt\", \"file-to-upload-2.txt\"]`);
expect(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Textbox).SetInputFilesAsync(new[] { \"file-to-upload.txt\", \"file-to-upload-2.txt\" });`);
+await page.GetByRole(AriaRole.Textbox).SetInputFilesAsync(new[] { \"file-to-upload.txt\", \"file-to-upload-2.txt\" });`);
});
test('should clear files', async ({ page, openRecorder, browserName, asset, isLinux }) => {
@@ -194,7 +194,7 @@ test.describe('cli codegen', () => {
await page.get_by_role("textbox").set_input_files([])`);
expect(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Textbox).SetInputFilesAsync(new[] { });`);
+await page.GetByRole(AriaRole.Textbox).SetInputFilesAsync(new[] { });`);
});
test('should download files', async ({ page, openRecorder, server }) => {
@@ -262,15 +262,15 @@ test.describe('cli codegen', () => {
download1 = await download1_info.value`);
expect.soft(sources.get('C#')!.text).toContain(`
- var download = await page.RunAndWaitForDownloadAsync(async () =>
- {
- await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync();
- });`);
+var download = await page.RunAndWaitForDownloadAsync(async () =>
+{
+ await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync();
+});`);
expect.soft(sources.get('C#')!.text).toContain(`
- var download1 = await page.RunAndWaitForDownloadAsync(async () =>
- {
- await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync();
- });`);
+var download1 = await page.RunAndWaitForDownloadAsync(async () =>
+{
+ await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync();
+});`);
});
test('should handle dialogs', async ({ page, openRecorder }) => {
@@ -310,14 +310,14 @@ test.describe('cli codegen', () => {
await page.get_by_role("button", name="click me").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- void page_Dialog_EventHandler(object sender, IDialog dialog)
- {
- Console.WriteLine($\"Dialog message: {dialog.Message}\");
- dialog.DismissAsync();
- page.Dialog -= page_Dialog_EventHandler;
- }
- page.Dialog += page_Dialog_EventHandler;
- await page.GetByRole(AriaRole.Button, new() { Name = "click me" }).ClickAsync();`);
+void page_Dialog_EventHandler(object sender, IDialog dialog)
+{
+ Console.WriteLine($\"Dialog message: {dialog.Message}\");
+ dialog.DismissAsync();
+ page.Dialog -= page_Dialog_EventHandler;
+}
+page.Dialog += page_Dialog_EventHandler;
+await page.GetByRole(AriaRole.Button, new() { Name = "click me" }).ClickAsync();`);
});
@@ -355,8 +355,8 @@ test.describe('cli codegen', () => {
page1 = await context.new_page()
await page1.goto("about:blank?foo")`);
expect(sources.get('C#')!.text).toContain(`
- var page1 = await context.NewPageAsync();
- await page1.GotoAsync("about:blank?foo");`);
+var page1 = await context.NewPageAsync();
+await page1.GotoAsync("about:blank?foo");`);
} else {
expect(sources.get('JavaScript')!.text).toContain(`
const page1Promise = page.waitForEvent('popup');
@@ -541,7 +541,7 @@ test.describe('cli codegen', () => {
await page.locator("#textarea").fill(\"Hello'\\"\`\\nWorld\")`);
expect(sources.get('C#')!.text).toContain(`
- await page.Locator("#textarea").FillAsync(\"Hello'\\"\`\\nWorld\");`);
+await page.Locator("#textarea").FillAsync(\"Hello'\\"\`\\nWorld\");`);
expect(message.text()).toBe('Hello\'\"\`\nWorld');
});
diff --git a/tests/library/inspector/cli-codegen-3.spec.ts b/tests/library/inspector/cli-codegen-3.spec.ts
index 91fae58192..77d91bb0e2 100644
--- a/tests/library/inspector/cli-codegen-3.spec.ts
+++ b/tests/library/inspector/cli-codegen-3.spec.ts
@@ -49,7 +49,7 @@ test.describe('cli codegen', () => {
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).first().click();`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).First.ClickAsync();`);
+await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).First.ClickAsync();`);
expect(message.text()).toBe('click1');
});
@@ -84,7 +84,7 @@ test.describe('cli codegen', () => {
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).nth(1).click();`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).Nth(1).ClickAsync();`);
+await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).Nth(1).ClickAsync();`);
expect(message.text()).toBe('click2');
});
@@ -133,7 +133,7 @@ test.describe('cli codegen', () => {
await page.frame_locator("#frame1").get_by_text("Hello1").click()`);
expect(sources.get('C#')!.text).toContain(`
- await page.FrameLocator("#frame1").GetByText("Hello1").ClickAsync();`);
+await page.FrameLocator("#frame1").GetByText("Hello1").ClickAsync();`);
[sources] = await Promise.all([
@@ -154,7 +154,7 @@ test.describe('cli codegen', () => {
await page.frame_locator("#frame1").frame_locator("iframe").get_by_text("Hello2").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.FrameLocator("#frame1").FrameLocator("iframe").GetByText("Hello2").ClickAsync();`);
+await page.FrameLocator("#frame1").FrameLocator("iframe").GetByText("Hello2").ClickAsync();`);
[sources] = await Promise.all([
@@ -175,7 +175,7 @@ test.describe('cli codegen', () => {
await page.frame_locator("#frame1").frame_locator("iframe").frame_locator("iframe[name=\\"one\\"]").get_by_text("HelloNameOne").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.FrameLocator("#frame1").FrameLocator("iframe").FrameLocator("iframe[name=\\"one\\"]").GetByText("HelloNameOne").ClickAsync();`);
+await page.FrameLocator("#frame1").FrameLocator("iframe").FrameLocator("iframe[name=\\"one\\"]").GetByText("HelloNameOne").ClickAsync();`);
[sources] = await Promise.all([
recorder.waitForOutput('JavaScript', 'HelloNameAnonymous'),
@@ -195,7 +195,7 @@ test.describe('cli codegen', () => {
await page.frame_locator("#frame1").frame_locator("iframe").frame_locator("iframe >> nth=2").get_by_text("HelloNameAnonymous").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.FrameLocator("#frame1").FrameLocator("iframe").FrameLocator("iframe >> nth=2").GetByText("HelloNameAnonymous").ClickAsync();`);
+await page.FrameLocator("#frame1").FrameLocator("iframe").FrameLocator("iframe >> nth=2").GetByText("HelloNameAnonymous").ClickAsync();`);
});
test('should generate frame locators with special characters in name attribute', async ({ page, openRecorder, server }) => {
@@ -223,7 +223,7 @@ test.describe('cli codegen', () => {
await page.frame_locator("iframe[name=\\"foo\\\\<bar\\\\'\\\\\\"\\\\\`\\\\>\\"]").get_by_role("button", name="Click me").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.FrameLocator("iframe[name=\\"foo\\\\<bar\\\\'\\\\\\"\\\\\`\\\\>\\"]").GetByRole(AriaRole.Button, new() { Name = "Click me" }).ClickAsync();`);
+await page.FrameLocator("iframe[name=\\"foo\\\\<bar\\\\'\\\\\\"\\\\\`\\\\>\\"]").GetByRole(AriaRole.Button, new() { Name = "Click me" }).ClickAsync();`);
});
test('should generate frame locators with title attribute', async ({ page, openRecorder, server }) => {
@@ -377,7 +377,7 @@ test.describe('cli codegen', () => {
await page.frame_locator("#frame1").get_by_role("button", name="Submit").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.FrameLocator("#frame1").GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();`);
+await page.FrameLocator("#frame1").GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();`);
});
test('should generate getByTestId', async ({ page, openRecorder }) => {
@@ -407,7 +407,7 @@ test.describe('cli codegen', () => {
page.getByTestId("testid").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByTestId("testid").ClickAsync();`);
+await page.GetByTestId("testid").ClickAsync();`);
expect(message.text()).toBe('click');
});
@@ -438,7 +438,7 @@ test.describe('cli codegen', () => {
page.getByPlaceholder("Country").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByPlaceholder("Country").ClickAsync();`);
+await page.GetByPlaceholder("Country").ClickAsync();`);
});
test('should generate getByAltText', async ({ page, openRecorder }) => {
@@ -467,7 +467,7 @@ test.describe('cli codegen', () => {
page.getByAltText("Country").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByAltText("Country").ClickAsync();`);
+await page.GetByAltText("Country").ClickAsync();`);
});
test('should generate getByLabel', async ({ page, openRecorder }) => {
@@ -496,7 +496,7 @@ test.describe('cli codegen', () => {
page.getByLabel("Country").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByLabel("Country").ClickAsync();`);
+await page.GetByLabel("Country").ClickAsync();`);
});
test('should generate getByLabel without regex', async ({ page, openRecorder }) => {
@@ -525,7 +525,7 @@ test.describe('cli codegen', () => {
page.getByLabel("Coun\\"try").click()`);
expect.soft(sources.get('C#')!.text).toContain(`
- await page.GetByLabel("Coun\\"try").ClickAsync();`);
+await page.GetByLabel("Coun\\"try").ClickAsync();`);
});
test('should consume pointer events', async ({ page, openRecorder }) => {
diff --git a/tests/library/inspector/cli-codegen-csharp.spec.ts b/tests/library/inspector/cli-codegen-csharp.spec.ts
index c44043cea1..b6d7db872c 100644
--- a/tests/library/inspector/cli-codegen-csharp.spec.ts
+++ b/tests/library/inspector/cli-codegen-csharp.spec.ts
@@ -33,16 +33,12 @@ test('should print the correct imports and context options', async ({ browserNam
using System;
using System.Threading.Tasks;
-class Program
+using var playwright = await Playwright.CreateAsync();
+await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
{
- public static async Task Main()
- {
- using var playwright = await Playwright.CreateAsync();
- await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
- {
- ${launchOptions(channel)}
- });
- var context = await browser.NewContextAsync();`;
+ ${launchOptions(channel)}
+});
+var context = await browser.NewContextAsync();`;
await cli.waitFor(expectedResult);
});
@@ -58,33 +54,33 @@ test('should print the correct context options for custom settings', async ({ br
'--target=csharp',
emptyHTML]);
const expectedResult = `
- 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,
- },
- });`;
+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,
+ },
+});`;
await cli.waitFor(expectedResult);
});
@@ -93,12 +89,12 @@ test('should print the correct context options when using a device', async ({ br
const cli = runCLI(['--device=Pixel 2', '--target=csharp', emptyHTML]);
const expectedResult = `
- 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"]);`;
+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"]);`;
await cli.waitFor(expectedResult);
});
@@ -117,33 +113,33 @@ test('should print the correct context options when using a device and additiona
'--target=csharp',
emptyHTML]);
const expectedResult = `
- 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,
- },
- });`;
+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,
+ },
+});`;
await cli.waitFor(expectedResult);
});
@@ -153,21 +149,21 @@ test('should print load/save storageState', async ({ browserName, channel, runCL
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=csharp', emptyHTML]);
const expectedResult1 = `
- 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, '\\\\')}",
- });`;
+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, '\\\\')}",
+});`;
await cli.waitFor(expectedResult1);
const expectedResult2 = `
- await context.StorageStateAsync(new BrowserContextStorageStateOptions
- {
- Path = "${saveFileName.replace(/\\/g, '\\\\')}"
- });
+await context.StorageStateAsync(new BrowserContextStorageStateOptions
+{
+ Path = "${saveFileName.replace(/\\/g, '\\\\')}"
+});
`;
await cli.waitFor(expectedResult2);
});
@@ -175,12 +171,12 @@ test('should print load/save storageState', async ({ browserName, channel, runCL
test('should work with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har');
const expectedResult = `
- var context = await browser.NewContextAsync(new BrowserNewContextOptions
- {
- RecordHarMode = HarMode.Minimal,
- RecordHarPath = ${JSON.stringify(harFileName)},
- ServiceWorkers = ServiceWorkerPolicy.Block,
- });`;
+var context = await browser.NewContextAsync(new BrowserNewContextOptions
+{
+ RecordHarMode = HarMode.Minimal,
+ RecordHarPath = ${JSON.stringify(harFileName)},
+ ServiceWorkers = ServiceWorkerPolicy.Block,
+});`;
const cli = runCLI(['--target=csharp', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult,
});
@@ -231,7 +227,6 @@ public class Tests : PageTest
public async Task MyTest()
{
await Page.GotoAsync("${emptyHTML}");
-
}
}`;
expect(cli.text()).toContain(expected);
@@ -259,7 +254,6 @@ public class Tests : PageTest
public async Task MyTest()
{
await Page.GotoAsync("${emptyHTML}");
-
}
}`;
expect(cli.text()).toContain(expected);