···923923### testTimeout
924924925925- **Type:** `number`
926926-- **Default:** `5000`
926926+- **Default:** `5_000` in Node.js, `15_000` if `browser.enabled` is `true`
927927- **CLI:** `--test-timeout=5000`, `--testTimeout=5000`
928928929929Default timeout of a test in milliseconds
···931931### hookTimeout
932932933933- **Type:** `number`
934934-- **Default:** `10000`
934934+- **Default:** `10_000` in Node.js, `30_000` if `browser.enabled` is `true`
935935- **CLI:** `--hook-timeout=10000`, `--hookTimeout=10000`
936936937937Default timeout of a hook in milliseconds
+454-29
docs/guide/browser.md
···212212 * @experimental
213213 */
214214export const userEvent: {
215215+ setup: () => UserEvent
215216 /**
216217 * Click on an element. Uses provider's API under the hood and supports all its options.
217218 * @see {@link https://playwright.dev/docs/api/class-locator#locator-click} Playwright API
···219220 * @see {@link https://testing-library.com/docs/user-event/convenience/#click} testing-library API
220221 */
221222 click: (element: Element, options?: UserEventClickOptions) => Promise<void>
223223+ /**
224224+ * Triggers a double click event on an element. Uses provider's API under the hood.
225225+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-dblclick} Playwright API
226226+ * @see {@link https://webdriver.io/docs/api/element/doubleClick/} WebdriverIO API
227227+ * @see {@link https://testing-library.com/docs/user-event/convenience/#dblClick} testing-library API
228228+ */
229229+ dblClick: (element: Element, options?: UserEventDoubleClickOptions) => Promise<void>
230230+ /**
231231+ * Choose one or more values from a select element. Uses provider's API under the hood.
232232+ * If select doesn't have `multiple` attribute, only the first value will be selected.
233233+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-select-option} Playwright API
234234+ * @see {@link https://webdriver.io/docs/api/element/doubleClick/} WebdriverIO API
235235+ * @see {@link https://testing-library.com/docs/user-event/utility/#-selectoptions-deselectoptions} testing-library API
236236+ */
237237+ selectOptions: (
238238+ element: Element,
239239+ values: HTMLElement | HTMLElement[] | string | string[],
240240+ options?: UserEventSelectOptions,
241241+ ) => Promise<void>
242242+ /**
243243+ * Type text on the keyboard. If any input is focused, it will receive the text,
244244+ * otherwise it will be typed on the document. Uses provider's API under the hood.
245245+ * **Supports** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`) even with `playwright` and `webdriverio` providers.
246246+ * @example
247247+ * await userEvent.keyboard('foo') // translates to: f, o, o
248248+ * await userEvent.keyboard('{{a[[') // translates to: {, a, [
249249+ * await userEvent.keyboard('{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
250250+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
251251+ * @see {@link https://webdriver.io/docs/api/browser/action#key-input-source} WebdriverIO API
252252+ * @see {@link https://testing-library.com/docs/user-event/keyboard} testing-library API
253253+ */
254254+ keyboard: (text: string) => Promise<void>
255255+ /**
256256+ * Types text into an element. Uses provider's API under the hood.
257257+ * **Supports** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`) even with `playwright` and `webdriverio` providers.
258258+ * @example
259259+ * await userEvent.type(input, 'foo') // translates to: f, o, o
260260+ * await userEvent.type(input, '{{a[[') // translates to: {, a, [
261261+ * await userEvent.type(input, '{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
262262+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
263263+ * @see {@link https://webdriver.io/docs/api/browser/action#key-input-source} WebdriverIO API
264264+ * @see {@link https://testing-library.com/docs/user-event/utility/#type} testing-library API
265265+ */
266266+ type: (element: Element, text: string, options?: UserEventTypeOptions) => Promise<void>
267267+ /**
268268+ * Removes all text from an element. Uses provider's API under the hood.
269269+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-clear} Playwright API
270270+ * @see {@link https://webdriver.io/docs/api/element/clearValue} WebdriverIO API
271271+ * @see {@link https://testing-library.com/docs/user-event/utility/#clear} testing-library API
272272+ */
273273+ clear: (element: Element) => Promise<void>
274274+ /**
275275+ * Sends a `Tab` key event. Uses provider's API under the hood.
276276+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
277277+ * @see {@link https://webdriver.io/docs/api/element/keys} WebdriverIO API
278278+ * @see {@link https://testing-library.com/docs/user-event/convenience/#tab} testing-library API
279279+ */
280280+ tab: (options?: UserEventTabOptions) => Promise<void>
281281+ /**
282282+ * Hovers over an element. Uses provider's API under the hood.
283283+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-hover} Playwright API
284284+ * @see {@link https://webdriver.io/docs/api/element/moveTo/} WebdriverIO API
285285+ * @see {@link https://testing-library.com/docs/user-event/convenience/#hover} testing-library API
286286+ */
287287+ hover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
288288+ /**
289289+ * Moves cursor position to the body element. Uses provider's API under the hood.
290290+ * By default, the cursor position is in the center (in webdriverio) or in some visible place (in playwright)
291291+ * of the body element, so if the current element is already there, this will have no effect.
292292+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-hover} Playwright API
293293+ * @see {@link https://webdriver.io/docs/api/element/moveTo/} WebdriverIO API
294294+ * @see {@link https://testing-library.com/docs/user-event/convenience/#hover} testing-library API
295295+ */
296296+ unhover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
297297+ /**
298298+ * Fills an input element with text. This will remove any existing text in the input before typing the new value.
299299+ * Uses provider's API under the hood.
300300+ * This API is faster than using `userEvent.type` or `userEvent.keyboard`, but it **doesn't support** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`).
301301+ * @example
302302+ * await userEvent.fill(input, 'foo') // translates to: f, o, o
303303+ * await userEvent.fill(input, '{{a[[') // translates to: {, {, a, [, [
304304+ * await userEvent.fill(input, '{Shift}') // translates to: {, S, h, i, f, t, }
305305+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-fill} Playwright API
306306+ * @see {@link https://webdriver.io/docs/api/element/setValue} WebdriverIO API
307307+ * @see {@link https://testing-library.com/docs/user-event/utility/#type} testing-library API
308308+ */
309309+ fill: (element: Element, text: string, options?: UserEventFillOptions) => Promise<void>
310310+ /**
311311+ * Drags a source element on top of the target element. This API is not supported by "preview" provider.
312312+ * @see {@link https://playwright.dev/docs/api/class-frame#frame-drag-and-drop} Playwright API
313313+ * @see {@link https://webdriver.io/docs/api/element/dragAndDrop/} WebdriverIO API
314314+ */
315315+ dragAndDrop: (source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>
222316}
223317224318/**
···243337 screenshot: (options?: ScreenshotOptions) => Promise<string>
244338}
245339```
340340+341341+## Interactivity API
342342+343343+Vitest implements a subset of [`@testing-library/user-event`](https://testing-library.com/docs/user-event) APIs using [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/) or [webdriver](https://www.w3.org/TR/webdriver/) APIs instead of faking events which makes the browser behaviour more reliable and consistent.
344344+345345+Almost every `userEvent` method inherits its provider options. To see all available options in your IDE, add `webdriver` or `playwright` types to your `tsconfig.json` file:
346346+347347+::: code-group
348348+```json [playwright]
349349+{
350350+ "compilerOptions": {
351351+ "types": [
352352+ "@vitest/browser/providers/playwright"
353353+ ]
354354+ }
355355+}
356356+```
357357+```json [webdriverio]
358358+{
359359+ "compilerOptions": {
360360+ "types": [
361361+ "@vitest/browser/providers/webdriverio"
362362+ ]
363363+ }
364364+}
365365+```
366366+:::
367367+368368+### userEvent.click
369369+370370+- **Type:** `(element: Element, options?: UserEventClickOptions) => Promise<void>`
371371+372372+Clicks on an element. Inherits provider's options. Please refer to your provider's documentation for detailed explanaition about how this method works.
373373+374374+```ts
375375+import { userEvent } from '@vitest/browser/context'
376376+import { screen } from '@testing-library/dom'
377377+378378+test('clicks on an element', () => {
379379+ const logo = screen.getByRole('img', { name: /logo/ })
380380+381381+ await userEvent.click(logo)
382382+})
383383+```
384384+385385+References:
386386+387387+- [Playwright `locator.click` API](https://playwright.dev/docs/api/class-locator#locator-click)
388388+- [WebdriverIO `element.click` API](https://webdriver.io/docs/api/element/click/)
389389+- [testing-library `click` API](https://testing-library.com/docs/user-event/convenience/#click)
390390+391391+### userEvent.dblClick
392392+393393+- **Type:** `(element: Element, options?: UserEventDoubleClickOptions) => Promise<void>`
394394+395395+Triggers a double click event on an element
396396+397397+Please refer to your provider's documentation for detailed explanaition about how this method works.
398398+399399+```ts
400400+import { userEvent } from '@vitest/browser/context'
401401+import { screen } from '@testing-library/dom'
402402+403403+test('triggers a double click on an element', () => {
404404+ const logo = screen.getByRole('img', { name: /logo/ })
405405+406406+ await userEvent.dblClick(logo)
407407+})
408408+```
409409+410410+References:
411411+412412+- [Playwright `locator.dblclick` API](https://playwright.dev/docs/api/class-locator#locator-dblclick)
413413+- [WebdriverIO `element.doubleClick` API](https://webdriver.io/docs/api/element/doubleClick/)
414414+- [testing-library `dblClick` API](https://testing-library.com/docs/user-event/convenience/#dblClick)
415415+416416+### userEvent.fill
417417+418418+- **Type:** `(element: Element, text: string) => Promise<void>`
419419+420420+Fills an input/textarea/conteneditable element with text. This will remove any existing text in the input before typing the new value.
421421+422422+```ts
423423+import { userEvent } from '@vitest/browser/context'
424424+import { screen } from '@testing-library/dom'
425425+426426+test('update input', () => {
427427+ const input = screen.getByRole('input')
428428+429429+ await userEvent.fill(input, 'foo') // input.value == foo
430430+ await userEvent.fill(input, '{{a[[') // input.value == {{a[[
431431+ await userEvent.fill(input, '{Shift}') // input.value == {Shift}
432432+})
433433+```
434434+435435+::: tip
436436+This API is faster than using [`userEvent.type`](#userevent-type) or [`userEvent.keyboard`](#userevent-keyboard), but it **doesn't support** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}{selectall}`).
437437+438438+We recommend using this API over [`userEvent.type`](#userevent-type) in situations when you don't need to enter special characters.
439439+:::
440440+441441+References:
442442+443443+- [Playwright `locator.fill` API](https://playwright.dev/docs/api/class-locator#locator-fill)
444444+- [WebdriverIO `element.setValue` API](https://webdriver.io/docs/api/element/setValue)
445445+- [testing-library `type` API](https://testing-library.com/docs/user-event/utility/#type)
446446+447447+### userEvent.keyboard
448448+449449+- **Type:** `(text: string) => Promise<void>`
450450+451451+The `userEvent.keyboard` allows you to trigger keyboard strokes. If any input has a focus, it will type characters into that input. Otherwise, it will trigger keyboard events on the currently focused element (`document.body` if there are no focused elements).
452452+453453+This API supports [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard).
454454+455455+```ts
456456+import { userEvent } from '@vitest/browser/context'
457457+import { screen } from '@testing-library/dom'
458458+459459+test('trigger keystrokes', () => {
460460+ await userEvent.keyboard('foo') // translates to: f, o, o
461461+ await userEvent.keyboard('{{a[[') // translates to: {, a, [
462462+ await userEvent.keyboard('{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
463463+ await userEvent.keyboard('{a>5}') // press a without releasing it and trigger 5 keydown
464464+ await userEvent.keyboard('{a>5/}') // press a for 5 keydown and then release it
465465+})
466466+```
467467+468468+References:
469469+470470+- [Playwright `locator.press` API](https://playwright.dev/docs/api/class-locator#locator-press)
471471+- [WebdriverIO `action('key')` API](https://webdriver.io/docs/api/browser/action#key-input-source)
472472+- [testing-library `type` API](https://testing-library.com/docs/user-event/utility/#type)
473473+474474+### userEvent.tab
475475+476476+- **Type:** `(options?: UserEventTabOptions) => Promise<void>`
477477+478478+Sends a `Tab` key event. This is a shorthand for `userEvent.keyboard('{tab}')`.
479479+480480+```ts
481481+import { userEvent } from '@vitest/browser/context'
482482+import { screen } from '@testing-library/dom'
483483+import '@testing-library/jest-dom' // adds support for "toHaveFocus"
484484+485485+test('tab works', () => {
486486+ const [input1, input2] = screen.getAllByRole('input')
487487+488488+ expect(input1).toHaveFocus()
489489+490490+ await userEvent.tab()
491491+492492+ expect(input2).toHaveFocus()
493493+494494+ await userEvent.tab({ shift: true })
495495+496496+ expect(input1).toHaveFocus()
497497+})
498498+```
499499+500500+References:
501501+502502+- [Playwright `locator.press` API](https://playwright.dev/docs/api/class-locator#locator-press)
503503+- [WebdriverIO `action('key')` API](https://webdriver.io/docs/api/browser/action#key-input-source)
504504+- [testing-library `tab` API](https://testing-library.com/docs/user-event/convenience/#tab)
505505+506506+### userEvent.type
507507+508508+- **Type:** `(element: Element, text: string, options?: UserEventTypeOptions) => Promise<void>`
509509+510510+::: warning
511511+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.
512512+:::
513513+514514+The `type` method implements `@testing-library/user-event`'s [`type`](https://testing-library.com/docs/user-event/utility/#type) utility built on top of [`keyboard`](https://testing-library.com/docs/user-event/keyboard) API.
515515+516516+This function allows you to type characters into an input/textarea/conteneditable element. It supports [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard).
517517+518518+If you just need to press characters without an input, use [`userEvent.keyboard`](#userevent-keyboard) API.
519519+520520+```ts
521521+import { userEvent } from '@vitest/browser/context'
522522+import { screen } from '@testing-library/dom'
523523+524524+test('update input', () => {
525525+ const input = screen.getByRole('input')
526526+527527+ await userEvent.type(input, 'foo') // input.value == foo
528528+ await userEvent.type(input, '{{a[[') // input.value == foo{a[
529529+ await userEvent.type(input, '{Shift}') // input.value == foo{a[
530530+})
531531+```
532532+533533+References:
534534+535535+- [Playwright `locator.press` API](https://playwright.dev/docs/api/class-locator#locator-press)
536536+- [WebdriverIO `action('key')` API](https://webdriver.io/docs/api/browser/action#key-input-source)
537537+- [testing-library `type` API](https://testing-library.com/docs/user-event/utility/#type)
538538+539539+### userEvent.clear
540540+541541+- **Type:** `(element: Element) => Promise<void>`
542542+543543+This method clear the input element content.
544544+545545+```ts
546546+import { userEvent } from '@vitest/browser/context'
547547+import { screen } from '@testing-library/dom'
548548+import '@testing-library/jest-dom' // adds support for "toHaveValue"
549549+550550+test('clears input', () => {
551551+ const input = screen.getByRole('input')
552552+553553+ await userEvent.fill(input, 'foo')
554554+ expect(input).toHaveValue('foo')
555555+556556+ await userEvent.clear(input)
557557+ expect(input).toHaveValue('')
558558+})
559559+```
560560+561561+References:
562562+563563+- [Playwright `locator.clear` API](https://playwright.dev/docs/api/class-locator#locator-clear)
564564+- [WebdriverIO `element.clearValue` API](https://webdriver.io/docs/api/element/clearValue)
565565+- [testing-library `clear` API](https://testing-library.com/docs/user-event/utility/#clear)
566566+567567+### userEvent.selectOptions
568568+569569+- **Type:** `(element: Element, values: HTMLElement | HTMLElement[] | string | string[], options?: UserEventSelectOptions) => Promise<void>`
570570+571571+The `userEvent.selectOptions` allows selecting a value in a `<select>` element.
572572+573573+::: warning
574574+If select element doesn't have [`multiple`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#attr-multiple) attribute, Vitest will select only the first element in the array.
575575+576576+Unlike `@testing-library`, Vitest doesn't support [listbox](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/listbox_role) at the moment, but we plan to add support for it in the future.
577577+:::
578578+579579+```ts
580580+import { userEvent } from '@vitest/browser/context'
581581+import { screen } from '@testing-library/dom'
582582+import '@testing-library/jest-dom' // adds support for "toHaveValue"
583583+584584+test('clears input', () => {
585585+ const select = screen.getByRole('select')
586586+587587+ await userEvent.selectOptions(select, 'Option 1')
588588+ expect(select).toHaveValue('option-1')
589589+590590+ await userEvent.selectOptions(select, 'option-1')
591591+ expect(select).toHaveValue('option-1')
592592+593593+ await userEvent.selectOptions(select, [
594594+ screen.getByRole('option', { name: 'Option 1' }),
595595+ screen.getByRole('option', { name: 'Option 2' }),
596596+ ])
597597+ expect(select).toHaveValue(['option-1', 'option-2'])
598598+})
599599+```
600600+601601+::: warning
602602+`webdriverio` provider doesn't support selecting multiple elements because it doesn't provide API to do so.
603603+:::
604604+605605+References:
606606+607607+- [Playwright `locator.selectOption` API](https://playwright.dev/docs/api/class-locator#locator-select-option)
608608+- [WebdriverIO `element.selectByIndex` API](https://webdriver.io/docs/api/element/selectByIndex)
609609+- [testing-library `selectOptions` API](https://testing-library.com/docs/user-event/utility/#-selectoptions-deselectoptions)
610610+611611+### userEvent.hover
612612+613613+- **Type:** `(element: Element, options?: UserEventHoverOptions) => Promise<void>`
614614+615615+This method moves the cursor position to selected element. Please refer to your provider's documentation for detailed explanaition about how this method works.
616616+617617+::: warning
618618+If you are using `webdriverio` provider, the cursor will move to the center of the element by default.
619619+620620+If you are using `playwright` provider, the cursor moves to "some" visible point of the element.
621621+:::
622622+623623+```ts
624624+import { userEvent } from '@vitest/browser/context'
625625+import { screen } from '@testing-library/dom'
626626+627627+test('hovers logo element', () => {
628628+ const logo = screen.getByRole('img', { name: /logo/ })
629629+630630+ await userEvent.hover(logo)
631631+})
632632+```
633633+634634+References:
635635+636636+- [Playwright `locator.hover` API](https://playwright.dev/docs/api/class-locator#locator-hover)
637637+- [WebdriverIO `element.moveTo` API](https://webdriver.io/docs/api/element/moveTo/)
638638+- [testing-library `hover` API](https://testing-library.com/docs/user-event/convenience/#hover)
639639+640640+### userEvent.unhover
641641+642642+- **Type:** `(element: Element, options?: UserEventHoverOptions) => Promise<void>`
643643+644644+This works the same as [`userEvent.hover`](#userevent-hover), but moves the cursor to the `document.body` element instead.
645645+646646+::: warning
647647+By default, the cursor position is in the center (in `webdriverio` provider) or in "some" visible place (in `playwright` provider) of the body element, so if the currently hovered element is already in the same position, this method will have no effect.
648648+:::
649649+650650+```ts
651651+import { userEvent } from '@vitest/browser/context'
652652+import { screen } from '@testing-library/dom'
653653+654654+test('unhover logo element', () => {
655655+ const logo = screen.getByRole('img', { name: /logo/ })
656656+657657+ await userEvent.unhover(logo)
658658+})
659659+```
660660+661661+References:
662662+663663+- [Playwright `locator.hover` API](https://playwright.dev/docs/api/class-locator#locator-hover)
664664+- [WebdriverIO `element.moveTo` API](https://webdriver.io/docs/api/element/moveTo/)
665665+- [testing-library `hover` API](https://testing-library.com/docs/user-event/convenience/#hover)
666666+667667+### userEvent.dragAndDrop
668668+669669+- **Type:** `(source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>`
670670+671671+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`.
672672+673673+```ts
674674+import { userEvent } from '@vitest/browser/context'
675675+import { screen } from '@testing-library/dom'
676676+import '@testing-library/jest-dom' // adds support for "toHaveTextContent"
677677+678678+test('drag and drop works', async () => {
679679+ const source = screen.getByRole('img', { name: /logo/ })
680680+ const target = screen.getByTestId('logo-target')
681681+682682+ await userEvent.dragAndDrop(source, target)
683683+684684+ expect(target).toHaveTextContent('Logo is processed')
685685+})
686686+```
687687+688688+::: warning
689689+This API is not supported by the `preview` provider.
690690+:::
691691+692692+References:
693693+694694+- [Playwright `frame.dragAndDrop` API](https://playwright.dev/docs/api/class-frame#frame-drag-and-drop)
695695+- [WebdriverIO `element.dragAndDrop` API](https://webdriver.io/docs/api/element/dragAndDrop/)
246696247697## Commands
248698···276726 await removeFile(file)
277727})
278728```
279279-280280-### Keyboard Interactions
281281-282282-Vitest also implements Web Test Runner's [`sendKeys` API](https://modern-web.dev/docs/test-runner/commands/#send-keys). It accepts an object with a single property:
283283-284284-- `type` - types a sequence of characters, this API _is not_ affected by modifier keys, so having `Shift` won't make letters uppercase
285285-- `press` - presses a single key, this API _is_ affected by modifier keys, so having `Shift` will make subsequent characters uppercase
286286-- `up` - holds down a key (supported only with `playwright` provider)
287287-- `down` - releases a key (supported only with `playwright` provider)
288288-289289-```ts
290290-interface TypePayload { type: string }
291291-interface PressPayload { press: string }
292292-interface DownPayload { down: string }
293293-interface UpPayload { up: string }
294294-295295-type SendKeysPayload = TypePayload | PressPayload | DownPayload | UpPayload
296296-297297-declare function sendKeys(payload: SendKeysPayload): Promise<void>
298298-```
299299-300300-This is just a simple wrapper around providers APIs. Please refer to their respective documentations for details:
301301-302302-- [Playwright Keyboard API](https://playwright.dev/docs/api/class-keyboard)
303303-- [Webdriver Keyboard API](https://webdriver.io/docs/api/browser/keys/)
304729305730## Custom Commands
306731···370795Vitest exposes several `playwright` specific properties on the command context.
371796372797- `page` references the full page that contains the test iframe. This is the orchestrator HTML and you most likely shouldn't touch it to not break things.
373373-- `tester` is the iframe locator. The API is pretty limited here, but you can chain it further to access your HTML elements.
374374-- `body` is the iframe's `body` locator that exposes more Playwright APIs.
798798+- `frame` is the tester [iframe instance](https://playwright.dev/docs/api/class-frame). It has a simillar API to the page, but it doesn't support certain methods.
799799+- `context` refers to the unique [BrowserContext](https://playwright.dev/docs/api/class-browsercontext).
375800376801```ts
377802import { defineCommand } from '@vitest/browser'
378803379804export const myCommand = defineCommand(async (ctx, arg1, arg2) => {
380805 if (ctx.provider.name === 'playwright') {
381381- const element = await ctx.tester.findByRole('alert')
806806+ const element = await ctx.frame.findByRole('alert')
382807 const screenshot = await element.screenshot()
383808 // do something with the screenshot
384809 return difference
···387812```
388813389814::: tip
390390-If you are using TypeScript, don't forget to add `@vitest/browser/providers/playwright` to your `tsconfig` "compilerOptions.types" field to get autocompletion:
815815+If you are using TypeScript, don't forget to add `@vitest/browser/providers/playwright` to your `tsconfig` "compilerOptions.types" field to get autocompletion in the config and on `userEvent` and `page` options:
391816392817```json
393818{
+120-22
packages/browser/context.d.ts
···1919 flag?: string | number
2020}
21212222-export interface TypePayload {
2323- type: string
2424-}
2525-export interface PressPayload {
2626- press: string
2727-}
2828-export interface DownPayload {
2929- down: string
3030-}
3131-export interface UpPayload {
3232- up: string
3333-}
3434-3535-export type SendKeysPayload =
3636- | TypePayload
3737- | PressPayload
3838- | DownPayload
3939- | UpPayload
4040-4122export interface ScreenshotOptions {
4223 element?: Element
4324 /**
···5738 options?: BufferEncoding | (FsOptions & { mode?: number | string })
5839 ) => Promise<void>
5940 removeFile: (path: string) => Promise<void>
6060- sendKeys: (payload: SendKeysPayload) => Promise<void>
6141}
62426343export interface UserEvent {
4444+ setup: () => UserEvent
6445 /**
6546 * Click on an element. Uses provider's API under the hood and supports all its options.
6647 * @see {@link https://playwright.dev/docs/api/class-locator#locator-click} Playwright API
···6849 * @see {@link https://testing-library.com/docs/user-event/convenience/#click} testing-library API
6950 */
7051 click: (element: Element, options?: UserEventClickOptions) => Promise<void>
5252+ /**
5353+ * Triggers a double click event on an element. Uses provider's API under the hood.
5454+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-dblclick} Playwright API
5555+ * @see {@link https://webdriver.io/docs/api/element/doubleClick/} WebdriverIO API
5656+ * @see {@link https://testing-library.com/docs/user-event/convenience/#dblClick} testing-library API
5757+ */
5858+ dblClick: (element: Element, options?: UserEventDoubleClickOptions) => Promise<void>
5959+ /**
6060+ * Choose one or more values from a select element. Uses provider's API under the hood.
6161+ * If select doesn't have `multiple` attribute, only the first value will be selected.
6262+ * @example
6363+ * await userEvent.selectOptions(select, 'Option 1')
6464+ * expect(select).toHaveValue('option-1')
6565+ *
6666+ * await userEvent.selectOptions(select, 'option-1')
6767+ * expect(select).toHaveValue('option-1')
6868+ *
6969+ * await userEvent.selectOptions(select, [
7070+ * screen.getByRole('option', { name: 'Option 1' }),
7171+ * screen.getByRole('option', { name: 'Option 2' }),
7272+ * ])
7373+ * expect(select).toHaveValue(['option-1', 'option-2'])
7474+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-select-option} Playwright API
7575+ * @see {@link https://webdriver.io/docs/api/element/doubleClick/} WebdriverIO API
7676+ * @see {@link https://testing-library.com/docs/user-event/utility/#-selectoptions-deselectoptions} testing-library API
7777+ */
7878+ selectOptions: (
7979+ element: Element,
8080+ values: HTMLElement | HTMLElement[] | string | string[],
8181+ options?: UserEventSelectOptions,
8282+ ) => Promise<void>
8383+ /**
8484+ * Type text on the keyboard. If any input is focused, it will receive the text,
8585+ * otherwise it will be typed on the document. Uses provider's API under the hood.
8686+ * **Supports** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`) even with `playwright` and `webdriverio` providers.
8787+ * @example
8888+ * await userEvent.keyboard('foo') // translates to: f, o, o
8989+ * await userEvent.keyboard('{{a[[') // translates to: {, a, [
9090+ * await userEvent.keyboard('{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
9191+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
9292+ * @see {@link https://webdriver.io/docs/api/browser/keys} WebdriverIO API
9393+ * @see {@link https://testing-library.com/docs/user-event/keyboard} testing-library API
9494+ */
9595+ keyboard: (text: string) => Promise<void>
9696+ /**
9797+ * Types text into an element. Uses provider's API under the hood.
9898+ * **Supports** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`) even with `playwright` and `webdriverio` providers.
9999+ * @example
100100+ * await userEvent.type(input, 'foo') // translates to: f, o, o
101101+ * await userEvent.type(input, '{{a[[') // translates to: {, a, [
102102+ * await userEvent.type(input, '{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
103103+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
104104+ * @see {@link https://webdriver.io/docs/api/browser/action#key-input-source} WebdriverIO API
105105+ * @see {@link https://testing-library.com/docs/user-event/utility/#type} testing-library API
106106+ */
107107+ type: (element: Element, text: string, options?: UserEventTypeOptions) => Promise<void>
108108+ /**
109109+ * Removes all text from an element. Uses provider's API under the hood.
110110+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-clear} Playwright API
111111+ * @see {@link https://webdriver.io/docs/api/element/clearValue} WebdriverIO API
112112+ * @see {@link https://testing-library.com/docs/user-event/utility/#clear} testing-library API
113113+ */
114114+ clear: (element: Element) => Promise<void>
115115+ /**
116116+ * Sends a `Tab` key event. Uses provider's API under the hood.
117117+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
118118+ * @see {@link https://webdriver.io/docs/api/element/keys} WebdriverIO API
119119+ * @see {@link https://testing-library.com/docs/user-event/convenience/#tab} testing-library API
120120+ */
121121+ tab: (options?: UserEventTabOptions) => Promise<void>
122122+ /**
123123+ * Hovers over an element. Uses provider's API under the hood.
124124+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-hover} Playwright API
125125+ * @see {@link https://webdriver.io/docs/api/element/moveTo/} WebdriverIO API
126126+ * @see {@link https://testing-library.com/docs/user-event/convenience/#hover} testing-library API
127127+ */
128128+ hover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
129129+ /**
130130+ * Moves cursor position to the body element. Uses provider's API under the hood.
131131+ * By default, the cursor position is in the center (in webdriverio) or in some visible place (in playwright)
132132+ * of the body element, so if the current element is already there, this will have no effect.
133133+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-hover} Playwright API
134134+ * @see {@link https://webdriver.io/docs/api/element/moveTo/} WebdriverIO API
135135+ * @see {@link https://testing-library.com/docs/user-event/convenience/#hover} testing-library API
136136+ */
137137+ unhover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
138138+ /**
139139+ * Fills an input element with text. This will remove any existing text in the input before typing the new text.
140140+ * Uses provider's API under the hood.
141141+ * This API is faster than using `userEvent.type` or `userEvent.keyboard`, but it **doesn't support** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`).
142142+ * @example
143143+ * await userEvent.fill(input, 'foo') // translates to: f, o, o
144144+ * await userEvent.fill(input, '{{a[[') // translates to: {, {, a, [, [
145145+ * await userEvent.fill(input, '{Shift}') // translates to: {, S, h, i, f, t, }
146146+ * @see {@link https://playwright.dev/docs/api/class-locator#locator-fill} Playwright API
147147+ * @see {@link https://webdriver.io/docs/api/element/setValue} WebdriverIO API
148148+ * @see {@link https://testing-library.com/docs/user-event/utility/#type} testing-library API
149149+ */
150150+ fill: (element: Element, text: string, options?: UserEventFillOptions) => Promise<void>
151151+ /**
152152+ * Drags a source element on top of the target element. This API is not supported by "preview" provider.
153153+ * @see {@link https://playwright.dev/docs/api/class-frame#frame-drag-and-drop} Playwright API
154154+ * @see {@link https://webdriver.io/docs/api/element/dragAndDrop/} WebdriverIO API
155155+ */
156156+ dragAndDrop: (source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>
71157}
721587373-export interface UserEventClickOptions {
7474- [key: string]: any
159159+export interface UserEventFillOptions {}
160160+export interface UserEventHoverOptions {}
161161+export interface UserEventSelectOptions {}
162162+export interface UserEventClickOptions {}
163163+export interface UserEventDoubleClickOptions {}
164164+export interface UserEventDragAndDropOptions {}
165165+166166+export interface UserEventTabOptions {
167167+ shift?: boolean
168168+}
169169+170170+export interface UserEventTypeOptions {
171171+ skipClick?: boolean
172172+ skipAutoClose?: boolean
75173}
7617477175type Platform =