chery-pick(#18060): fix(generator): .NET getByRole w/ name (#18066)

This PR cherry-picks the following commits:

- a60073d664

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Playwright Service 2022-10-13 09:45:29 -07:00 committed by GitHub
parent fde19c1652
commit 63d6a35ce5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 19 deletions

View file

@ -22,7 +22,7 @@ await page.GetByLabel("User Name").FillAsync("John");
await page.GetByLabel("Password").FillAsync("secret-password");
await page.GetByRole("button", new() { Name = "Sign in" }).ClickAsync();
await page.GetByRole("button", new() { NameString = "Sign in" }).ClickAsync();
await Expect(page.GetByText("Welcome, John!")).ToBeVisibleAsync();
```

View file

@ -285,8 +285,10 @@ export class CSharpLocatorFactory implements LocatorFactory {
return `Last`;
case 'role':
const attrs: string[] = [];
for (const [name, value] of Object.entries(options.attrs!))
attrs.push(`${toTitleCase(name)} = ${typeof value === 'string' ? this.quote(value) : value}`);
for (const [name, value] of Object.entries(options.attrs!)) {
const optionKey = name === 'name' ? 'NameString' : toTitleCase(name);
attrs.push(`${optionKey} = ${typeof value === 'string' ? this.quote(value) : value}`);
}
const attrString = attrs.length ? `, new() { ${attrs.join(', ')} }` : '';
return `GetByRole(AriaRole.${toTitleCase(body as string)}${attrString})`;
case 'has-text':

View file

@ -47,7 +47,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() { NameString = "Submit" }).ClickAsync();`);
expect(message.text()).toBe('click');
});
@ -170,7 +170,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() { NameString = "Submit" }).ClickAsync();`);
expect(message.text()).toBe('click');
});
@ -572,7 +572,7 @@ test.describe('cli codegen', () => {
expect.soft(sources.get('C#').text).toContain(`
var page1 = await page.RunAndWaitForPopupAsync(async () =>
{
await page.GetByRole(AriaRole.Link, new () { Name = "link" }).ClickAsync();
await page.GetByRole(AriaRole.Link, new() { NameString = "link" }).ClickAsync();
});`);
expect(popup.url()).toBe('about:blank');

View file

@ -260,7 +260,7 @@ test.describe('cli codegen', () => {
expect.soft(sources.get('C#').text).toContain(`
var download1 = await page.RunAndWaitForDownloadAsync(async () =>
{
await page.GetByRole(AriaRole.Link, new () { Name = "Download" }).ClickAsync();
await page.GetByRole(AriaRole.Link, new() { NameString = "Download" }).ClickAsync();
});`);
});
@ -308,7 +308,7 @@ test.describe('cli codegen', () => {
page.Dialog -= page_Dialog1_EventHandler;
}
page.Dialog += page_Dialog1_EventHandler;
await page.GetByRole(AriaRole.Button, new () { Name = "click me" }).ClickAsync();`);
await page.GetByRole(AriaRole.Button, new() { NameString = "click me" }).ClickAsync();`);
});

View file

@ -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() { NameString = "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() { NameString = "Submit" }).Nth(1).ClickAsync();`);
expect(message.text()).toBe('click2');
});
@ -226,7 +226,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() { NameString = "Submit" }).ClickAsync();`);
});
test('should generate getByTestId', async ({ page, openRecorder }) => {