Selectors
セレクターは、カスタムセレクターエンジンをインストールするために使用できます。拡張性を参照してください。
メソッド
register
v1.9より前に追加セレクターは、ページを作成する前に登録する必要があります。
使用法
タグ名に基づいて要素をクエリするセレクターエンジンの登録例
const { selectors, firefox } = require('@playwright/test'); // Or 'chromium' or 'webkit'.
(async () => {
// Must be a function that evaluates to a selector engine instance.
const createTagNameEngine = () => ({
// Returns the first element matching given selector in the root's subtree.
query(root, selector) {
return root.querySelector(selector);
},
// Returns all elements matching given selector in the root's subtree.
queryAll(root, selector) {
return Array.from(root.querySelectorAll(selector));
}
});
// Register the engine. Selectors will be prefixed with "tag=".
await selectors.register('tag', createTagNameEngine);
const browser = await firefox.launch();
const page = await browser.newPage();
await page.setContent(`<div><button>Click me</button></div>`);
// Use the selector prefixed with its name.
const button = page.locator('tag=button');
// We can combine it with built-in locators.
await page.locator('tag=div').getByText('Click me').click();
// Can use it in any methods supporting selectors.
const buttonCount = await page.locator('tag=button').count();
await browser.close();
})();
引数
-
セレクター内でプレフィックスとして使用される名前です。例:
{name: 'foo'}はfoo=myselectorbodyセレクターを有効にします。[a-zA-Z0-9_]文字のみを含めることができます。 -
scriptfunction | string | Object#-
pathstring (optional)JavaScriptファイルへのパス。
pathが相対パスの場合、現在の作業ディレクトリを基準に解決されます。オプションです。 -
contentstring (optional)スクリプトの生のコンテンツ。オプションです。
セレクターエンジンのインスタンスに評価されるスクリプト。スクリプトはページコンテキストで評価されます。
-
-
optionsObject (optional)
戻り値
setTestIdAttribute
追加バージョン: v1.27page.getByTestId() で使用するカスタム属性名を定義します。デフォルトでは data-testid が使用されます。
使用法
selectors.setTestIdAttribute(attributeName);
引数