diff --git a/packages/playwright-core/src/server/injected/selectorGenerator.ts b/packages/playwright-core/src/server/injected/selectorGenerator.ts index 4b8f70494460b..04f15987621fe 100644 --- a/packages/playwright-core/src/server/injected/selectorGenerator.ts +++ b/packages/playwright-core/src/server/injected/selectorGenerator.ts @@ -38,9 +38,9 @@ const kOtherTestIdScore = 2; // other data-test* attributes const kIframeByAttributeScore = 10; const kBeginPenalizedScore = 50; -const kPlaceholderScore = 100; -const kLabelScore = 120; -const kRoleWithNameScore = 140; +const kRoleWithNameScore = 100; +const kPlaceholderScore = 120; +const kLabelScore = 140; const kAltTextScore = 160; const kTextScore = 180; const kTitleScore = 200; diff --git a/tests/library/inspector/cli-codegen-3.spec.ts b/tests/library/inspector/cli-codegen-3.spec.ts index a57e7d5405429..22032a8f26bff 100644 --- a/tests/library/inspector/cli-codegen-3.spec.ts +++ b/tests/library/inspector/cli-codegen-3.spec.ts @@ -472,7 +472,7 @@ await page.GetByTestId("testid").ClickAsync();`); await recorder.setContentAndWait(``); const locator = await recorder.hoverOverElement('input'); - expect(locator).toBe(`getByPlaceholder('Country')`); + expect(locator).toBe(`getByRole('textbox', { name: 'Country' })`); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'click'), @@ -480,19 +480,19 @@ await page.GetByTestId("testid").ClickAsync();`); ]); expect.soft(sources.get('JavaScript')!.text).toContain(` - await page.getByPlaceholder('Country').click();`); + await page.getByRole('textbox', { name: 'Country' }).click();`); expect.soft(sources.get('Python')!.text).toContain(` - page.get_by_placeholder("Country").click()`); + page.get_by_role("textbox", name="Country").click()`); expect.soft(sources.get('Python Async')!.text).toContain(` - await page.get_by_placeholder("Country").click()`); + await page.get_by_role("textbox", name="Country").click()`); expect.soft(sources.get('Java')!.text).toContain(` - page.getByPlaceholder("Country").click()`); + page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("Country")).click()`); expect.soft(sources.get('C#')!.text).toContain(` -await page.GetByPlaceholder("Country").ClickAsync();`); +await page.GetByRole(AriaRole.Textbox, new() { Name = "Country" }).ClickAsync();`); }); test('should generate getByAltText', async ({ openRecorder }) => { @@ -530,7 +530,7 @@ await page.GetByAltText("Country").ClickAsync();`); await recorder.setContentAndWait(``); const locator = await recorder.hoverOverElement('input'); - expect(locator).toBe(`getByLabel('Country')`); + expect(locator).toBe(`getByRole('textbox', { name: 'Country' })`); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'click'), @@ -538,19 +538,19 @@ await page.GetByAltText("Country").ClickAsync();`); ]); expect.soft(sources.get('JavaScript')!.text).toContain(` - await page.getByLabel('Country').click();`); + await page.getByRole('textbox', { name: 'Country' }).click();`); expect.soft(sources.get('Python')!.text).toContain(` - page.get_by_label("Country").click()`); + page.get_by_role("textbox", name="Country").click()`); expect.soft(sources.get('Python Async')!.text).toContain(` - await page.get_by_label("Country").click()`); + await page.get_by_role("textbox", name="Country").click()`); expect.soft(sources.get('Java')!.text).toContain(` - page.getByLabel("Country").click()`); + page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("Country")).click()`); expect.soft(sources.get('C#')!.text).toContain(` -await page.GetByLabel("Country").ClickAsync();`); +await page.GetByRole(AriaRole.Textbox, new() { Name = "Country" }).ClickAsync();`); }); test('should generate getByLabel without regex', async ({ openRecorder }) => { @@ -559,7 +559,7 @@ await page.GetByLabel("Country").ClickAsync();`); await recorder.setContentAndWait(``); const locator = await recorder.hoverOverElement('input'); - expect(locator).toBe(`getByLabel('Coun"try')`); + expect(locator).toBe(`getByRole('textbox', { name: 'Coun"try' })`); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'click'), @@ -567,19 +567,19 @@ await page.GetByLabel("Country").ClickAsync();`); ]); expect.soft(sources.get('JavaScript')!.text).toContain(` - await page.getByLabel('Coun\"try').click();`); + await page.getByRole('textbox', { name: 'Coun\"try' }).click();`); expect.soft(sources.get('Python')!.text).toContain(` - page.get_by_label("Coun\\"try").click()`); + page.get_by_role("textbox", name="Coun\\"try").click()`); expect.soft(sources.get('Python Async')!.text).toContain(` - await page.get_by_label("Coun\\"try").click()`); + await page.get_by_role("textbox", name="Coun\\"try").click()`); expect.soft(sources.get('Java')!.text).toContain(` - page.getByLabel("Coun\\"try").click()`); + page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName(\"Coun\\\"try\")).click();`); expect.soft(sources.get('C#')!.text).toContain(` -await page.GetByLabel("Coun\\"try").ClickAsync();`); +await page.GetByRole(AriaRole.Textbox, new() { Name = \"Coun\\\"try\" }).ClickAsync();`); }); test('should consume pointer events', async ({ openRecorder }) => { diff --git a/tests/library/selector-generator.spec.ts b/tests/library/selector-generator.spec.ts index c2e074c49f0b4..1feaf538e7be3 100644 --- a/tests/library/selector-generator.spec.ts +++ b/tests/library/selector-generator.spec.ts @@ -58,7 +58,7 @@ it.describe('selector generator', () => { it('should not escape spaces inside named attr selectors', async ({ page }) => { await page.setContent(``); - expect(await generate(page, 'input')).toBe('internal:attr=[placeholder=\"Foo b ar\"i]'); + expect(await generate(page, 'input')).toBe('internal:role=textbox[name=\"Foo b ar\"i]'); }); it('should generate text for ', async ({ page }) => { @@ -91,7 +91,7 @@ it.describe('selector generator', () => { it('should try to improve label text by shortening', async ({ page }) => { await page.setContent(``); - expect(await generate(page, 'input')).toBe('internal:label="Longest verbose description"i'); + expect(await generate(page, 'input')).toBe('internal:role=textbox[name=\"Longest verbose description\"i]'); }); it('should not improve guid text', async ({ page }) => { @@ -316,7 +316,7 @@ it.describe('selector generator', () => { }); it('placeholder', async ({ page }) => { await page.setContent(``); - expect(await generate(page, 'input')).toBe('internal:attr=[placeholder=\"foobar\"i]'); + expect(await generate(page, 'input')).toBe('internal:role=textbox[name=\"foobar\"i]'); }); it('name', async ({ page }) => { await page.setContent(` @@ -437,7 +437,7 @@ it.describe('selector generator', () => { it('should accept valid aria-label for candidate consideration', async ({ page }) => { await page.setContent(``); - expect(await generate(page, 'button')).toBe('internal:label="ariaLabel"i'); + expect(await generate(page, 'button')).toBe('internal:role=button[name=\"ariaLabel\"i]'); }); it('should ignore empty role for candidate consideration', async ({ page }) => { @@ -469,15 +469,15 @@ it.describe('selector generator', () => {