Skip to content

Commit 5858c88

Browse files
committed
docs: browser mode with docker playwright
1 parent 615fd52 commit 5858c88

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

docs/config/browser/playwright.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,49 @@ These options are directly passed down to `playwright[browser].connect` command.
7575
Since this command connects to an existing Playwright server, any `launch` options will be ignored.
7676
:::
7777

78+
### Running Browsers in Docker
79+
80+
If your platform doesn't support Playwright browsers natively (e.g. WebKit on Arch Linux), you can run a [Playwright server in Docker](https://playwright.dev/docs/docker#remote-connection) and connect to it via `connectOptions`.
81+
82+
Start a Playwright server using Docker Compose:
83+
84+
```yaml [docker-compose.yml]
85+
services:
86+
playwright:
87+
image: mcr.microsoft.com/playwright:v1.52.0-noble
88+
command: /bin/sh -c "npx -y playwright@1.52.0 run-server --port 6677 --host 127.0.0.1"
89+
init: true
90+
network_mode: host
91+
```
92+
93+
```sh
94+
docker compose up -d
95+
```
96+
97+
Then configure Vitest to connect to it:
98+
99+
```ts [vitest.config.ts]
100+
import { playwright } from '@vitest/browser-playwright'
101+
import { defineConfig } from 'vitest/config'
102+
103+
export default defineConfig({
104+
test: {
105+
browser: {
106+
provider: playwright({
107+
connectOptions: {
108+
wsEndpoint: 'ws://127.0.0.1:6677/',
109+
},
110+
}),
111+
instances: [{ browser: 'webkit' }],
112+
},
113+
},
114+
})
115+
```
116+
117+
::: tip
118+
Using `network_mode: host` lets the containerized browser reach Vitest's dev server on localhost without needing to expose it on `0.0.0.0`. Make sure the Playwright version in the Docker image matches the version installed locally.
119+
:::
120+
78121
## contextOptions
79122

80123
Vitest creates a new context for every test file by calling [`browser.newContext()`](https://playwright.dev/docs/api/class-browsercontext). You can configure this behaviour by specifying [custom arguments](https://playwright.dev/docs/api/class-browser#browser-new-context).

test/browser/docker-compose.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
playwright:
3+
image: mcr.microsoft.com/playwright:v1.58.1-noble
4+
command: /bin/sh -c "npx -y playwright@1.58.1 run-server --port 6677 --host 127.0.0.1"
5+
init: true
6+
network_mode: host
7+
user: pwuser

test/browser/settings.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ export const providers = {
1010
webdriverio,
1111
}
1212

13-
export const provider = providers[providerName]()
13+
// Run browser test suites with playwright browsers in docker container
14+
// $ docker compose up -d
15+
// $ BROWSER_WS_ENDPOINT=ws://127.0.0.1:6677/ pnpm test:playwright
16+
export const provider
17+
= providerName === 'playwright' && process.env.BROWSER_WS_ENDPOINT
18+
? playwright({
19+
connectOptions: { wsEndpoint: process.env.BROWSER_WS_ENDPOINT },
20+
})
21+
: providers[providerName]()
22+
1423
export const browser = process.env.BROWSER || (provider.name !== 'playwright' ? 'chromium' : 'chrome')
1524

1625
const devInstances: BrowserInstanceOption[] = [
17-
{ browser },
26+
{ browser: browser as any },
1827
]
1928

2029
const playwrightInstances: BrowserInstanceOption[] = [

0 commit comments

Comments
 (0)