--- title: Context API | Browser Mode --- # Context API Vitest exposes a context module via `@vitest/browser/context` entry point. As of 2.0, it exposes a small set of utilities that might be useful to you in tests. ## `userEvent` ::: tip The `userEvent` API is explained in detail at [Interactivity API](/guide/browser/interactivity-api). ::: ```ts /** * Handler for user interactions. The support is implemented by the browser provider (`playwright` or `webdriverio`). * If used with `preview` provider, fallbacks to simulated events via `@testing-library/user-event`. * @experimental */ export const userEvent: { setup: () => UserEvent cleanup: () => Promise click: (element: Element, options?: UserEventClickOptions) => Promise dblClick: (element: Element, options?: UserEventDoubleClickOptions) => Promise tripleClick: (element: Element, options?: UserEventTripleClickOptions) => Promise selectOptions: ( element: Element, values: HTMLElement | HTMLElement[] | string | string[], options?: UserEventSelectOptions, ) => Promise keyboard: (text: string) => Promise type: (element: Element, text: string, options?: UserEventTypeOptions) => Promise clear: (element: Element) => Promise tab: (options?: UserEventTabOptions) => Promise hover: (element: Element, options?: UserEventHoverOptions) => Promise unhover: (element: Element, options?: UserEventHoverOptions) => Promise fill: (element: Element, text: string, options?: UserEventFillOptions) => Promise dragAndDrop: (source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise } ``` ## `commands` ::: tip This API is explained in detail at [Commands API](/guide/browser/commands). ::: ```ts /** * Available commands for the browser. * A shortcut to `server.commands`. */ export const commands: BrowserCommands ``` ## `page` The `page` export provides utilities to interact with the current `page`. ::: warning While it exposes some utilities from Playwright's `page`, it is not the same object. Since the browser context is evaluated in the browser, your tests don't have access to Playwright's `page` because it runs on the server. Use [Commands API](/guide/browser/commands) if you need to have access to Playwright's `page` object. ::: ```ts export const page: { /** * Change the size of iframe's viewport. */ viewport(width: number, height: number): Promise /** * Make a screenshot of the test iframe or a specific element. * @returns Path to the screenshot file or path and base64. */ screenshot(options: Omit & { base64: true }): Promise<{ path: string base64: string }> screenshot(options?: ScreenshotOptions): Promise /** * Extend default `page` object with custom methods. */ extend(methods: Partial): 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). ::: ::: warning WARNING 3.2.0 Note that `screenshot` will always return a base64 string if `save` is set to `false`. The `path` is also ignored in that case. ::: ## `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. ::: warning CDP session works only with `playwright` provider and only when using `chromium` browser. You can read more about it in playwright's [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) documentation. ::: ```ts export const cdp: () => CDPSession ``` ## `server` The `server` export represents the Node.js environment where the Vitest server is running. It is mostly useful for debugging or limiting your tests based on the environment. ```ts export const server: { /** * Platform the Vitest server is running on. * The same as calling `process.platform` on the server. */ platform: Platform /** * Runtime version of the Vitest server. * The same as calling `process.version` on the server. */ version: string /** * Name of the browser provider. */ provider: string /** * Name of the current browser. */ browser: string /** * Available commands for the browser. */ commands: BrowserCommands /** * Serialized test config. */ config: SerializedConfig } ```