Locator
Locator is the core API for element querying and interaction in Browser Mode. You can build query chains via page.getBy* or page.locator(), then execute interaction actions. Concrete execution semantics are provided by the configured browser provider.
With the Playwright provider, Locator interaction methods (such as click, fill, check) automatically wait for the element to become actionable (visible, enabled, stable) before executing. You do not need to manually wait for elements before performing actions. This is distinct from the auto-retry behavior of expect.element assertions.
The following example demonstrates the typical workflow: query elements, perform interactions, and combine with expect.element for assertions.
page
- Type:
BrowserPage
page is the starting point in tests: first use page to locate elements, then execute interactions and assertions on the returned Locator.
page is a query-only object: it only creates Locator instances and does not directly execute interaction actions.
The submitButton above is a Locator. You can continue chaining calls on it (e.g., .click()) or pass it to expect.element(...) for assertions.
BrowserPage
- Type:
Pick<Locator, 'locator' | 'getByRole' | 'getByText' | 'getByLabel' | 'getByPlaceholder' | 'getByAltText' | 'getByTitle' | 'getByTestId'>
BrowserPage is the type definition for page. It only exposes query entry points and does not include interaction methods like click, fill, etc.
This means you need to first obtain a Locator through page, then call interactions and assertions on the Locator.
Query API
All the following APIs return a new Locator and support further chaining.
locator
- Type:
(selector: string) => Locator
Query elements by CSS selector.
getByRole
- Type:
(role: string, options?: LocatorGetByRoleOptions) => Locator
Query elements by semantic role. Recommended as the first choice.
LocatorGetByRoleOptions supports: name, exact, checked, disabled, expanded, selected, pressed, includeHidden, level.
getByText
- Type:
(text: string | RegExp, options?: { exact?: boolean }) => Locator
Query by visible text.
getByLabel
- Type:
(text: string | RegExp, options?: { exact?: boolean }) => Locator
Query by form label.
getByPlaceholder
- Type:
(text: string | RegExp, options?: { exact?: boolean }) => Locator
Query by placeholder.
getByAltText
- Type:
(text: string | RegExp, options?: { exact?: boolean }) => Locator
Query by alt text.
getByTitle
- Type:
(text: string | RegExp, options?: { exact?: boolean }) => Locator
Query by title.
getByTestId
- Type:
(text: string | RegExp) => Locator
Query by test id.
Configuration API
setTestIdAttribute
- Type:
(attribute: string) => Promise<void>
Set the attribute name used by getByTestId(). The default value is data-testid.
This configuration is forwarded through the Browser Mode channel to the host provider (e.g., Playwright's selectors.setTestIdAttribute()).
This is a global configuration. It is recommended to set it once during the test setup phase to avoid inconsistent query behavior caused by mid-test modifications.
It is recommended to configure setTestIdAttribute in a setup file to ensure it applies to all tests consistently:
Then reference it in your config:
Composition API
filter
- Type:
(options: LocatorFilterOptions) => Locator
LocatorFilterOptions supports:
hasText?: string | RegExp: Keep elements matching the texthasNotText?: string | RegExp: Exclude elements matching the texthas?: Locator: Keep elements containing the child LocatorhasNot?: Locator: Exclude elements containing the child Locator
Used for secondary filtering on existing query results.
and / or
- Type:
(other: Locator) => Locator
Combine two Locator conditions.
nth / first / last
- Type:
nth(index: number): Locatorfirst(): Locatorlast(): Locator
Select a specific element from the matched set.
Interaction API
The following APIs trigger actual browser actions and return Promise<void>.
These *Options types represent the set of options currently supported in Browser Mode:
LocatorClickOptions/LocatorDblclickOptions:button,delay,force,modifiers,position,timeout,trial(clickadditionally supportsclickCount)LocatorHoverOptions:force,modifiers,position,timeout,trialLocatorPressOptions:delay,timeoutLocatorFillOptions:force,timeoutLocatorCheckOptions:force,position,timeout,trialLocatorFocusOptions/LocatorBlurOptions/LocatorScrollIntoViewIfNeededOptions:timeoutLocatorWaitForOptions:state(attached/detached/visible/hidden) andtimeoutLocatorSelectOptionOptions:force,timeoutLocatorSetInputFilesOptions:timeout
click
- Type:
(options?: LocatorClickOptions) => Promise<void>
Click the element matched by the current Locator.
dblclick
- Type:
(options?: LocatorDblclickOptions) => Promise<void>
Double-click the element.
hover
- Type:
(options?: LocatorHoverOptions) => Promise<void>
Hover the mouse over the element, commonly used to trigger hover menus or tooltips.
press
- Type:
(key: string, options?: LocatorPressOptions) => Promise<void>
Send a keyboard key press on the element.
fill
- Type:
(value: string, options?: LocatorFillOptions) => Promise<void>
Set the value of an input field. Applicable to input, textarea, and other editable elements.
clear
- Type:
() => Promise<void>
Clear the value of the current input element.
focus
- Type:
(options?: LocatorFocusOptions) => Promise<void>
Focus the element.
blur
- Type:
(options?: LocatorBlurOptions) => Promise<void>
Remove focus from the element.
check
- Type:
(options?: LocatorCheckOptions) => Promise<void>
Check a checkbox or radio button.
uncheck
- Type:
(options?: LocatorCheckOptions) => Promise<void>
Uncheck a checkbox.
scrollIntoViewIfNeeded
- Type:
(options?: LocatorScrollIntoViewIfNeededOptions) => Promise<void>
Scroll the page if needed to bring the element into the visible area.
waitFor
- Type:
(options?: LocatorWaitForOptions) => Promise<void>
Wait until the Locator meets the specified condition before continuing execution. Useful for handling async rendering.
dispatchEvent
- Type:
(type: string, eventInit?: LocatorDispatchEventInit) => Promise<void>
Dispatch a custom or native event on the element.
selectOption
- Type:
(value: string | string[], options?: LocatorSelectOptionOptions) => Promise<void>
Select an option of a select element. Currently only supports string or string[] as the value.
setInputFiles
- Type:
(files: string | string[], options?: LocatorSetInputFilesOptions) => Promise<void>
Set files for an input[type="file"]. Currently only supports file paths as string or string[].
Usage constraints
- The APIs listed on this page represent the currently supported subset of the Playwright Locator API. APIs not listed here are not yet available
- The argument to
and/or/filter({ has | hasNot })must be aLocatorreturned by@rstest/browser - The
typeargument todispatchEvent(type, eventInit?)must be a non-empty string selectOptioncurrently only supportsstringorstring[]setInputFilescurrently only supports file paths asstringorstring[]- Some parameters are transmitted through the Browser Mode communication channel; it is recommended to keep them JSON-serializable
Using with expect.element
Locator is typically used together with expect.element(locator). For the full list of assertions, see Assertion.