Skip to content

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

fix(locators): parse mixed role options (#43170)

Yury Semikhatskyf0536183566b
diff --git a/packages/isomorphic/locatorParser.ts b/packages/isomorphic/locatorParser.ts
index 4222f67d14..ada8f9fb78 100644
--- a/packages/isomorphic/locatorParser.ts
+++ b/packages/isomorphic/locatorParser.ts
@@ -606,6 +606,8 @@ function getByRoleParts(call: CallArguments): ParsedSelectorPart[] {
body += `[${name}=${escapeForAttributeSelector(option(call, name, isText)!, exact)}]`;
else if (name === 'level')
body += `[level=${option(call, name, isNumber)}]`;
+ else if (name === 'checked' || name === 'pressed')
+ body += `[${name}=${option(call, name, isBooleanOrMixed)}]`;
else if (name !== 'exact')
body += `[${name === 'includeHidden' ? 'include-hidden' : name}=${option(call, name, isBoolean)}]`;
}
@@ -663,6 +665,10 @@ function isBoolean(value: Value): value is boolean {
return typeof value === 'boolean';
}
+function isBooleanOrMixed(value: Value): value is boolean | 'mixed' {
+ return isBoolean(value) || value === 'mixed';
+}
+
function isSelector(value: Value): value is ParsedSelector {
return typeof value === 'object' && !(value instanceof RegExp);
}
diff --git a/tests/library/locator-generator.spec.ts b/tests/library/locator-generator.spec.ts
index f9eb12a14e..8627e60f65 100644
--- a/tests/library/locator-generator.spec.ts
+++ b/tests/library/locator-generator.spec.ts
@@ -714,6 +714,15 @@ it('parseLocator role options', async () => {
expect.soft(parseLocator('javascript', `getByRole('button', { nme: 'foo' })`, '')).toBe(``);
});
+it('parseLocator round-trips mixed role options', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43158' }
+}, async () => {
+ for (const selector of ['internal:role=checkbox[checked=mixed]', 'internal:role=button[pressed=mixed]']) {
+ for (const lang of ['javascript', 'python', 'java', 'csharp'] as const)
+ expect.soft(parseLocator(lang, asLocator(lang, selector), 'data-testid'), `${lang}: ${selector}`).toBe(selector);
+ }
+});
+
it('parseLocator round-trips selectors', async () => {
const selectors = [
'div',