Package com.microsoft.playwright.impl
Class SharedSelectors
- java.lang.Object
-
- com.microsoft.playwright.impl.LoggingSupport
-
- com.microsoft.playwright.impl.SharedSelectors
-
- All Implemented Interfaces:
Selectors
public class SharedSelectors extends LoggingSupport implements Selectors
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSharedSelectors.Registration-
Nested classes/interfaces inherited from interface com.microsoft.playwright.Selectors
Selectors.RegisterOptions
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<SelectorsImpl>channelsprivate java.util.List<SharedSelectors.Registration>registrations
-
Constructor Summary
Constructors Constructor Description SharedSelectors()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddChannel(SelectorsImpl channel)voidregister(java.lang.String name, java.lang.String script, Selectors.RegisterOptions options)Selectors must be registered before creating the page.voidregister(java.lang.String name, java.nio.file.Path path, Selectors.RegisterOptions options)Selectors must be registered before creating the page.private voidregisterImpl(java.lang.String name, java.lang.String script, Selectors.RegisterOptions options)(package private) voidremoveChannel(SelectorsImpl channel)voidsetTestIdAttribute(java.lang.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 Detail
-
channels
private final java.util.List<SelectorsImpl> channels
-
registrations
private final java.util.List<SharedSelectors.Registration> registrations
-
-
Method Detail
-
register
public void register(java.lang.String name, java.lang.String script, Selectors.RegisterOptions options)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
public void register(java.lang.String name, java.nio.file.Path path, Selectors.RegisterOptions options)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
public void setTestIdAttribute(java.lang.String attributeName)
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
void addChannel(SelectorsImpl channel)
-
removeChannel
void removeChannel(SelectorsImpl channel)
-
registerImpl
private void registerImpl(java.lang.String name, java.lang.String script, Selectors.RegisterOptions options)
-
-