Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(browser): introduce built-in locators #6084

Merged
merged 52 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
a6e94eb
feat(browser): introduce built-in locators
sheremet-va Jul 10, 2024
9e2b52f
feat: support chaining
sheremet-va Jul 10, 2024
68884d8
refactor: element is private
sheremet-va Jul 10, 2024
cf6074c
chore: better typing, support webdriverio
sheremet-va Jul 10, 2024
1d6195d
chore: include playwright locator parser
sheremet-va Jul 11, 2024
a690d5b
chore: require element() method on a locator
sheremet-va Jul 11, 2024
e34ba05
chore: cleanup
sheremet-va Jul 11, 2024
08885c3
chore: receive element
sheremet-va Jul 11, 2024
649b1d4
chore: cleanup
sheremet-va Jul 11, 2024
8da8a1c
chore: cleanup
sheremet-va Jul 11, 2024
f4995a1
chore: allow options in every locator
sheremet-va Jul 11, 2024
7b12d9d
refactor: cleanup, always use playwright locators
sheremet-va Jul 12, 2024
3c70e9d
feat: add all() method
sheremet-va Jul 12, 2024
9d942d1
chore: cleanup
sheremet-va Jul 12, 2024
00c8ec4
chore: fix selector
sheremet-va Jul 12, 2024
f6474eb
feat: inject locators
sheremet-va Jul 12, 2024
526354b
test: add simple blog post test
sheremet-va Jul 12, 2024
8fe6236
refactor: cleanup
sheremet-va Jul 12, 2024
03dc368
chore: cleanup
sheremet-va Jul 12, 2024
62ed228
feat: support aria role types
sheremet-va Jul 12, 2024
a0ac650
feat: support cusotm testId and selectOptions on a locator
sheremet-va Jul 12, 2024
8387241
perf: store an element if it locator is created from `elementLocator`
sheremet-va Jul 12, 2024
d2e7607
test: fix tests number
sheremet-va Jul 12, 2024
869938e
test: move locators test to the fixtures folder
sheremet-va Jul 12, 2024
1b5a6b4
chore: cleanup
sheremet-va Jul 12, 2024
6b70b91
test: run locators tests
sheremet-va Jul 12, 2024
4be6403
chore: prefer css selectors in webdriverio/preview elementLocator
sheremet-va Jul 12, 2024
53a418c
chore: generate locator to the element itself, not the parent
sheremet-va Jul 12, 2024
236a3ca
perf: faster .all() locator
sheremet-va Jul 12, 2024
ebc8ca3
chore: improve windows resolve
sheremet-va Jul 12, 2024
d244040
fix: correctly inject locator init in windows
sheremet-va Jul 12, 2024
dfbe5c7
chore: draft empty locators md
sheremet-va Jul 12, 2024
6b75986
chore: add triple click options
sheremet-va Jul 12, 2024
762df25
chore: don't setup user event in preview locator - sessions should no…
sheremet-va Jul 12, 2024
010e43b
docs: a few docs comments
sheremet-va Jul 12, 2024
26cef8d
chore: type fix
sheremet-va Jul 12, 2024
48c82c6
chore: cleanup
sheremet-va Jul 12, 2024
d27d519
chore: use ivya instead of playwright locators directly
sheremet-va Aug 5, 2024
f8abbb9
chore: wtf
sheremet-va Aug 5, 2024
e350ae3
chore: cleanup
sheremet-va Aug 5, 2024
52bec94
chore: cleanup
sheremet-va Aug 5, 2024
6b55a16
chore: cleanup
sheremet-va Aug 5, 2024
ddbc349
chore: cleanup
sheremet-va Aug 5, 2024
27b14e7
chore: hover/unhover options
sheremet-va Aug 5, 2024
b040a99
chore: fix dead link
sheremet-va Aug 5, 2024
8a10bc3
fix: correct url
sheremet-va Aug 5, 2024
58e2cb0
fix: respect path in screenshot options
sheremet-va Aug 5, 2024
687fd89
feat: allow locator in selectOptions
sheremet-va Aug 5, 2024
ef6eb7c
docs: more locator docs
sheremet-va Aug 5, 2024
fa0ac46
docs: add getByRole docs
sheremet-va Aug 5, 2024
e04e8a8
docs: add docs for every getBy* method
sheremet-va Aug 5, 2024
b3042db
chore: cleanup
sheremet-va Aug 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export default ({ mode }: { mode: string }) => {
link: '/guide/browser/interactivity-api',
docFooterText: 'Interactivity API | Browser Mode',
},
{
text: 'Locators',
link: '/guide/browser/locators',
docFooterText: 'Locators | Browser Mode',
},
{
text: 'Assertion API',
link: '/guide/browser/assertion-api',
Expand Down
11 changes: 11 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,17 @@ Should Vitest UI be injected into the page. By default, injects UI iframe during

Default iframe's viewport.

#### browser.locators {#browser-locators}

Options for built-in [browser locators](/guide/browser/locators).

##### browser.locators.testIdAttribute

- **Type:** `string`
- **Default:** `data-testid`

Attribute used to find elements with `getByTestId` locator.

#### browser.screenshotDirectory {#browser-screenshotdirectory}

- **Type:** `string`
Expand Down
23 changes: 23 additions & 0 deletions docs/guide/browser/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,32 @@ export const page: {
base64: string
}>
screenshot(options?: ScreenshotOptions): Promise<string>
/**
* Extend default `page` object with custom methods.
*/
extend(methods: Partial<BrowserPage>): BrowserPage
/**
* Wrap an HTML element in a `Locator`. When querying for elements, the search will always return this element.
*/
elementLocator(element: Element): Locator

/**
* Locator APIs. See its documentation for more details.
*/
getByRole(role: ARIARole | string, options?: LocatorByRoleOptions): Locator
getByLabelText(text: string | RegExp, options?: LocatorOptions): Locator
getByTestId(text: string | RegExp): Locator
getByAltText(text: string | RegExp, options?: LocatorOptions): Locator
getByPlaceholder(text: string | RegExp, options?: LocatorOptions): Locator
getByText(text: string | RegExp, options?: LocatorOptions): Locator
getByTitle(text: string | RegExp, options?: LocatorOptions): Locator
}
```

::: tip
The `getBy*` API is explained at [Locators API](/guide/browser/locators).
:::

## `cdp`

The `cdp` export returns the current Chrome DevTools Protocol session. It is mostly useful to library authors to build tools on top of it.
Expand Down
20 changes: 10 additions & 10 deletions docs/guide/browser/interactivity-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ This behaviour is more useful because we do not emulate the keyboard, we actuall

## userEvent.click

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

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

Expand All @@ -87,7 +87,7 @@ References:

## userEvent.dblClick

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

Triggers a double click event on an element.

Expand All @@ -112,7 +112,7 @@ References:

## userEvent.tripleClick

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

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`.

Expand Down Expand Up @@ -144,7 +144,7 @@ References:

## userEvent.fill

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

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

Expand Down Expand Up @@ -234,7 +234,7 @@ References:

## userEvent.type

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

::: warning
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.
Expand Down Expand Up @@ -267,7 +267,7 @@ References:

## userEvent.clear

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

This method clears the input element content.

Expand All @@ -294,7 +294,7 @@ References:

## userEvent.selectOptions

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

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

Expand Down Expand Up @@ -337,7 +337,7 @@ References:

## userEvent.hover

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

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.

Expand Down Expand Up @@ -366,7 +366,7 @@ References:

## userEvent.unhover

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

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

Expand All @@ -393,7 +393,7 @@ References:

## userEvent.dragAndDrop

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

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`.

Expand Down
Loading
Loading