Skip to content

Commit

Permalink
feat(text selector): match button input by value (#1657)
Browse files Browse the repository at this point in the history
Inputs of type button and submit are rendered with their value as text,
so we match them by text.

Fixes #1427.
  • Loading branch information
dgozman authored Apr 3, 2020
1 parent f8ecdff commit 270206e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Text engine finds an element that contains a text node with passed text. Example

> **NOTE** Text engine searches for elements inside open shadow roots, but not inside closed shadow roots or iframes.
> **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">`.
> **NOTE** Malformed selector starting with `"` is automatically transformed to text selector. For example, Playwright converts `page.click('"Login"')` to `page.click('text="Login"')`.
### id, data-testid, data-test-id, data-test
Expand Down
4 changes: 4 additions & 0 deletions src/injected/textSelectorEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ function queryInternal(root: SelectorRoot, matcher: Matcher): Element | undefine
const node = walker.currentNode;
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as Element;
if ((element instanceof HTMLInputElement) && (element.type === 'submit' || element.type === 'button') && matcher(element.value))
return element;
if (element.shadowRoot)
shadowRoots.push(element.shadowRoot);
} else {
Expand All @@ -92,6 +94,8 @@ function queryAllInternal(root: SelectorRoot, matcher: Matcher, result: Element[
const node = walker.currentNode;
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as Element;
if ((element instanceof HTMLInputElement) && (element.type === 'submit' || element.type === 'button') && matcher(element.value))
result.push(element);
if (element.shadowRoot)
shadowRoots.push(element.shadowRoot);
} else {
Expand Down
6 changes: 6 additions & 0 deletions test/queryselector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ module.exports.describe = function({testRunner, expect, playwright, FFOX, CHROMI
expect(await page.$(`text="with"`)).toBe(null);
});

it('should match input[type=button|submit]', async({page}) => {
await page.setContent(`<input type="submit" value="hello"><input type="button" value="world">`);
expect(await page.$eval(`text=hello`, e => e.outerHTML)).toBe('<input type="submit" value="hello">');
expect(await page.$eval(`text=world`, e => e.outerHTML)).toBe('<input type="button" value="world">');
});

it('should work for open shadow roots', async({page, server}) => {
await page.goto(server.PREFIX + '/deep-shadow.html');
expect(await page.$eval(`text=root1`, e => e.outerHTML)).toBe('<span>Hello from root1</span>');
Expand Down

0 comments on commit 270206e

Please sign in to comment.