docs(extensibility): make selectors.register per worker (#26518)

Fixes https://github.com/microsoft/playwright/issues/26493
This commit is contained in:
Max Schmitt 2023-08-17 18:16:57 +02:00 committed by GitHub
parent adc9b2d390
commit 5bcd1fb65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,8 +26,11 @@ 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
import { test, expect } from '@playwright/test'; ```js title="baseTest.ts"
import { test as base } from '@playwright/test';
export { 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 = () => ({
@ -42,11 +45,18 @@ const createTagNameEngine = () => ({
} }
}); });
// Register selectors before any page is created. export const test = base.extend<{}, { selectorRegistration: void }>({
test.beforeAll(async ({ playwright }) => { // Register selectors once per worker.
// Register the engine. Selectors will be prefixed with "tag=". selectorRegistration: [async ({ playwright }, use) => {
await playwright.selectors.register('tag', createTagNameEngine); // Register the engine. Selectors will be prefixed with "tag=".
await playwright.selectors.register('tag', createTagNameEngine);
await use();
}, { scope: 'worker', auto: true }],
}); });
```
```js title="example.spec.ts"
import { test, expect } from './baseTest';
test('selector engine test', async ({ page }) => { test('selector engine test', async ({ page }) => {
// Now we can use 'tag=' selectors. // Now we can use 'tag=' selectors.