Skip to content

Commit 3347f83

Browse files
authored
feat(browser): introduce built-in locators (#6084)
1 parent 0453c5a commit 3347f83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1975
-392
lines changed

docs/.vitepress/config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ export default ({ mode }: { mode: string }) => {
185185
link: '/guide/browser/interactivity-api',
186186
docFooterText: 'Interactivity API | Browser Mode',
187187
},
188+
{
189+
text: 'Locators',
190+
link: '/guide/browser/locators',
191+
docFooterText: 'Locators | Browser Mode',
192+
},
188193
{
189194
text: 'Assertion API',
190195
link: '/guide/browser/assertion-api',

docs/config/index.md

+11
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,17 @@ Should Vitest UI be injected into the page. By default, injects UI iframe during
16671667

16681668
Default iframe's viewport.
16691669

1670+
#### browser.locators {#browser-locators}
1671+
1672+
Options for built-in [browser locators](/guide/browser/locators).
1673+
1674+
##### browser.locators.testIdAttribute
1675+
1676+
- **Type:** `string`
1677+
- **Default:** `data-testid`
1678+
1679+
Attribute used to find elements with `getByTestId` locator.
1680+
16701681
#### browser.screenshotDirectory {#browser-screenshotdirectory}
16711682

16721683
- **Type:** `string`

docs/guide/browser/context.md

+23
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,32 @@ export const page: {
7878
base64: string
7979
}>
8080
screenshot(options?: ScreenshotOptions): Promise<string>
81+
/**
82+
* Extend default `page` object with custom methods.
83+
*/
84+
extend(methods: Partial<BrowserPage>): BrowserPage
85+
/**
86+
* Wrap an HTML element in a `Locator`. When querying for elements, the search will always return this element.
87+
*/
88+
elementLocator(element: Element): Locator
89+
90+
/**
91+
* Locator APIs. See its documentation for more details.
92+
*/
93+
getByRole(role: ARIARole | string, options?: LocatorByRoleOptions): Locator
94+
getByLabelText(text: string | RegExp, options?: LocatorOptions): Locator
95+
getByTestId(text: string | RegExp): Locator
96+
getByAltText(text: string | RegExp, options?: LocatorOptions): Locator
97+
getByPlaceholder(text: string | RegExp, options?: LocatorOptions): Locator
98+
getByText(text: string | RegExp, options?: LocatorOptions): Locator
99+
getByTitle(text: string | RegExp, options?: LocatorOptions): Locator
81100
}
82101
```
83102
103+
::: tip
104+
The `getBy*` API is explained at [Locators API](/guide/browser/locators).
105+
:::
106+
84107
## `cdp`
85108
86109
The `cdp` export returns the current Chrome DevTools Protocol session. It is mostly useful to library authors to build tools on top of it.

docs/guide/browser/interactivity-api.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ This behaviour is more useful because we do not emulate the keyboard, we actuall
6464

6565
## userEvent.click
6666

67-
- **Type:** `(element: Element, options?: UserEventClickOptions) => Promise<void>`
67+
- **Type:** `(element: Element | Locator, options?: UserEventClickOptions) => Promise<void>`
6868

6969
Click on an element. Inherits provider's options. Please refer to your provider's documentation for detailed explanation about how this method works.
7070

@@ -87,7 +87,7 @@ References:
8787

8888
## userEvent.dblClick
8989

90-
- **Type:** `(element: Element, options?: UserEventDoubleClickOptions) => Promise<void>`
90+
- **Type:** `(element: Element | Locator, options?: UserEventDoubleClickOptions) => Promise<void>`
9191

9292
Triggers a double click event on an element.
9393

@@ -112,7 +112,7 @@ References:
112112

113113
## userEvent.tripleClick
114114

115-
- **Type:** `(element: Element, options?: UserEventTripleClickOptions) => Promise<void>`
115+
- **Type:** `(element: Element | Locator, options?: UserEventTripleClickOptions) => Promise<void>`
116116

117117
Triggers a triple click event on an element. Since there is no `tripleclick` in browser api, this method will fire three click events in a row, and so you must check [click event detail](https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event#usage_notes) to filter the event: `evt.detail === 3`.
118118

@@ -144,7 +144,7 @@ References:
144144

145145
## userEvent.fill
146146

147-
- **Type:** `(element: Element, text: string) => Promise<void>`
147+
- **Type:** `(element: Element | Locator, text: string) => Promise<void>`
148148

149149
Set a value to the `input/textarea/conteneditable` field. This will remove any existing text in the input before setting the new value.
150150

@@ -234,7 +234,7 @@ References:
234234

235235
## userEvent.type
236236

237-
- **Type:** `(element: Element, text: string, options?: UserEventTypeOptions) => Promise<void>`
237+
- **Type:** `(element: Element | Locator, text: string, options?: UserEventTypeOptions) => Promise<void>`
238238

239239
::: warning
240240
If you don't rely on [special characters](https://testing-library.com/docs/user-event/keyboard) (e.g., `{shift}` or `{selectall}`), it is recommended to use [`userEvent.fill`](#userevent-fill) instead.
@@ -267,7 +267,7 @@ References:
267267

268268
## userEvent.clear
269269

270-
- **Type:** `(element: Element) => Promise<void>`
270+
- **Type:** `(element: Element | Locator) => Promise<void>`
271271

272272
This method clears the input element content.
273273

@@ -294,7 +294,7 @@ References:
294294

295295
## userEvent.selectOptions
296296

297-
- **Type:** `(element: Element, values: HTMLElement | HTMLElement[] | string | string[], options?: UserEventSelectOptions) => Promise<void>`
297+
- **Type:** `(element: Element | Locator, values: HTMLElement | HTMLElement[] | Locator | Locator[] | string | string[], options?: UserEventSelectOptions) => Promise<void>`
298298

299299
The `userEvent.selectOptions` allows selecting a value in a `<select>` element.
300300

@@ -337,7 +337,7 @@ References:
337337

338338
## userEvent.hover
339339

340-
- **Type:** `(element: Element, options?: UserEventHoverOptions) => Promise<void>`
340+
- **Type:** `(element: Element | Locator, options?: UserEventHoverOptions) => Promise<void>`
341341

342342
This method moves the cursor position to the selected element. Please refer to your provider's documentation for detailed explanation about how this method works.
343343

@@ -366,7 +366,7 @@ References:
366366

367367
## userEvent.unhover
368368

369-
- **Type:** `(element: Element, options?: UserEventHoverOptions) => Promise<void>`
369+
- **Type:** `(element: Element | Locator, options?: UserEventHoverOptions) => Promise<void>`
370370

371371
This works the same as [`userEvent.hover`](#userevent-hover), but moves the cursor to the `document.body` element instead.
372372

@@ -393,7 +393,7 @@ References:
393393

394394
## userEvent.dragAndDrop
395395

396-
- **Type:** `(source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>`
396+
- **Type:** `(source: Element | Locator, target: Element | Locator, options?: UserEventDragAndDropOptions) => Promise<void>`
397397

398398
Drags the source element on top of the target element. Don't forget that the `source` element has to have the `draggable` attribute set to `true`.
399399

0 commit comments

Comments
 (0)