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): improve error message when unchecking radio buttons (#37184)

Shahed25c105ab7e9b
diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.ts
index a18c6ce2b5..dde268ee61 100644
--- a/packages/injected/src/injectedScript.ts
+++ b/packages/injected/src/injectedScript.ts
@@ -51,7 +51,7 @@ export type FrameExpectParams = Omit<channels.FrameExpectParams, 'expectedValue'
export type ElementState = 'visible' | 'hidden' | 'enabled' | 'disabled' | 'editable' | 'checked' | 'unchecked' | 'indeterminate' | 'stable';
export type ElementStateWithoutStable = Exclude<ElementState, 'stable'>;
-export type ElementStateQueryResult = { matches: boolean, received?: string | 'error:notconnected' };
+export type ElementStateQueryResult = { matches: boolean, received?: string | 'error:notconnected', isRadio?: boolean };
export type HitTargetInterceptionResult = {
stop: () => 'done' | { hitTargetDescription: string };
@@ -735,9 +735,11 @@ export class InjectedScript {
const checked = getCheckedWithoutMixed(element);
if (checked === 'error')
throw this.createStacklessError('Not a checkbox or radio button');
+ const isRadio = element.nodeName === 'INPUT' && (element as HTMLInputElement).type === 'radio';
return {
matches: need === checked,
received: checked ? 'checked' : 'unchecked',
+ isRadio,
};
}
diff --git a/packages/playwright-core/src/server/dom.ts b/packages/playwright-core/src/server/dom.ts
index a393afc170..8aa3a6e864 100644
--- a/packages/playwright-core/src/server/dom.ts
+++ b/packages/playwright-core/src/server/dom.ts
@@ -730,17 +730,21 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
const result = await progress.race(this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'checked'), {}));
if (result === 'error:notconnected' || result.received === 'error:notconnected')
throwElementIsNotAttached();
- return result.matches;
+ return { matches: result.matches, isRadio: result.isRadio };
};
await this._markAsTargetElement(progress);
- if (await isChecked() === state)
+ const checkedState = await isChecked();
+ if (checkedState.matches === state)
return 'done';
+ if (!state && checkedState.isRadio)
+ throw new NonRecoverableDOMError('Cannot uncheck radio button. Radio buttons can only be unchecked by selecting another radio button in the same group.');
const result = await this._click(progress, { ...options, waitAfter: 'disabled' });
if (result !== 'done')
return result;
if (options.trial)
return 'done';
- if (await isChecked() !== state)
+ const finalState = await isChecked();
+ if (finalState.matches !== state)
throw new NonRecoverableDOMError('Clicking the checkbox did not change its state');
return 'done';
}
diff --git a/tests/page/page-check.spec.ts b/tests/page/page-check.spec.ts
index 01b00ddc55..73fe05acb4 100644
--- a/tests/page/page-check.spec.ts
+++ b/tests/page/page-check.spec.ts
@@ -145,3 +145,9 @@ it('should check the box using setChecked', async ({ page }) => {
await page.setChecked('input', false);
expect(await page.evaluate(() => window['checkbox'].checked)).toBe(false);
});
+
+it('should throw when trying to uncheck radio button', async ({ page }) => {
+ await page.setContent(`<input type='radio' name='test' checked id='radio'>`);
+ const error = await page.uncheck('#radio').catch(e => e);
+ expect(error.message).toContain('Cannot uncheck radio button');
+});