Skip to content

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

fix(locator): render getByRole() with a regex name and a string description (#42780)

om singhal9f6e40f05e72
diff --git a/packages/isomorphic/locatorGenerators.ts b/packages/isomorphic/locatorGenerators.ts
index 942c5d1c3c..7ac621c2cf 100644
--- a/packages/isomorphic/locatorGenerators.ts
+++ b/packages/isomorphic/locatorGenerators.ts
@@ -166,16 +166,14 @@ function innerAsLocators(factory: LocatorFactory, parsed: ParsedSelector, isFram
const attrSelector = parseAttributeSelector(part.body as string, true);
const options: LocatorOptions = { attrs: [] };
for (const attr of attrSelector.attributes) {
- if (attr.name === 'name') {
- if (options.exact !== undefined && options.exact !== attr.caseSensitive)
- throw new Error(`Conflicting exactness in internal:role selector: ${stringifySelector({ parts: [part] })}`);
- options.exact = attr.caseSensitive;
- options.name = attr.value;
- } else if (attr.name === 'description') {
- if (options.exact !== undefined && options.exact !== attr.caseSensitive)
- throw new Error(`Conflicting exactness in internal:role selector: ${stringifySelector({ parts: [part] })}`);
- options.exact = attr.caseSensitive;
- options.description = attr.value;
+ if (attr.name === 'name' || attr.name === 'description') {
+ // Only string values carry exactness, regular expressions are used as is.
+ if (typeof attr.value === 'string') {
+ if (options.exact !== undefined && options.exact !== attr.caseSensitive)
+ throw new Error(`Conflicting exactness in internal:role selector: ${stringifySelector({ parts: [part] })}`);
+ options.exact = attr.caseSensitive;
+ }
+ options[attr.name] = attr.value;
} else {
if (attr.name === 'level' && typeof attr.value === 'string')
attr.value = +attr.value;
diff --git a/tests/library/locator-generator.spec.ts b/tests/library/locator-generator.spec.ts
index cb3ad87158..6921d553ce 100644
--- a/tests/library/locator-generator.spec.ts
+++ b/tests/library/locator-generator.spec.ts
@@ -264,6 +264,21 @@ it('refuses to translate internal:role with conflicting name/description exactne
}
});
+it('reverse engineer internal:role with regex and string name/description', async () => {
+ expect.soft(generateForSelector('internal:role=alert[name=/Upload/][description="doc.pdf"i]')).toEqual({
+ javascript: `getByRole('alert', { name: /Upload/, description: 'doc.pdf' })`,
+ python: `get_by_role("alert", name=re.compile(r"Upload"), description="doc.pdf")`,
+ java: `getByRole(AriaRole.ALERT, new Page.GetByRoleOptions().setName(Pattern.compile("Upload")).setDescription("doc.pdf"))`,
+ csharp: `GetByRole(AriaRole.Alert, new() { NameRegex = new Regex("Upload"), Description = "doc.pdf" })`,
+ });
+ expect.soft(generateForSelector('internal:role=alert[name="Upload"i][description=/doc\\.pdf/]')).toEqual({
+ javascript: `getByRole('alert', { name: 'Upload', description: /doc\\.pdf/ })`,
+ python: `get_by_role("alert", name="Upload", description=re.compile(r"doc\\.pdf"))`,
+ java: `getByRole(AriaRole.ALERT, new Page.GetByRoleOptions().setName("Upload").setDescription(Pattern.compile("doc\\\\.pdf")))`,
+ csharp: `GetByRole(AriaRole.Alert, new() { Name = "Upload", DescriptionRegex = new Regex("doc\\\\.pdf") })`,
+ });
+});
+
it('reverse engineer ignore-case locators', async ({ page }) => {
expect.soft(generate(page.getByText('hello my\nwo"rld'))).toEqual({
csharp: 'GetByText("hello my\\nwo\\"rld")',