Package com.microsoft.playwright.impl
Class SharedSelectors
java.lang.Object
com.microsoft.playwright.impl.LoggingSupport
com.microsoft.playwright.impl.SharedSelectors
- All Implemented Interfaces:
Selectors
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface com.microsoft.playwright.Selectors
Selectors.RegisterOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<SelectorsImpl> private final List<SharedSelectors.Registration> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidaddChannel(SelectorsImpl channel) voidregister(String name, String script, Selectors.RegisterOptions options) Selectors must be registered before creating the page.voidregister(String name, Path path, Selectors.RegisterOptions options) Selectors must be registered before creating the page.private voidregisterImpl(String name, String script, Selectors.RegisterOptions options) (package private) voidremoveChannel(SelectorsImpl channel) voidsetTestIdAttribute(String attributeName) Defines custom attribute name to be used inPage.getByTestId().Methods inherited from class com.microsoft.playwright.impl.LoggingSupport
logApi, logApiIfEnabled, logWithTimestamp, withLogging, withLogging
-
Field Details
-
channels
-
registrations
-
-
Constructor Details
-
SharedSelectors
public SharedSelectors()
-
-
Method Details
-
register
Description copied from interface:SelectorsSelectors must be registered before creating the page.**Usage**
An example of registering selector engine that queries elements based on a tag name:
// Script that evaluates to a selector engine instance. The script is evaluated in the page context. String createTagNameEngine = "{\n" + " // Returns the first element matching given selector in the root's subtree.\n" + " query(root, selector) {\n" + " return root.querySelector(selector);\n" + " },\n" + " // Returns all elements matching given selector in the root's subtree.\n" + " queryAll(root, selector) {\n" + " return Array.from(root.querySelectorAll(selector));\n" + " }\n" + "}"; // Register the engine. Selectors will be prefixed with "tag=". playwright.selectors().register("tag", createTagNameEngine); Browser browser = playwright.firefox().launch(); Page page = browser.newPage(); page.setContent("<div><button>Click me</button></div>"); // Use the selector prefixed with its name. Locator button = page.locator("tag=button"); // Combine it with built-in locators. page.locator("tag=div").getByText("Click me").click(); // Can use it in any methods supporting selectors. int buttonCount = (int) page.locator("tag=button").count(); browser.close();- Specified by:
registerin interfaceSelectors- Parameters:
name- Name that is used in selectors as a prefix, e.g.{name: 'foo'}enablesfoo=myselectorbodyselectors. May only contain[a-zA-Z0-9_]characters.script- Script that evaluates to a selector engine instance. The script is evaluated in the page context.
-
register
Description copied from interface:SelectorsSelectors must be registered before creating the page.**Usage**
An example of registering selector engine that queries elements based on a tag name:
// Script that evaluates to a selector engine instance. The script is evaluated in the page context. String createTagNameEngine = "{\n" + " // Returns the first element matching given selector in the root's subtree.\n" + " query(root, selector) {\n" + " return root.querySelector(selector);\n" + " },\n" + " // Returns all elements matching given selector in the root's subtree.\n" + " queryAll(root, selector) {\n" + " return Array.from(root.querySelectorAll(selector));\n" + " }\n" + "}"; // Register the engine. Selectors will be prefixed with "tag=". playwright.selectors().register("tag", createTagNameEngine); Browser browser = playwright.firefox().launch(); Page page = browser.newPage(); page.setContent("<div><button>Click me</button></div>"); // Use the selector prefixed with its name. Locator button = page.locator("tag=button"); // Combine it with built-in locators. page.locator("tag=div").getByText("Click me").click(); // Can use it in any methods supporting selectors. int buttonCount = (int) page.locator("tag=button").count(); browser.close();- Specified by:
registerin interfaceSelectors- Parameters:
name- Name that is used in selectors as a prefix, e.g.{name: 'foo'}enablesfoo=myselectorbodyselectors. May only contain[a-zA-Z0-9_]characters.path- Script that evaluates to a selector engine instance. The script is evaluated in the page context.
-
setTestIdAttribute
Description copied from interface:SelectorsDefines custom attribute name to be used inPage.getByTestId().data-testidis used by default.- Specified by:
setTestIdAttributein interfaceSelectors- Parameters:
attributeName- Test id attribute name.
-
addChannel
-
removeChannel
-
registerImpl
-