Registration
add(path) returns a registration: the ten locator methods, one of which registers the path, then the steps
filter, nth and describe. Its type is not exported. It ends at the next call on the registry, after which every
method of it throws; see
registered locators do not change.
registry.add("main").locator("main");registry.add("main.list").locator("ul.orders");registry.add("main.list.item").getByRole("listitem").filter({ hasText: "Open" }).nth(0);// main.list.item: locator('main').locator('ul.orders').getByRole('listitem').filter({ hasText: 'Open' }).first()The chains on this page are shown without the description, which is the path unless describe sets one. Guides:
registering locators, filters and indices,
reusing locators.
Locator methods
Section titled “Locator methods”Each takes the arguments of the Playwright method it is named after and returns the registration, for filter, nth
and describe. Each throws at the call, naming the method and the path, when its first argument is missing (from
JavaScript) or is an empty string where Playwright cannot use one, when the path already has a locator, when the
registration has ended, and, from JavaScript, when the path breaks the path rules:
getByRole requires a role value for "main.button@save".A locator schema with the path "main" already exists.A locator definition for "main" has already been provided; only one locator type can be set for a registration.getByRole() cannot apply to "main.button@save": its registration has ended; give a registration all its steps in its add(…) chain, or query the path for one use.LocatorSchemaPath string cannot contain consecutive dots: main..listThe second message goes on with the existing and the attempted locator; the third is a second locator method on one registration; the fourth a locator method of a registration kept in a variable after the registry was called again.
frameLocator()
Section titled “frameLocator()”Registers a frame segment: with a selector, the <iframe> element, whose descendants resolve inside the frame; without
one, Playwright’s any-frame page.frameLocator(). See
frames and
any frame.
Usage
registry.add("shell").locator("div.shell");registry.add("shell.frame@payment").frameLocator("iframe#payment");registry.add("shell.frame@payment.button@pay").getByRole("button", { name: "Pay" });// locator('div.shell').locator('iframe#payment').contentFrame().getByRole('button', { name: 'Pay' })
registry.add("anyFrame").frameLocator();registry.add("anyFrame.button@accept").getByRole("button", { name: "Accept" });// frameLocator().getByRole('button', { name: 'Accept' })Arguments
-
selectorstring (optional)The
<iframe>element’s selector. Without it, the any-frame form, which must be the first segment of its chain and takes no steps.
Returns
- the registration, with a selector;
undefinedwithout one
Throws
frameLocator requires a non-empty selector for "shell.frame".An any-frame segment that is not the first segment of its chain fails verify()
and every resolution through it:
frameLocator() without a selector must be the first segment of the chain; "shell.anyFrame" has the ancestor "shell".getByAltText()
Section titled “getByAltText()”Registers the path’s locator as Playwright’s
page.getByAltText(): an element, usually an image,
by its alt text.
Usage
registry.add("header.image@logo").getByAltText("Shop logo");// getByAltText('Shop logo')Arguments
textstring | RegExpoptionsObject (optional): Playwright’s options,exact.
An empty string is a value Playwright matches and is stored as given.
Returns
- the registration
getById()
Section titled “getById()”Registers a locator by the id attribute, which Playwright has no method for: a string as written, a RegExp with its
flags. See locating by id.
Usage
registry.add("main.panel@settings").getById("settings.panel");// selector: [id="settings.panel"]registry.add("main.button@submit").getById(/^button\.submit\.[a-z0-9]{4}$/i);// selector: internal:attr=[id=/^button\.submit\.[a-z0-9]{4}$/i]Arguments
-
idstring | RegExpThe id as written, without a
#; or a pattern, unanchored unless you anchor it.
Returns
- the registration
Throws
getById requires a non-empty id for "main.panel@settings".getByLabel()
Section titled “getByLabel()”Registers the path’s locator as Playwright’s
page.getByLabel(): an input by its <label>,
aria-labelledby or aria-label.
Usage
registry.add("main.form.input@email").getByLabel("Email", { exact: true });// getByLabel('Email', { exact: true })Arguments
textstring | RegExpoptionsObject (optional): Playwright’s options,exact.
Returns
- the registration
getByPlaceholder()
Section titled “getByPlaceholder()”Registers the path’s locator as Playwright’s
page.getByPlaceholder(): an input by its
placeholder.
Usage
registry.add("main.input@search").getByPlaceholder("Search");// getByPlaceholder('Search')Arguments
textstring | RegExpoptionsObject (optional): Playwright’s options,exact.
Returns
- the registration
getByRole()
Section titled “getByRole()”Registers the path’s locator as Playwright’s
page.getByRole(): by ARIA role, accessible name and
state. Which strings are roles is Playwright’s own type, with completion in the editor.
Usage
registry.add("main.button@save").getByRole("button", { name: "Save" });// getByRole('button', { name: 'Save' })Arguments
-
rolestringAn ARIA role; an empty one is rejected.
-
optionsObject (optional): Playwright’sgetByRoleoptions, such asname,exactandlevel.
Returns
- the registration
Throws
getByRole requires a non-empty role for "main.heading".getByTestId()
Section titled “getByTestId()”Registers the path’s locator as Playwright’s
page.getByTestId(), by the attribute the project’s
testIdAttribute names.
Usage
registry.add("main.row@order").getByTestId("order-row");// getByTestId('order-row')Arguments
-
testIdstring | RegExpAn empty string is a value Playwright matches (
data-testid="") and is stored as given.
Returns
- the registration
getByText()
Section titled “getByText()”Registers the path’s locator as Playwright’s
page.getByText(): by the text an element contains.
Usage
registry.add("main.alert@error").getByText(/failed/i);// getByText(/failed/i)Arguments
textstring | RegExpoptionsObject (optional): Playwright’s options,exact.
An empty string is a value Playwright matches and is stored as given.
Returns
- the registration
getByTitle()
Section titled “getByTitle()”Registers the path’s locator as Playwright’s
page.getByTitle(): an element by its title
attribute.
Usage
registry.add("main.icon@info").getByTitle("More information");// getByTitle('More information')Arguments
textstring | RegExpoptionsObject (optional): Playwright’s options,exact.
Returns
- the registration
locator()
Section titled “locator()”Registers the path’s locator as Playwright’s page.locator():
by a CSS or other Playwright selector.
Usage
registry.add("main.list").locator("ul.orders");// locator('ul.orders')Arguments
-
selectorstringAn empty selector is rejected: Playwright cannot parse it.
-
optionsObject (optional): Playwright’slocatoroptions, such ashasTextandhas.
Returns
- the registration
Throws
locator requires a non-empty selector for "main.list".Available after a locator method, chained in any order and number, and applied in that order. A step of a registration kept in a variable, after the registry was called again, throws:
nth() cannot apply to "main.list.item": its registration has ended; give a registration all its steps in its add(…) chain, or query the path for one use.From JavaScript, a step before the locator method throws:
filter() needs a locator strategy first for "main.list.item"; call getByRole, getByText, getByLabel, getByPlaceholder, getByAltText, getByTitle, locator, frameLocator, getByTestId or getById before it.describe()
Section titled “describe()”Sets the description of the resolved locator, instead of the path, as
locator.describe() does; "" removes it. See
descriptions.
Usage
registry.add("main.list.item@first").getByRole("listitem").nth("first").describe("First order");Arguments
descriptionstring
Returns
- the registration
Throws
describe requires a string for "main.list.item@first"; pass "" to remove the description.filter()
Section titled “filter()”Adds a locator.filter() step; has and hasNot also
take a registered path. See filters.
Usage
registry.add("main.section@orders.heading").getByRole("heading", { name: "Orders" });registry.add("main.section@orders").locator("section").filter({ has: "main.section@orders.heading" });Arguments
-
filterFilterDefinitionPlaywright’s filter options,
hasText,hasNotText,has,hasNotandvisible, with a registered path allowed forhasandhasNot.
Returns
- the registration
A has path that is not registered, or a cycle of has paths, fails
verify().
Adds an index step: "first" and "last" become Playwright’s
first() and
last(), and a number goes to
nth() as given (-1 is the last element).
Usage
registry.add("main.list.item@last").getByRole("listitem").nth("last");// getByRole('listitem').last()Arguments
indexnumber | “first” | “last”
Returns
- the registration
After { reuse }
Section titled “After { reuse }”add(path, { reuse }) registers the path at once. What it returns depends on what is reused.
A registered path
Section titled “A registered path”The path is a copy of the reused one, which filter, nth and describe narrow; there is no locator method. See
reuse by path.
registry.add("main").locator("main");registry.add("main.list").locator("ul");registry.add("main.list.items@open").getByRole("listitem").filter({ hasText: "Open" });registry.add("main.list.item@lastOpen", { reuse: "main.list.items@open" }).nth("last").describe("Last open item");// locator('main').locator('ul').getByRole('listitem').filter({ hasText: 'Open' }).last()A seed
Section titled “A seed”The seed is registered as it is. Its own locator method may patch it once, as
update() does, then come filter, nth and describe. See
changing a seed’s options.
const saveButton = registry.createReusable.getByRole("button", { name: "Save", exact: true });
registry.add("main.button@cancel", { reuse: saveButton }).getByRole({ name: "Cancel" });// getByRole('button', { name: 'Cancel', exact: true })registry.add("main.link@plain", { reuse: saveButton }).getByRole("link", null);// getByRole('link')Throws
The locator definition for "main.button@cancel" must use the "role" strategy when reusing a locator.A locator definition for "main.link@help" was already provided from reuse; only one matching override is allowed.The first for another strategy’s method, the second for a second change on a registration kept in a variable.