docs: custom selectors registration (#20687)

https://github.com/microsoft/playwright/issues/20424
This commit is contained in:
Yury Semikhatsky 2023-02-06 13:32:00 -08:00 committed by GitHub
parent 9303dd552d
commit 86dd29a15e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 27 deletions

View File

@ -7,6 +7,8 @@ information.
## async method: Selectors.register ## async method: Selectors.register
* since: v1.8 * since: v1.8
Selectors must be registered before creating the page.
**Usage** **Usage**
An example of registering selector engine that queries elements based on a tag name: An example of registering selector engine that queries elements based on a tag name:
@ -37,8 +39,8 @@ const { selectors, firefox } = require('playwright'); // Or 'chromium' or 'webk
// Use the selector prefixed with its name. // Use the selector prefixed with its name.
const button = page.locator('tag=button'); const button = page.locator('tag=button');
// Combine it with other selector engines. // We can combine it with built-in locators.
await page.locator('tag=div >> text="Click me"').click(); await page.locator('tag=div').getByText('Click me').click();
// Can use it in any methods supporting selectors. // Can use it in any methods supporting selectors.
const buttonCount = await page.locator('tag=button').count(); const buttonCount = await page.locator('tag=button').count();
@ -65,8 +67,8 @@ Page page = browser.newPage();
page.setContent("<div><button>Click me</button></div>"); page.setContent("<div><button>Click me</button></div>");
// Use the selector prefixed with its name. // Use the selector prefixed with its name.
Locator button = page.locator("tag=button"); Locator button = page.locator("tag=button");
// Combine it with other selector engines. // Combine it with built-in locators.
page.locator("tag=div >> text=\"Click me\"").click(); page.locator("tag=div").getByText("Click me").click();
// Can use it in any methods supporting selectors. // Can use it in any methods supporting selectors.
int buttonCount = (int) page.locator("tag=button").count(); int buttonCount = (int) page.locator("tag=button").count();
browser.close(); browser.close();
@ -97,8 +99,8 @@ async def run(playwright):
# Use the selector prefixed with its name. # Use the selector prefixed with its name.
button = await page.query_selector('tag=button') button = await page.query_selector('tag=button')
# Combine it with other selector engines. # Combine it with built-in locators.
await page.locator('tag=div >> text="Click me"').click() await page.locator('tag=div').get_by_text('Click me').click()
# Can use it in any methods supporting selectors. # Can use it in any methods supporting selectors.
button_count = await page.locator('tag=button').count() button_count = await page.locator('tag=button').count()
print(button_count) print(button_count)
@ -135,8 +137,8 @@ def run(playwright):
# Use the selector prefixed with its name. # Use the selector prefixed with its name.
button = page.locator('tag=button') button = page.locator('tag=button')
# Combine it with other selector engines. # Combine it with built-in locators.
page.locator('tag=div >> text="Click me"').click() page.locator('tag=div').get_by_text('Click me').click()
# Can use it in any methods supporting selectors. # Can use it in any methods supporting selectors.
button_count = page.locator('tag=button').count() button_count = page.locator('tag=button').count()
print(button_count) print(button_count)
@ -170,8 +172,8 @@ var page = await browser.NewPageAsync();
await page.SetContentAsync("<div><button>Click me</button></div>"); await page.SetContentAsync("<div><button>Click me</button></div>");
// Use the selector prefixed with its name. // Use the selector prefixed with its name.
var button = page.Locator("tag=button"); var button = page.Locator("tag=button");
// Combine it with other selector engines. // Combine it with built-in locators.
await page.Locator("tag=div >> text=\"Click me\"").ClickAsync(); await page.Locator("tag=div").GetByText("Click me").ClickAsync();
// Can use it in any methods supporting selectors. // Can use it in any methods supporting selectors.
int buttonCount = await page.Locator("tag=button").CountAsync(); int buttonCount = await page.Locator("tag=button").CountAsync();
``` ```

View File

@ -22,9 +22,13 @@ tampering with the global objects, for example altering `Node.prototype` methods
content scripts. Note that running as a content script is not guaranteed when the engine is used together with other content scripts. Note that running as a content script is not guaranteed when the engine is used together with other
custom engines. custom engines.
Selectors must be registered before creating the page.
An example of registering selector engine that queries elements based on a tag name: An example of registering selector engine that queries elements based on a tag name:
```js ```js
import { test, expect } from '@playwright/test';
// Must be a function that evaluates to a selector engine instance. // Must be a function that evaluates to a selector engine instance.
const createTagNameEngine = () => ({ const createTagNameEngine = () => ({
// Returns the first element matching given selector in the root's subtree. // Returns the first element matching given selector in the root's subtree.
@ -38,18 +42,23 @@ const createTagNameEngine = () => ({
} }
}); });
// Register the engine. Selectors will be prefixed with "tag=". // Register selectors before any page is created.
await selectors.register('tag', createTagNameEngine); test.beforeAll(async ({ playwright }) => {
// Register the engine. Selectors will be prefixed with "tag=".
await playwright.selectors.register('tag', createTagNameEngine);
});
// Now we can use 'tag=' selectors. test('selector engine test', async ({ page }) => {
const button = page.locator('tag=button'); // Now we can use 'tag=' selectors.
await button.click(); const button = page.locator('tag=button');
await button.click();
// We can combine it with other selector engines using `>>` combinator. // We can combine it with built-in locators.
await page.locator('tag=div >> span >> "Click me"').click(); await page.locator('tag=div').getByText('Click me').click();
// We can use it in any methods supporting selectors. // We can use it in any methods supporting selectors.
const buttonCount = await page.locator('tag=button').count(); await expect(page.locator('tag=button')).toHaveCount(3);
});
``` ```
```java ```java
@ -73,8 +82,8 @@ playwright.selectors().register("tag", createTagNameEngine);
Locator button = page.locator("tag=button"); Locator button = page.locator("tag=button");
button.click(); button.click();
// We can combine it with other selector engines using ">>" combinator. // We can combine it with built-in locators.
page.locator("tag=div >> span >> \"Click me\"").click(); page.locator("tag=div").getByText("Click me").click();
// We can use it in any methods supporting selectors. // We can use it in any methods supporting selectors.
int buttonCount = (int) page.locator("tag=button").count(); int buttonCount = (int) page.locator("tag=button").count();
@ -102,8 +111,8 @@ await playwright.selectors.register("tag", tag_selector)
button = page.locator("tag=button") button = page.locator("tag=button")
await button.click() await button.click()
# we can combine it with other selector engines using `>>` combinator. # we can combine it with built-in locators.
await page.locator("tag=div >> span >> \"click me\"").click() await page.locator("tag=div").get_by_text("click me").click()
# we can use it in any methods supporting selectors. # we can use it in any methods supporting selectors.
button_count = await page.locator("tag=button").count() button_count = await page.locator("tag=button").count()
@ -131,8 +140,8 @@ playwright.selectors.register("tag", tag_selector)
button = page.locator("tag=button") button = page.locator("tag=button")
button.click() button.click()
# we can combine it with other selector engines using `>>` combinator. # we can combine it with built-in locators.
page.locator("tag=div >> span >> \"click me\"").click() page.locator("tag=div").get_by_text("click me").click()
# we can use it in any methods supporting selectors. # we can use it in any methods supporting selectors.
button_count = page.locator("tag=button").count() button_count = page.locator("tag=button").count()

View File

@ -18043,6 +18043,8 @@ export interface Route {
*/ */
export interface Selectors { export interface Selectors {
/** /**
* Selectors must be registered before creating the page.
*
* **Usage** * **Usage**
* *
* An example of registering selector engine that queries elements based on a tag name: * An example of registering selector engine that queries elements based on a tag name:
@ -18073,8 +18075,8 @@ export interface Selectors {
* *
* // Use the selector prefixed with its name. * // Use the selector prefixed with its name.
* const button = page.locator('tag=button'); * const button = page.locator('tag=button');
* // Combine it with other selector engines. * // We can combine it with built-in locators.
* await page.locator('tag=div >> text="Click me"').click(); * await page.locator('tag=div').getByText('Click me').click();
* // Can use it in any methods supporting selectors. * // Can use it in any methods supporting selectors.
* const buttonCount = await page.locator('tag=button').count(); * const buttonCount = await page.locator('tag=button').count();
* *