Skip to content

Commit a2822e7

Browse files
committed
docs: locator types
1 parent 7eb0e23 commit a2822e7

File tree

1 file changed

+97
-21
lines changed

1 file changed

+97
-21
lines changed

docs/guide/browser/locators.md

Lines changed: 97 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ The locator API uses a fork of [Playwright's locators](https://playwright.dev/do
1111

1212
## getByRole
1313

14-
- **Type:** `(role: ARIARole | string, options?: LocatorByRoleOptions) => Locator`
14+
```ts
15+
function getByRole(
16+
role: ARIARole | string,
17+
options?: LocatorByRoleOptions,
18+
): Locator
19+
```
1520

1621
Creates a way to locate an element by its [ARIA role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles), [ARIA attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes) and [accessible name](https://developer.mozilla.org/en-US/docs/Glossary/Accessible_name).
1722

@@ -188,7 +193,12 @@ Providing roles via `role` or `aria-*` attributes to built-in elements that alre
188193

189194
## getByAltText
190195

191-
- **Type:** `(text: string | RegExp, options?: LocatorOptions) => Locator`
196+
```ts
197+
function getByAltText(
198+
text: string | RegExp,
199+
options?: LocatorOptions,
200+
): Locator
201+
```
192202

193203
Creates a locator capable of finding an element with an `alt` attribute that matches the text. Unlike testing-library's implementation, Vitest will match any element that has a matching `alt` attribute.
194204

@@ -211,7 +221,12 @@ page.getByAltText('non existing alt text') // ❌
211221

212222
## getByLabelText
213223

214-
- **Type:** `(text: string | RegExp, options?: LocatorOptions) => Locator`
224+
```ts
225+
function getByLabelText(
226+
text: string | RegExp,
227+
options?: LocatorOptions,
228+
): Locator
229+
```
215230

216231
Creates a locator capable of finding an element that has an assosiated label.
217232

@@ -253,7 +268,12 @@ The `page.getByLabelText('Username')` locator will find every input in the examp
253268

254269
## getByPlaceholder
255270

256-
- **Type:** `(text: string | RegExp, options?: LocatorOptions) => Locator`
271+
```ts
272+
function getByPlaceholder(
273+
text: string | RegExp,
274+
options?: LocatorOptions,
275+
): Locator
276+
```
257277

258278
Creates a locator capable of finding an element that has the specified `placeholder` attribute. Vitest will match any element that has a matching `placeholder` attribute, not just `input`.
259279

@@ -280,7 +300,12 @@ It is generally better to rely on a label using [`getByLabelText`](#getbylabelte
280300

281301
## getByText
282302

283-
- **Type:** `(text: string | RegExp, options?: LocatorOptions) => Locator`
303+
```ts
304+
function getByText(
305+
text: string | RegExp,
306+
options?: LocatorOptions,
307+
): Locator
308+
```
284309

285310
Creates a locator capable of finding an element that contains the specified text. The text will be matched against TextNode's [`nodeValue`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue) or input's value if the type is `button` or `reset`. Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.
286311

@@ -307,7 +332,12 @@ This locator is useful for locating non-interactive elements. If you need to loc
307332

308333
## getByTitle
309334

310-
- **Type:** `(text: string | RegExp, options?: LocatorOptions) => Locator`
335+
```ts
336+
function getByTitle(
337+
text: string | RegExp,
338+
options?: LocatorOptions,
339+
): Locator
340+
```
311341

312342
Creates a locator capable of finding an element that has the specified `title` attribute. Unlike testing-library's `getByTitle`, Vitest cannot find `title` elements within an SVG.
313343

@@ -330,7 +360,9 @@ page.getByTitle('Create') // ❌
330360

331361
## getByTestId
332362

333-
- **Type:** `(text: string | RegExp) => Locator`
363+
```ts
364+
function getByTestId(text: string | RegExp): Locator
365+
```
334366

335367
Creates a locator capable of finding an element that matches the specified test id attribute. You can configure the attribute name with [`browser.locators.testIdAttribute`](/config/#browser-locators-testidattribute).
336368

@@ -359,7 +391,9 @@ It is recommended to use this only after the other locators don't work for your
359391

360392
### click
361393

362-
- **Type:** `(options?: UserEventClickOptions) => Promise<void>`
394+
```ts
395+
function click(options?: UserEventClickOptions): Promise<void>
396+
```
363397

364398
Click on an element. You can use the options to set the cursor position.
365399

@@ -373,7 +407,9 @@ await page.getByRole('img', { name: 'Rose' }).click()
373407

374408
### dblClick
375409

376-
- **Type:** `(options?: UserEventClickOptions) => Promise<void>`
410+
```ts
411+
function dblClick(options?: UserEventDoubleClickOptions): Promise<void>
412+
```
377413

378414
Triggers a double click event on an element. You can use the options to set the cursor position.
379415

@@ -387,7 +423,9 @@ await page.getByRole('img', { name: 'Rose' }).dblClick()
387423

388424
### tripleClick
389425

390-
- **Type:** `(options?: UserEventClickOptions) => Promise<void>`
426+
```ts
427+
function tripleClick(options?: UserEventTripleClickOptions): Promise<void>
428+
```
391429

392430
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.
393431

@@ -401,7 +439,9 @@ await page.getByRole('img', { name: 'Rose' }).tripleClick()
401439

402440
### clear
403441

404-
- **Type:** `() => Promise<void>`
442+
```ts
443+
function clear(): Promise<void>
444+
```
405445

406446
Clears the input element content.
407447

@@ -415,7 +455,9 @@ await page.getByRole('textbox', { name: 'Full Name' }).clear()
415455

416456
### hover
417457

418-
- **Type:** `(options?: UserEventHoverOptions) => Promise<void>`
458+
```ts
459+
function hover(options?: UserEventHoverOptions): Promise<void>
460+
```
419461

420462
Moves the cursor position to the selected element.
421463

@@ -429,7 +471,9 @@ await page.getByRole('img', { name: 'Rose' }).hover()
429471

430472
### unhover
431473

432-
- **Type:** `(options?: UserEventHoverOptions) => Promise<void>`
474+
```ts
475+
function unhover(options?: UserEventHoverOptions): Promise<void>
476+
```
433477

434478
This works the same as [`locator.hover`](#hover), but moves the cursor to the `document.body` element instead.
435479

@@ -443,7 +487,9 @@ await page.getByRole('img', { name: 'Rose' }).unhover()
443487

444488
### fill
445489

446-
- **Type:** `(text: string, options?: UserEventFillOptions) => Promise<void>`
490+
```ts
491+
function fill(text: string, options?: UserEventFillOptions): Promise<void>
492+
```
447493

448494
Sets the value of the current `input`, `textarea` or `conteneditable` element.
449495

@@ -457,7 +503,12 @@ await page.getByRole('input', { name: 'Full Name' }).fill('Mr. Bean')
457503

458504
### dropTo
459505

460-
- **Type:** `(target: Locator, options?: UserEventDragAndDropOptions) => Promise<void>`
506+
```ts
507+
function dropTo(
508+
target: Locator,
509+
options?: UserEventDragAndDropOptions,
510+
): Promise<void>
511+
```
461512

462513
Drags the current element to the target location.
463514

@@ -474,7 +525,18 @@ await paris.dropTo(france)
474525

475526
### selectOptions
476527

477-
- **Type:** `(values: HTMLElement | HTMLElement[] | string | string[], options?: UserEventSelectOptions) => Promise<void>`
528+
```ts
529+
function selectOptions(
530+
values:
531+
| HTMLElement
532+
| HTMLElement[]
533+
| Locator
534+
| Locator[]
535+
| string
536+
| string[],
537+
options?: UserEventSelectOptions,
538+
): Promise<void>
539+
```
478540

479541
Choose one or more values from a `<select>` element.
480542

@@ -495,7 +557,13 @@ await languages.selectOptions([
495557

496558
### screenshot
497559

498-
- **Type:** `(options?: LocatorScreenshotOptions) => Promise<string | { path: string; base64: string }>`
560+
```ts
561+
function screenshot(options: LocatorScreenshotOptions & { base64: true }): Promise<{
562+
path: string
563+
base64: string
564+
}>
565+
function screenshot(options?: LocatorScreenshotOptions & { base64?: false }): Promise<string>
566+
```
499567

500568
Creates a screenshot of the element matching the locator's selector.
501569

@@ -520,7 +588,9 @@ const { path, base64 } = await button.screenshot({
520588

521589
### query
522590

523-
- **Type:** `() => Element | null`
591+
```ts
592+
function query(): Element | null
593+
```
524594

525595
This method returns a single element matching the locator's selector or `null` if no element is found.
526596

@@ -552,7 +622,9 @@ page.getByText(/^Hello/).query() // ❌
552622

553623
### element
554624

555-
- **Type:** `() => Element`
625+
```ts
626+
function element(): Element
627+
```
556628

557629
This method returns a single element matching the locator's selector.
558630

@@ -598,7 +670,9 @@ page.getByText('Hello USA').element() // ❌
598670

599671
### elements
600672

601-
- **Type:** `() => Element[]`
673+
```ts
674+
function elements(): Element[]
675+
```
602676

603677
This method returns an array of elements matching the locator's selector.
604678

@@ -623,7 +697,9 @@ page.getByText('Hello USA').elements() // ✅ []
623697

624698
### all
625699

626-
- **Type:** `() => Locator[]`
700+
```ts
701+
function all(): Locator[]
702+
```
627703

628704
This method returns an array of new locators that match the selector.
629705

0 commit comments

Comments
 (0)