Skip to content

Commit b3316ec

Browse files
committed
docs: a few docs comments
1 parent e0047cf commit b3316ec

File tree

4 files changed

+150
-12
lines changed

4 files changed

+150
-12
lines changed

docs/.vitepress/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ export default ({ mode }: { mode: string }) => {
263263
link: '/guide/browser/context',
264264
docFooterText: 'Context | Browser Mode',
265265
},
266+
{
267+
text: 'Locators',
268+
link: '/guide/browser/locators',
269+
docFooterText: 'Locators | Browser Mode',
270+
},
266271
{
267272
text: 'Interactivity API',
268273
link: '/guide/browser/interactivity-api',

docs/guide/browser/locators.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: Locators | Browser Mode
3+
outline: deep
34
---
45

56
# Locators <Version>2.x</Version> <!-- TODO when we decide on the release -->
@@ -24,6 +25,53 @@ title: Locators | Browser Mode
2425
### dropTo
2526
### selectOptions
2627
### screenshot
28+
2729
### element
30+
31+
- **Type:** `() => Element`
32+
33+
This method returns a single element matching the locator's selector.
34+
35+
If _no element_ matches the selector, an error is thrown. Consider using [`.query()`](#query) when you just need to check if the element exists.
36+
37+
If _multiple elements_ match the selector, an error is thrown. Use [`.elements()`](#elements) when you need all matching DOM Elements or [`.all()`](#all) if you need an array of locators matching the selector.
38+
39+
::: tip
40+
This method can be useful if you need to pass it down to an external library. It is called automatically when locator is used with `expect.element` every time the assertion is [retried](/guide/browser/retry-ability):
41+
42+
```ts
43+
await expect.element(page.getByRole('button')).toBeDisabled()
44+
```
45+
:::
46+
47+
Consider the following DOM structure:
48+
49+
```html
50+
<div>Hello <span>World</span></div>
51+
<div>Hello Germany</div>
52+
<div>Hello</div>
53+
```
54+
55+
These locators will not throw an error:
56+
57+
```ts
58+
page.getByText('Hello World').element() //
59+
page.getByText('Hello Germany').element() //
60+
page.getByText('World').element() //
61+
page.getByText('Hello', { exact: true }).element() //
62+
```
63+
64+
These locators will throw an error:
65+
66+
```ts
67+
// returns multiple elements
68+
page.getByText('Hello').element() //
69+
page.getByText(/^Hello/).element() //
70+
71+
// returns no elements
72+
page.getByText('Hello USA').element() //
73+
```
74+
75+
### query
2876
### elements
2977
### all

packages/browser/context.d.ts

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export interface UserEvent {
112112
/**
113113
* Types text into an element. Uses provider's API under the hood.
114114
* **Supports** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`) even with `playwright` and `webdriverio` providers.
115+
* This method can be significantly slower than `userEvent.fill`, so it should be used only when necessary.
115116
* @example
116117
* await userEvent.type(input, 'foo') // translates to: f, o, o
117118
* await userEvent.type(input, '{{a[[') // translates to: {, a, [
@@ -181,16 +182,23 @@ export interface UserEventTripleClickOptions {}
181182
export interface UserEventDragAndDropOptions {}
182183

183184
export interface LocatorOptions {
185+
/**
186+
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a
187+
* regular expression. Note that exact match still trims whitespace.
188+
*/
184189
exact?: boolean
185190
}
186191

187-
export interface LocatorByRoleOptions {
192+
export interface LocatorByRoleOptions extends LocatorOptions {
188193
checked?: boolean
189194
disabled?: boolean
190-
exact?: boolean
191195
expanded?: boolean
192196
includeHidden?: boolean
193197
level?: number
198+
/**
199+
* Option to match the [accessible name](https://w3c.github.io/accname/#dfn-accessible-name). By default, matching is
200+
* case-insensitive and searches for a substring, use `exact` to control this behavior.
201+
*/
194202
name?: string | RegExp
195203
pressed?: boolean
196204
selected?: boolean
@@ -199,39 +207,116 @@ export interface LocatorByRoleOptions {
199207
interface LocatorScreenshotOptions extends Omit<ScreenshotOptions, 'element'> {}
200208

201209
export interface Locator {
202-
selector: string
203-
210+
/**
211+
* @see {@link https://vitest.dev/guide/browser/locators#getByRole}
212+
*/
204213
getByRole(role: ARIARole | ({} & string), options?: LocatorByRoleOptions): Locator
214+
/**
215+
* @see {@link https://vitest.dev/guide/browser/locators#getByLabelText}
216+
*/
205217
getByLabelText(text: string | RegExp, options?: LocatorOptions): Locator
206-
getByTestId(text: string | RegExp): Locator
218+
/**
219+
* @see {@link https://vitest.dev/guide/browser/locators#getByAltText}
220+
*/
207221
getByAltText(text: string | RegExp, options?: LocatorOptions): Locator
222+
/**
223+
* @see {@link https://vitest.dev/guide/browser/locators#getByPlaceholder}
224+
*/
208225
getByPlaceholder(text: string | RegExp, options?: LocatorOptions): Locator
226+
/**
227+
* @see {@link https://vitest.dev/guide/browser/locators#getByText}
228+
*/
209229
getByText(text: string | RegExp, options?: LocatorOptions): Locator
230+
/**
231+
* @see {@link https://vitest.dev/guide/browser/locators#getByTitle}
232+
*/
210233
getByTitle(text: string | RegExp, options?: LocatorOptions): Locator
234+
/**
235+
* @see {@link https://vitest.dev/guide/browser/locators#getByTestId}
236+
*/
237+
getByTestId(text: string | RegExp): Locator
211238

212-
dropTo(target: Locator, options?: UserEventDragAndDropOptions): Promise<void>
239+
/**
240+
* @see {@link https://vitest.dev/guide/browser/locators#click}
241+
*/
213242
click(options?: UserEventClickOptions): Promise<void>
243+
/**
244+
* @see {@link https://vitest.dev/guide/browser/locators#dblClick}
245+
*/
214246
dblClick(options?: UserEventDoubleClickOptions): Promise<void>
247+
/**
248+
* @see {@link https://vitest.dev/guide/browser/locators#tripleClick}
249+
*/
215250
tripleClick(options?: UserEventTripleClickOptions): Promise<void>
251+
/**
252+
* @see {@link https://vitest.dev/guide/browser/locators#clear}
253+
*/
216254
clear(): Promise<void>
255+
/**
256+
* @see {@link https://vitest.dev/guide/browser/locators#hover}
257+
*/
217258
hover(): Promise<void>
259+
/**
260+
* @see {@link https://vitest.dev/guide/browser/locators#unhover}
261+
*/
218262
unhover(): Promise<void>
263+
/**
264+
* @see {@link https://vitest.dev/guide/browser/locators#fill}
265+
*/
219266
fill(text: string, options?: UserEventFillOptions): Promise<void>
267+
/**
268+
* @see {@link https://vitest.dev/guide/browser/locators#dropTo}
269+
*/
270+
dropTo(target: Locator, options?: UserEventDragAndDropOptions): Promise<void>
271+
/**
272+
* @see {@link https://vitest.dev/guide/browser/locators#selectOptions}
273+
*/
220274
selectOptions(
221275
values: HTMLElement | HTMLElement[] | string | string[],
222276
options?: UserEventSelectOptions,
223277
): Promise<void>
224278

225-
element(): Element
226-
elements(): Element[]
227-
query(): Element | null
228-
all(): Locator[]
229-
279+
/**
280+
* @see {@link https://vitest.dev/guide/browser/locators#screenshot}
281+
*/
230282
screenshot(options: Omit<LocatorScreenshotOptions, 'base64'> & { base64: true }): Promise<{
231283
path: string
232284
base64: string
233285
}>
234286
screenshot(options?: LocatorScreenshotOptions): Promise<string>
287+
288+
/**
289+
* Returns an element matching the selector.
290+
*
291+
* - If multiple elements match the selector, an error is thrown.
292+
* - If no elements match the selector, an error is thrown.
293+
*
294+
* @see {@link https://vitest.dev/guide/browser/locators#element}
295+
*/
296+
element(): Element
297+
/**
298+
* Returns an array of elements matching the selector.
299+
*
300+
* If no elements match the selector, an empty array is returned.
301+
*
302+
* @see {@link https://vitest.dev/guide/browser/locators#elements}
303+
*/
304+
elements(): Element[]
305+
/**
306+
* Returns an element matching the selector.
307+
*
308+
* - If multiple elements match the selector, an error is thrown.
309+
* - If no elements match the selector, returns `null`.
310+
*
311+
* @see {@link https://vitest.dev/guide/browser/locators#query}
312+
*/
313+
query(): Element | null
314+
/**
315+
* Wraps an array of `.elements()` matching the selector in a new `Locator`.
316+
*
317+
* @see {@link https://vitest.dev/guide/browser/locators#all}
318+
*/
319+
all(): Locator[]
235320
}
236321

237322
export interface UserEventTabOptions {

packages/browser/src/client/tester/locators/playwright-selector/selector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class PlaywrightSelector {
114114
}
115115
const element = node as Element
116116

117-
const attrs = []
117+
const attrs: string[] = []
118118
for (let i = 0; i < element.attributes.length; i++) {
119119
const { name, value } = element.attributes[i]
120120
if (name === 'style') {

0 commit comments

Comments
 (0)