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

This commit is contained in:
Max Schmitt 2023-12-15 10:24:26 -08:00 committed by GitHub
parent 9c845365f7
commit 44c3ad5ceb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 158 additions and 166 deletions

View File

@ -53,7 +53,7 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
generateAction(actionInContext: ActionInContext): string { generateAction(actionInContext: ActionInContext): string {
const action = this._generateActionInner(actionInContext); const action = this._generateActionInner(actionInContext);
if (action) if (action)
return action + '\n'; return action;
return ''; return '';
} }
@ -64,7 +64,7 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
let pageAlias = actionInContext.frame.pageAlias; let pageAlias = actionInContext.frame.pageAlias;
if (this._mode !== 'library') if (this._mode !== 'library')
pageAlias = pageAlias.replace('page', 'Page'); pageAlias = pageAlias.replace('page', 'Page');
const formatter = new CSharpFormatter(8); const formatter = new CSharpFormatter(this._mode === 'library' ? 0 : 8);
if (action.name === 'openPage') { if (action.name === 'openPage') {
formatter.add(`var ${pageAlias} = await context.NewPageAsync();`); formatter.add(`var ${pageAlias} = await context.NewPageAsync();`);
@ -184,10 +184,6 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
class Program
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')}); await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')});
var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`); var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
@ -220,9 +216,11 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
} }
generateFooter(saveStorage: string | undefined): string { generateFooter(saveStorage: string | undefined): string {
const storageStateLine = saveStorage ? `\n await context.StorageStateAsync(new BrowserContextStorageStateOptions\n {\n Path = ${quote(saveStorage)}\n });\n` : ''; const offset = this._mode === 'library' ? '' : ' ';
return `${storageStateLine} } let storageStateLine = saveStorage ? `\n${offset}await context.StorageStateAsync(new BrowserContextStorageStateOptions\n${offset}{\n${offset} Path = ${quote(saveStorage)}\n${offset}});\n` : '';
}\n`; if (this._mode !== 'library')
storageStateLine += ` }\n}\n`;
return storageStateLine;
} }
} }

View File

@ -46,7 +46,7 @@ test.describe('cli codegen', () => {
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click()`); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click()`);
expect.soft(sources.get('C#')!.text).toContain(` 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'); expect(message.text()).toBe('click');
}); });
@ -144,14 +144,14 @@ test.describe('cli codegen', () => {
.setPosition(250, 250));`); .setPosition(250, 250));`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await page.Locator("canvas").ClickAsync(new LocatorClickOptions await page.Locator("canvas").ClickAsync(new LocatorClickOptions
{ {
Position = new Position Position = new Position
{ {
X = 250, X = 250,
Y = 250, Y = 250,
}, },
});`); });`);
expect(message.text()).toBe('click 250 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()`); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click()`);
expect.soft(sources.get('C#')!.text).toContain(` 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'); expect(message.text()).toBe('click');
}); });
@ -249,7 +249,7 @@ test.describe('cli codegen', () => {
await page.locator("#input").fill(\"John\")`); await page.locator("#input").fill(\"John\")`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await page.Locator("#input").FillAsync(\"John\");`); await page.Locator("#input").FillAsync(\"John\");`);
expect(message.text()).toBe('John'); expect(message.text()).toBe('John');
}); });
@ -283,7 +283,7 @@ test.describe('cli codegen', () => {
await page.locator("#input").fill(\"てすと\")`); await page.locator("#input").fill(\"てすと\")`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await page.Locator("#input").FillAsync(\"てすと\");`); await page.Locator("#input").FillAsync(\"てすと\");`);
expect(message.text()).toBe('てすと'); expect(message.text()).toBe('てすと');
}); });
@ -366,7 +366,7 @@ test.describe('cli codegen', () => {
await page.get_by_role("textbox").press("Shift+Enter")`); await page.get_by_role("textbox").press("Shift+Enter")`);
expect(sources.get('C#')!.text).toContain(` 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'); expect(messages[0].text()).toBe('press');
}); });
@ -474,7 +474,7 @@ test.describe('cli codegen', () => {
await page.locator("#checkbox").check()`); await page.locator("#checkbox").check()`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await page.Locator("#checkbox").CheckAsync();`); await page.Locator("#checkbox").CheckAsync();`);
expect(message.text()).toBe('true'); expect(message.text()).toBe('true');
}); });
@ -544,7 +544,7 @@ test.describe('cli codegen', () => {
await page.locator("#checkbox").uncheck()`); await page.locator("#checkbox").uncheck()`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await page.Locator("#checkbox").UncheckAsync();`); await page.Locator("#checkbox").UncheckAsync();`);
expect(message.text()).toBe('false'); expect(message.text()).toBe('false');
}); });
@ -576,7 +576,7 @@ test.describe('cli codegen', () => {
await page.locator("#age").select_option("2")`); await page.locator("#age").select_option("2")`);
expect(sources.get('C#')!.text).toContain(` 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'); expect(message.text()).toBe('2');
}); });
@ -617,7 +617,7 @@ test.describe('cli codegen', () => {
await page.locator(\"#age\").select_option(\"2\")`); await page.locator(\"#age\").select_option(\"2\")`);
expect(sources.get('C#')!.text).toContain(` 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'); expect(message.text()).toBe('2');
}); });
@ -656,10 +656,10 @@ test.describe('cli codegen', () => {
page1 = await page1_info.value`); page1 = await page1_info.value`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
var page1 = await page.RunAndWaitForPopupAsync(async () => var page1 = await page.RunAndWaitForPopupAsync(async () =>
{ {
await page.GetByRole(AriaRole.Link, new() { Name = "link" }).ClickAsync(); await page.GetByRole(AriaRole.Link, new() { Name = "link" }).ClickAsync();
});`); });`);
expect(popup.url()).toBe('about:blank'); expect(popup.url()).toBe('about:blank');
}); });
@ -696,7 +696,7 @@ test.describe('cli codegen', () => {
page.get_by_text("link").click()`); page.get_by_text("link").click()`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
await page.GetByText("link").ClickAsync();`); await page.GetByText("link").ClickAsync();`);
expect(page.url()).toContain('about:blank#foo'); expect(page.url()).toContain('about:blank#foo');
}); });
@ -741,9 +741,9 @@ test.describe('cli codegen', () => {
.setButton(MouseButton.MIDDLE));`); .setButton(MouseButton.MIDDLE));`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await page.GetByText("Click me").ClickAsync(new LocatorClickOptions await page.GetByText("Click me").ClickAsync(new LocatorClickOptions
{ {
Button = MouseButton.Middle, Button = MouseButton.Middle,
});`); });`);
}); });
}); });

View File

@ -40,7 +40,7 @@ test.describe('cli codegen', () => {
page = await context.new_page()`); page = await context.new_page()`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
var page = await context.NewPageAsync();`); var page = await context.NewPageAsync();`);
}); });
test('should contain second page', async ({ openRecorder, page }) => { test('should contain second page', async ({ openRecorder, page }) => {
@ -63,7 +63,7 @@ test.describe('cli codegen', () => {
page1 = await context.new_page()`); page1 = await context.new_page()`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
var page1 = await context.NewPageAsync();`); var page1 = await context.NewPageAsync();`);
}); });
test('should contain close page', async ({ openRecorder, page }) => { test('should contain close page', async ({ openRecorder, page }) => {
@ -87,7 +87,7 @@ test.describe('cli codegen', () => {
await page.close()`); await page.close()`);
expect(sources.get('C#')!.text).toContain(` 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 }) => { 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\")`); await page.get_by_role("textbox").set_input_files(\"file-to-upload.txt\")`);
expect(sources.get('C#')!.text).toContain(` 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 }) => { 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\"]`); await page.get_by_role("textbox").set_input_files([\"file-to-upload.txt\", \"file-to-upload-2.txt\"]`);
expect(sources.get('C#')!.text).toContain(` 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 }) => { 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([])`); await page.get_by_role("textbox").set_input_files([])`);
expect(sources.get('C#')!.text).toContain(` 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 }) => { test('should download files', async ({ page, openRecorder, server }) => {
@ -262,15 +262,15 @@ test.describe('cli codegen', () => {
download1 = await download1_info.value`); download1 = await download1_info.value`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
var download = await page.RunAndWaitForDownloadAsync(async () => var download = await page.RunAndWaitForDownloadAsync(async () =>
{ {
await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync(); await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync();
});`); });`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
var download1 = await page.RunAndWaitForDownloadAsync(async () => var download1 = await page.RunAndWaitForDownloadAsync(async () =>
{ {
await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync(); await page.GetByRole(AriaRole.Link, new() { Name = "Download" }).ClickAsync();
});`); });`);
}); });
test('should handle dialogs', async ({ page, openRecorder }) => { 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()`); await page.get_by_role("button", name="click me").click()`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
void page_Dialog_EventHandler(object sender, IDialog dialog) void page_Dialog_EventHandler(object sender, IDialog dialog)
{ {
Console.WriteLine($\"Dialog message: {dialog.Message}\"); Console.WriteLine($\"Dialog message: {dialog.Message}\");
dialog.DismissAsync(); dialog.DismissAsync();
page.Dialog -= page_Dialog_EventHandler; page.Dialog -= page_Dialog_EventHandler;
} }
page.Dialog += page_Dialog_EventHandler; page.Dialog += page_Dialog_EventHandler;
await page.GetByRole(AriaRole.Button, new() { Name = "click me" }).ClickAsync();`); await page.GetByRole(AriaRole.Button, new() { Name = "click me" }).ClickAsync();`);
}); });
@ -355,8 +355,8 @@ test.describe('cli codegen', () => {
page1 = await context.new_page() page1 = await context.new_page()
await page1.goto("about:blank?foo")`); await page1.goto("about:blank?foo")`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
var page1 = await context.NewPageAsync(); var page1 = await context.NewPageAsync();
await page1.GotoAsync("about:blank?foo");`); await page1.GotoAsync("about:blank?foo");`);
} else { } else {
expect(sources.get('JavaScript')!.text).toContain(` expect(sources.get('JavaScript')!.text).toContain(`
const page1Promise = page.waitForEvent('popup'); const page1Promise = page.waitForEvent('popup');
@ -541,7 +541,7 @@ test.describe('cli codegen', () => {
await page.locator("#textarea").fill(\"Hello'\\"\`\\nWorld\")`); await page.locator("#textarea").fill(\"Hello'\\"\`\\nWorld\")`);
expect(sources.get('C#')!.text).toContain(` 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'); expect(message.text()).toBe('Hello\'\"\`\nWorld');
}); });

View File

@ -49,7 +49,7 @@ test.describe('cli codegen', () => {
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).first().click();`); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).first().click();`);
expect.soft(sources.get('C#')!.text).toContain(` 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'); 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();`); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).nth(1).click();`);
expect.soft(sources.get('C#')!.text).toContain(` 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'); expect(message.text()).toBe('click2');
}); });
@ -133,7 +133,7 @@ test.describe('cli codegen', () => {
await page.frame_locator("#frame1").get_by_text("Hello1").click()`); await page.frame_locator("#frame1").get_by_text("Hello1").click()`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await page.FrameLocator("#frame1").GetByText("Hello1").ClickAsync();`); await page.FrameLocator("#frame1").GetByText("Hello1").ClickAsync();`);
[sources] = await Promise.all([ [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()`); await page.frame_locator("#frame1").frame_locator("iframe").get_by_text("Hello2").click()`);
expect.soft(sources.get('C#')!.text).toContain(` 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([ [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()`); 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(` 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([ [sources] = await Promise.all([
recorder.waitForOutput('JavaScript', 'HelloNameAnonymous'), 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()`); 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(` 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 }) => { 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()`); await page.frame_locator("iframe[name=\\"foo\\\\<bar\\\\'\\\\\\"\\\\\`\\\\>\\"]").get_by_role("button", name="Click me").click()`);
expect.soft(sources.get('C#')!.text).toContain(` 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 }) => { 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()`); await page.frame_locator("#frame1").get_by_role("button", name="Submit").click()`);
expect.soft(sources.get('C#')!.text).toContain(` 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 }) => { test('should generate getByTestId', async ({ page, openRecorder }) => {
@ -407,7 +407,7 @@ test.describe('cli codegen', () => {
page.getByTestId("testid").click()`); page.getByTestId("testid").click()`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
await page.GetByTestId("testid").ClickAsync();`); await page.GetByTestId("testid").ClickAsync();`);
expect(message.text()).toBe('click'); expect(message.text()).toBe('click');
}); });
@ -438,7 +438,7 @@ test.describe('cli codegen', () => {
page.getByPlaceholder("Country").click()`); page.getByPlaceholder("Country").click()`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
await page.GetByPlaceholder("Country").ClickAsync();`); await page.GetByPlaceholder("Country").ClickAsync();`);
}); });
test('should generate getByAltText', async ({ page, openRecorder }) => { test('should generate getByAltText', async ({ page, openRecorder }) => {
@ -467,7 +467,7 @@ test.describe('cli codegen', () => {
page.getByAltText("Country").click()`); page.getByAltText("Country").click()`);
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
await page.GetByAltText("Country").ClickAsync();`); await page.GetByAltText("Country").ClickAsync();`);
}); });
test('should generate getByLabel', async ({ page, openRecorder }) => { test('should generate getByLabel', async ({ page, openRecorder }) => {
@ -496,7 +496,7 @@ test.describe('cli codegen', () => {
page.getByLabel("Country").click()`); page.getByLabel("Country").click()`);
expect.soft(sources.get('C#')!.text).toContain(` 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 }) => { test('should generate getByLabel without regex', async ({ page, openRecorder }) => {
@ -525,7 +525,7 @@ test.describe('cli codegen', () => {
page.getByLabel("Coun\\"try").click()`); page.getByLabel("Coun\\"try").click()`);
expect.soft(sources.get('C#')!.text).toContain(` 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 }) => { test('should consume pointer events', async ({ page, openRecorder }) => {

View File

@ -33,16 +33,12 @@ test('should print the correct imports and context options', async ({ browserNam
using System; using System;
using System.Threading.Tasks; 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)} ${launchOptions(channel)}
}); });
var context = await browser.NewContextAsync();`; var context = await browser.NewContextAsync();`;
await cli.waitFor(expectedResult); await cli.waitFor(expectedResult);
}); });
@ -58,17 +54,17 @@ test('should print the correct context options for custom settings', async ({ br
'--target=csharp', '--target=csharp',
emptyHTML]); emptyHTML]);
const expectedResult = ` const expectedResult = `
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
{ {
${launchOptions(channel)} ${launchOptions(channel)}
Proxy = new ProxySettings Proxy = new ProxySettings
{ {
Server = "http://myproxy:3128", Server = "http://myproxy:3128",
}, },
}); });
var context = await browser.NewContextAsync(new BrowserNewContextOptions var context = await browser.NewContextAsync(new BrowserNewContextOptions
{ {
ColorScheme = ColorScheme.Dark, ColorScheme = ColorScheme.Dark,
Geolocation = new Geolocation Geolocation = new Geolocation
{ {
@ -84,7 +80,7 @@ test('should print the correct context options for custom settings', async ({ br
Height = 720, Height = 720,
Width = 1280, Width = 1280,
}, },
});`; });`;
await cli.waitFor(expectedResult); 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 cli = runCLI(['--device=Pixel 2', '--target=csharp', emptyHTML]);
const expectedResult = ` const expectedResult = `
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
{ {
${launchOptions(channel)} ${launchOptions(channel)}
}); });
var context = await browser.NewContextAsync(playwright.Devices["Pixel 2"]);`; var context = await browser.NewContextAsync(playwright.Devices["Pixel 2"]);`;
await cli.waitFor(expectedResult); await cli.waitFor(expectedResult);
}); });
@ -117,17 +113,17 @@ test('should print the correct context options when using a device and additiona
'--target=csharp', '--target=csharp',
emptyHTML]); emptyHTML]);
const expectedResult = ` const expectedResult = `
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
{ {
${launchOptions(channel)} ${launchOptions(channel)}
Proxy = new ProxySettings Proxy = new ProxySettings
{ {
Server = "http://myproxy:3128", Server = "http://myproxy:3128",
}, },
}); });
var context = await browser.NewContextAsync(new BrowserNewContextOptions(playwright.Devices["iPhone 11"]) var context = await browser.NewContextAsync(new BrowserNewContextOptions(playwright.Devices["iPhone 11"])
{ {
ColorScheme = ColorScheme.Dark, ColorScheme = ColorScheme.Dark,
Geolocation = new Geolocation Geolocation = new Geolocation
{ {
@ -143,7 +139,7 @@ test('should print the correct context options when using a device and additiona
Height = 720, Height = 720,
Width = 1280, Width = 1280,
}, },
});`; });`;
await cli.waitFor(expectedResult); 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'); await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=csharp', emptyHTML]); const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=csharp', emptyHTML]);
const expectedResult1 = ` const expectedResult1 = `
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
{ {
${launchOptions(channel)} ${launchOptions(channel)}
}); });
var context = await browser.NewContextAsync(new BrowserNewContextOptions var context = await browser.NewContextAsync(new BrowserNewContextOptions
{ {
StorageStatePath = "${loadFileName.replace(/\\/g, '\\\\')}", StorageStatePath = "${loadFileName.replace(/\\/g, '\\\\')}",
});`; });`;
await cli.waitFor(expectedResult1); await cli.waitFor(expectedResult1);
const expectedResult2 = ` const expectedResult2 = `
await context.StorageStateAsync(new BrowserContextStorageStateOptions await context.StorageStateAsync(new BrowserContextStorageStateOptions
{ {
Path = "${saveFileName.replace(/\\/g, '\\\\')}" Path = "${saveFileName.replace(/\\/g, '\\\\')}"
}); });
`; `;
await cli.waitFor(expectedResult2); 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) => { test('should work with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har'); const harFileName = testInfo.outputPath('har.har');
const expectedResult = ` const expectedResult = `
var context = await browser.NewContextAsync(new BrowserNewContextOptions var context = await browser.NewContextAsync(new BrowserNewContextOptions
{ {
RecordHarMode = HarMode.Minimal, RecordHarMode = HarMode.Minimal,
RecordHarPath = ${JSON.stringify(harFileName)}, RecordHarPath = ${JSON.stringify(harFileName)},
ServiceWorkers = ServiceWorkerPolicy.Block, ServiceWorkers = ServiceWorkerPolicy.Block,
});`; });`;
const cli = runCLI(['--target=csharp', `--save-har=${harFileName}`], { const cli = runCLI(['--target=csharp', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult, autoExitWhen: expectedResult,
}); });
@ -231,7 +227,6 @@ public class Tests : PageTest
public async Task MyTest() public async Task MyTest()
{ {
await Page.GotoAsync("${emptyHTML}"); await Page.GotoAsync("${emptyHTML}");
} }
}`; }`;
expect(cli.text()).toContain(expected); expect(cli.text()).toContain(expected);
@ -259,7 +254,6 @@ public class Tests : PageTest
public async Task MyTest() public async Task MyTest()
{ {
await Page.GotoAsync("${emptyHTML}"); await Page.GotoAsync("${emptyHTML}");
} }
}`; }`;
expect(cli.text()).toContain(expected); expect(cli.text()).toContain(expected);