Skip to content

Commit 270206e

Browse files
authored
feat(text selector): match button input by value (#1657)
Inputs of type button and submit are rendered with their value as text, so we match them by text. Fixes #1427.
1 parent f8ecdff commit 270206e

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

docs/selectors.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Text engine finds an element that contains a text node with passed text. Example
7878

7979
> **NOTE** Text engine searches for elements inside open shadow roots, but not inside closed shadow roots or iframes.
8080
81+
> **NOTE** Input elements of the type `button` and `submit` are rendered with their value as text, and text engine finds them. For example, `text=Login` matches `<input type=button value="Login">`.
82+
8183
> **NOTE** Malformed selector starting with `"` is automatically transformed to text selector. For example, Playwright converts `page.click('"Login"')` to `page.click('text="Login"')`.
8284
8385
### id, data-testid, data-test-id, data-test

src/injected/textSelectorEngine.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ function queryInternal(root: SelectorRoot, matcher: Matcher): Element | undefine
6868
const node = walker.currentNode;
6969
if (node.nodeType === Node.ELEMENT_NODE) {
7070
const element = node as Element;
71+
if ((element instanceof HTMLInputElement) && (element.type === 'submit' || element.type === 'button') && matcher(element.value))
72+
return element;
7173
if (element.shadowRoot)
7274
shadowRoots.push(element.shadowRoot);
7375
} else {
@@ -92,6 +94,8 @@ function queryAllInternal(root: SelectorRoot, matcher: Matcher, result: Element[
9294
const node = walker.currentNode;
9395
if (node.nodeType === Node.ELEMENT_NODE) {
9496
const element = node as Element;
97+
if ((element instanceof HTMLInputElement) && (element.type === 'submit' || element.type === 'button') && matcher(element.value))
98+
result.push(element);
9599
if (element.shadowRoot)
96100
shadowRoots.push(element.shadowRoot);
97101
} else {

test/queryselector.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,12 @@ module.exports.describe = function({testRunner, expect, playwright, FFOX, CHROMI
541541
expect(await page.$(`text="with"`)).toBe(null);
542542
});
543543

544+
it('should match input[type=button|submit]', async({page}) => {
545+
await page.setContent(`<input type="submit" value="hello"><input type="button" value="world">`);
546+
expect(await page.$eval(`text=hello`, e => e.outerHTML)).toBe('<input type="submit" value="hello">');
547+
expect(await page.$eval(`text=world`, e => e.outerHTML)).toBe('<input type="button" value="world">');
548+
});
549+
544550
it('should work for open shadow roots', async({page, server}) => {
545551
await page.goto(server.PREFIX + '/deep-shadow.html');
546552
expect(await page.$eval(`text=root1`, e => e.outerHTML)).toBe('<span>Hello from root1</span>');

0 commit comments

Comments
 (0)