cherry-pick(#17961): fix(codegen): use constants when generating C# and Java roles

This commit is contained in:
Pavel Feldman 2022-10-10 11:25:56 -08:00
parent 4ff2f3d0c5
commit f87d6f2838
4 changed files with 48 additions and 48 deletions

View file

@ -234,7 +234,7 @@ export class JavaLocatorFactory implements LocatorFactory {
for (const [name, value] of Object.entries(options.attrs!)) for (const [name, value] of Object.entries(options.attrs!))
attrs.push(`.set${toTitleCase(name)}(${typeof value === 'string' ? this.quote(value) : value})`); attrs.push(`.set${toTitleCase(name)}(${typeof value === 'string' ? this.quote(value) : value})`);
const attrString = attrs.length ? `, new ${clazz}.GetByRoleOptions()${attrs.join('')}` : ''; const attrString = attrs.length ? `, new ${clazz}.GetByRoleOptions()${attrs.join('')}` : '';
return `getByRole(${this.quote(body)}${attrString})`; return `getByRole(AriaRole.${toSnakeCase(body).toUpperCase()}${attrString})`;
case 'has-text': case 'has-text':
return `locator(${this.quote(body)}, new ${clazz}.LocatorOptions().setHasText(${this.quote(options.hasText!)}))`; return `locator(${this.quote(body)}, new ${clazz}.LocatorOptions().setHasText(${this.quote(options.hasText!)}))`;
case 'test-id': case 'test-id':
@ -286,7 +286,7 @@ export class CSharpLocatorFactory implements LocatorFactory {
for (const [name, value] of Object.entries(options.attrs!)) for (const [name, value] of Object.entries(options.attrs!))
attrs.push(`${toTitleCase(name)} = ${typeof value === 'string' ? this.quote(value) : value}`); attrs.push(`${toTitleCase(name)} = ${typeof value === 'string' ? this.quote(value) : value}`);
const attrString = attrs.length ? `, new () { ${attrs.join(', ')} }` : ''; const attrString = attrs.length ? `, new () { ${attrs.join(', ')} }` : '';
return `GetByRole(${this.quote(body)}${attrString})`; return `GetByRole(AriaRole.${toTitleCase(body)}${attrString})`;
case 'has-text': case 'has-text':
return `Locator(${this.quote(body)}, new () { HasTextString: ${this.quote(options.hasText!)} })`; return `Locator(${this.quote(body)}, new () { HasTextString: ${this.quote(options.hasText!)} })`;
case 'test-id': case 'test-id':

View file

@ -34,20 +34,20 @@ test.describe('cli codegen', () => {
page.dispatchEvent('button', 'click', { detail: 1 }) page.dispatchEvent('button', 'click', { detail: 1 })
]); ]);
expect(sources.get('JavaScript').text).toContain(` expect.soft(sources.get('JavaScript').text).toContain(`
await page.getByRole('button', { name: 'Submit' }).click();`); await page.getByRole('button', { name: 'Submit' }).click();`);
expect(sources.get('Python').text).toContain(` expect.soft(sources.get('Python').text).toContain(`
page.get_by_role("button", name="Submit").click()`); page.get_by_role("button", name="Submit").click()`);
expect(sources.get('Python Async').text).toContain(` expect.soft(sources.get('Python Async').text).toContain(`
await page.get_by_role("button", name="Submit").click()`); await page.get_by_role("button", name="Submit").click()`);
expect(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
page.getByRole("button", new Page.GetByRoleOptions().setName("Submit")).click()`); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click()`);
expect(sources.get('C#').text).toContain(` expect.soft(sources.get('C#').text).toContain(`
await page.GetByRole("button", new () { Name = "Submit" }).ClickAsync();`); await page.GetByRole(AriaRole.Button, new () { Name = "Submit" }).ClickAsync();`);
expect(message.text()).toBe('click'); expect(message.text()).toBe('click');
}); });
@ -157,20 +157,20 @@ test.describe('cli codegen', () => {
page.dispatchEvent('button', 'click', { detail: 1 }) page.dispatchEvent('button', 'click', { detail: 1 })
]); ]);
expect(sources.get('JavaScript').text).toContain(` expect.soft(sources.get('JavaScript').text).toContain(`
await page.getByRole('button', { name: 'Submit' }).click();`); await page.getByRole('button', { name: 'Submit' }).click();`);
expect(sources.get('Python').text).toContain(` expect.soft(sources.get('Python').text).toContain(`
page.get_by_role("button", name="Submit").click()`); page.get_by_role("button", name="Submit").click()`);
expect(sources.get('Python Async').text).toContain(` expect.soft(sources.get('Python Async').text).toContain(`
await page.get_by_role("button", name="Submit").click()`); await page.get_by_role("button", name="Submit").click()`);
expect(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
page.getByRole("button", new Page.GetByRoleOptions().setName("Submit")).click()`); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click()`);
expect(sources.get('C#').text).toContain(` expect.soft(sources.get('C#').text).toContain(`
await page.GetByRole("button", new () { Name = "Submit" }).ClickAsync();`); await page.GetByRole(AriaRole.Button, new () { Name = "Submit" }).ClickAsync();`);
expect(message.text()).toBe('click'); expect(message.text()).toBe('click');
}); });
@ -548,31 +548,31 @@ test.describe('cli codegen', () => {
page.dispatchEvent('a', 'click', { detail: 1 }) page.dispatchEvent('a', 'click', { detail: 1 })
]); ]);
expect(sources.get('JavaScript').text).toContain(` expect.soft(sources.get('JavaScript').text).toContain(`
const [page1] = await Promise.all([ const [page1] = await Promise.all([
page.waitForEvent('popup'), page.waitForEvent('popup'),
page.getByRole('link', { name: 'link' }).click() page.getByRole('link', { name: 'link' }).click()
]);`); ]);`);
expect(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
Page page1 = page.waitForPopup(() -> { Page page1 = page.waitForPopup(() -> {
page.getByRole("link", new Page.GetByRoleOptions().setName("link")).click(); page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("link")).click();
});`); });`);
expect(sources.get('Python').text).toContain(` expect.soft(sources.get('Python').text).toContain(`
with page.expect_popup() as popup_info: with page.expect_popup() as popup_info:
page.get_by_role("link", name="link").click() page.get_by_role("link", name="link").click()
page1 = popup_info.value`); page1 = popup_info.value`);
expect(sources.get('Python Async').text).toContain(` expect.soft(sources.get('Python Async').text).toContain(`
async with page.expect_popup() as popup_info: async with page.expect_popup() as popup_info:
await page.get_by_role("link", name="link").click() await page.get_by_role("link", name="link").click()
page1 = await popup_info.value`); page1 = await popup_info.value`);
expect(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("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');

View file

@ -226,41 +226,41 @@ test.describe('cli codegen', () => {
]); ]);
const sources = await recorder.waitForOutput('JavaScript', 'waitForEvent'); const sources = await recorder.waitForOutput('JavaScript', 'waitForEvent');
expect(sources.get('JavaScript').text).toContain(` expect.soft(sources.get('JavaScript').text).toContain(`
const context = await browser.newContext();`); const context = await browser.newContext();`);
expect(sources.get('JavaScript').text).toContain(` expect.soft(sources.get('JavaScript').text).toContain(`
const [download] = await Promise.all([ const [download] = await Promise.all([
page.waitForEvent('download'), page.waitForEvent('download'),
page.getByRole('link', { name: 'Download' }).click() page.getByRole('link', { name: 'Download' }).click()
]);`); ]);`);
expect(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
BrowserContext context = browser.newContext();`); BrowserContext context = browser.newContext();`);
expect(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
Download download = page.waitForDownload(() -> { Download download = page.waitForDownload(() -> {
page.getByRole("link", new Page.GetByRoleOptions().setName("Download")).click(); page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Download")).click();
});`); });`);
expect(sources.get('Python').text).toContain(` expect.soft(sources.get('Python').text).toContain(`
context = browser.new_context()`); context = browser.new_context()`);
expect(sources.get('Python').text).toContain(` expect.soft(sources.get('Python').text).toContain(`
with page.expect_download() as download_info: with page.expect_download() as download_info:
page.get_by_role("link", name="Download").click() page.get_by_role("link", name="Download").click()
download = download_info.value`); download = download_info.value`);
expect(sources.get('Python Async').text).toContain(` expect.soft(sources.get('Python Async').text).toContain(`
context = await browser.new_context()`); context = await browser.new_context()`);
expect(sources.get('Python Async').text).toContain(` expect.soft(sources.get('Python Async').text).toContain(`
async with page.expect_download() as download_info: async with page.expect_download() as download_info:
await page.get_by_role("link", name="Download").click() await page.get_by_role("link", name="Download").click()
download = await download_info.value`); download = await download_info.value`);
expect(sources.get('C#').text).toContain(` expect.soft(sources.get('C#').text).toContain(`
var context = await browser.NewContextAsync();`); var context = await browser.NewContextAsync();`);
expect(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("link", new () { Name = "Download" }).ClickAsync(); await page.GetByRole(AriaRole.Link, new () { Name = "Download" }).ClickAsync();
});`); });`);
}); });
@ -278,29 +278,29 @@ test.describe('cli codegen', () => {
const sources = await recorder.waitForOutput('JavaScript', 'once'); const sources = await recorder.waitForOutput('JavaScript', 'once');
expect(sources.get('JavaScript').text).toContain(` expect.soft(sources.get('JavaScript').text).toContain(`
page.once('dialog', dialog => { page.once('dialog', dialog => {
console.log(\`Dialog message: \${dialog.message()}\`); console.log(\`Dialog message: \${dialog.message()}\`);
dialog.dismiss().catch(() => {}); dialog.dismiss().catch(() => {});
}); });
await page.getByRole('button', { name: 'click me' }).click();`); await page.getByRole('button', { name: 'click me' }).click();`);
expect(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
page.onceDialog(dialog -> { page.onceDialog(dialog -> {
System.out.println(String.format("Dialog message: %s", dialog.message())); System.out.println(String.format("Dialog message: %s", dialog.message()));
dialog.dismiss(); dialog.dismiss();
}); });
page.getByRole("button", new Page.GetByRoleOptions().setName("click me")).click();`); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("click me")).click();`);
expect(sources.get('Python').text).toContain(` expect.soft(sources.get('Python').text).toContain(`
page.once(\"dialog\", lambda dialog: dialog.dismiss()) page.once(\"dialog\", lambda dialog: dialog.dismiss())
page.get_by_role("button", name="click me").click()`); page.get_by_role("button", name="click me").click()`);
expect(sources.get('Python Async').text).toContain(` expect.soft(sources.get('Python Async').text).toContain(`
page.once(\"dialog\", lambda dialog: dialog.dismiss()) page.once(\"dialog\", lambda dialog: dialog.dismiss())
await page.get_by_role("button", name="click me").click()`); await page.get_by_role("button", name="click me").click()`);
expect(sources.get('C#').text).toContain(` expect.soft(sources.get('C#').text).toContain(`
void page_Dialog1_EventHandler(object sender, IDialog dialog) void page_Dialog1_EventHandler(object sender, IDialog dialog)
{ {
Console.WriteLine($\"Dialog message: {dialog.Message}\"); Console.WriteLine($\"Dialog message: {dialog.Message}\");
@ -308,7 +308,7 @@ test.describe('cli codegen', () => {
page.Dialog -= page_Dialog1_EventHandler; page.Dialog -= page_Dialog1_EventHandler;
} }
page.Dialog += page_Dialog1_EventHandler; page.Dialog += page_Dialog1_EventHandler;
await page.GetByRole("button", new () { Name = "click me" }).ClickAsync();`); await page.GetByRole(AriaRole.Button, new () { Name = "click me" }).ClickAsync();`);
}); });

View file

@ -46,10 +46,10 @@ test.describe('cli codegen', () => {
await page.get_by_role("button", name="Submit").first.click()`); await page.get_by_role("button", name="Submit").first.click()`);
expect.soft(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
page.getByRole("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("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');
}); });
@ -81,10 +81,10 @@ test.describe('cli codegen', () => {
await page.get_by_role("button", name="Submit").nth(1).click()`); await page.get_by_role("button", name="Submit").nth(1).click()`);
expect.soft(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
page.getByRole("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("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');
}); });
@ -217,7 +217,7 @@ test.describe('cli codegen', () => {
await page.frameLocator('#frame1').getByRole('button', { name: 'Submit' }).click();`); await page.frameLocator('#frame1').getByRole('button', { name: 'Submit' }).click();`);
expect.soft(sources.get('Java').text).toContain(` expect.soft(sources.get('Java').text).toContain(`
page.frameLocator("#frame1").getByRole("button", new FrameLocator.GetByRoleOptions().setName("Submit")).click();`); page.frameLocator("#frame1").getByRole(AriaRole.BUTTON, new FrameLocator.GetByRoleOptions().setName("Submit")).click();`);
expect.soft(sources.get('Python').text).toContain(` expect.soft(sources.get('Python').text).toContain(`
page.frame_locator("#frame1").get_by_role("button", name="Submit").click()`); page.frame_locator("#frame1").get_by_role("button", name="Submit").click()`);
@ -226,7 +226,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("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 }) => {