|
| 1 | +# @webcontainer/playwright |
| 2 | + |
| 3 | +[![Version][version-badge]][npm-url] |
| 4 | + |
| 5 | +> Playwright utilities for testing applications in WebContainers |
| 6 | +
|
| 7 | +[Installation](#installation) | [API](#api) |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +Test your applications and packages inside WebContainers using Playwright. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```sh |
| 16 | +$ npm install --save-dev @webcontainer/playwright |
| 17 | +``` |
| 18 | + |
| 19 | +`@playwright/test` is required as peer dependency: |
| 20 | + |
| 21 | +```sh |
| 22 | +$ npm install --save-dev @playwright/test |
| 23 | +``` |
| 24 | + |
| 25 | +## API |
| 26 | + |
| 27 | +### Fixtures |
| 28 | + |
| 29 | +WebContainer utilities are defined as [Playwright fixtures](https://playwright.dev/docs/test-fixtures). You can import pre-defined `test()`, or import each fixture manually and extend your own `test` with each fixture. |
| 30 | + |
| 31 | +```ts |
| 32 | +// Pre-defined test() |
| 33 | +import { test } from "@webcontainer/playwright"; |
| 34 | +``` |
| 35 | + |
| 36 | +```ts |
| 37 | +// Manual import of each fixture |
| 38 | +import { |
| 39 | + Editor, |
| 40 | + Preview, |
| 41 | + Terminal, |
| 42 | + WebContainer, |
| 43 | +} from "@webcontainer/playwright"; |
| 44 | +import { test as base } from "@playwright/test"; |
| 45 | + |
| 46 | +const test = base.extend<{ |
| 47 | + editor: Editor; |
| 48 | + preview: Preview; |
| 49 | + terminal: Terminal; |
| 50 | + webcontainer: WebContainer; |
| 51 | +}>({ |
| 52 | + editor: async ({ page }, use) => { |
| 53 | + use(new Editor(page)); |
| 54 | + }, |
| 55 | + preview: async ({ page }, use) => { |
| 56 | + use(new Preview(page)); |
| 57 | + }, |
| 58 | + terminal: async ({ page }, use) => { |
| 59 | + use(new Terminal(page)); |
| 60 | + }, |
| 61 | + webcontainer: async ({ page }, use) => { |
| 62 | + use(new WebContainer(page)); |
| 63 | + }, |
| 64 | +}); |
| 65 | + |
| 66 | +export { test }; |
| 67 | +``` |
| 68 | + |
| 69 | +You can access each fixture in your test cases: |
| 70 | + |
| 71 | +```ts |
| 72 | +import { test } from "@webcontainer/playwright"; // or your own `test` setup |
| 73 | + |
| 74 | +test("user can open Vite TypeScript starter", async ({ |
| 75 | + page, |
| 76 | + editor, |
| 77 | + preview, |
| 78 | + terminal, |
| 79 | +}) => { |
| 80 | + await page.goto("/"); |
| 81 | + |
| 82 | + await editor.getByFile("package.json", /"vite": "^6.0.11"/); |
| 83 | + await editor.getByFile("src/main.ts", /<h1>Hello Vite<\/h1>/); |
| 84 | + |
| 85 | + await terminal.getByText("VITE v6.0.11 ready"); |
| 86 | + |
| 87 | + await preview.getByRole("heading", { level: 1, name: "Hello Vite" }); |
| 88 | +}); |
| 89 | +``` |
| 90 | + |
| 91 | +#### Editor |
| 92 | + |
| 93 | +##### `getByFile` |
| 94 | + |
| 95 | +Get file by its name and content. |
| 96 | + |
| 97 | +```ts |
| 98 | +async function getByFile(filename: string, content: RegExp | string): Locator; |
| 99 | +``` |
| 100 | + |
| 101 | +#### Preview |
| 102 | + |
| 103 | +##### `getByRole` |
| 104 | + |
| 105 | +Playwright's [`getByRole`](https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-role) that's scoped to the preview `<iframe>`. |
| 106 | + |
| 107 | +#### Terminal |
| 108 | + |
| 109 | +##### `getByText` |
| 110 | + |
| 111 | +Playwright's [`getByText`](https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-role) that's scoped to the terminal. |
| 112 | + |
| 113 | +#### WebContainer |
| 114 | + |
| 115 | +##### `runCommand` |
| 116 | + |
| 117 | +Run command inside WebContainer process. |
| 118 | + |
| 119 | +```ts |
| 120 | +async function runCommand(command: string): Promise<void>; |
| 121 | +``` |
| 122 | + |
| 123 | +[version-badge]: https://img.shields.io/npm/v/@webcontainer/playwright |
| 124 | +[npm-url]: https://www.npmjs.com/package/@webcontainer/playwright |
0 commit comments