> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microsandbox.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Automate a browser

> Capture pages or expose a remote browser server without a GPU

Headless Chromium does not require GPU acceleration. This guide uses Microsoft's Playwright image to capture a screenshot and optionally run a persistent browser server for a host-side test runner or agent.

> [!NOTE]
>
> The Playwright image is large. Its first pull can take several minutes, while cached browser launches are much faster. Consider turning a prepared browser sandbox into a [snapshot](https://docs.microsandbox.dev/how-to/sandboxing/warm-workers.md) for repeated jobs.

## Use Playwright

**1. Capture a screenshot**

Limited on cloud (Headless capture works on microsandbox cloud after omitting replace-on-create from this command.)

```sh macOS & Linux theme={null}
msb run --name playwright-shot --replace \
  --cpus 2 --memory 2G --root-disk 4G \
  mcr.microsoft.com/playwright:v1.61.0-noble -- sh -lc '
    npx -y playwright@1.61.0 screenshot \
      --browser chromium \
      https://example.com \
      /root/example.png &&
    stat -c "%s bytes" /root/example.png
  '
```

```powershell Windows theme={null}
msb run --name playwright-shot --replace `
  --cpus 2 --memory 2G --root-disk 4G `
  mcr.microsoft.com/playwright:v1.61.0-noble -- sh -lc '
    npx -y playwright@1.61.0 screenshot \
      --browser chromium \
      https://example.com \
      /root/example.png &&
    stat -c "%s bytes" /root/example.png
  '
```

Copy the artifact out of the stopped sandbox:

```sh Shell theme={null}
msb cp playwright-shot:/root/example.png ./example.png
```

The smoke test used for this guide produced a valid 15,909-byte PNG. Rendering can vary slightly between releases and hosts, so verify the file rather than asserting an exact byte count.

**2. Optional: run a remote server**

Local-only (Publishing the browser server to a port on the computer running the client is not available on microsandbox cloud.)

```sh macOS & Linux theme={null}
msb run -d --name playwright-server --replace \
  --cpus 2 --memory 2G --root-disk 4G \
  -p 127.0.0.1:3000:3000 \
  mcr.microsoft.com/playwright:v1.61.0-noble -- sh -lc '
    exec npx -y playwright@1.61.0 run-server \
      --host 0.0.0.0 \
      --port 3000
  '
```

```powershell Windows theme={null}
msb run -d --name playwright-server --replace `
  --cpus 2 --memory 2G --root-disk 4G `
  -p 127.0.0.1:3000:3000 `
  mcr.microsoft.com/playwright:v1.61.0-noble -- sh -lc '
    exec npx -y playwright@1.61.0 run-server \
      --host 0.0.0.0 \
      --port 3000
  '
```

Verify the forwarding path:

```sh macOS & Linux theme={null}
curl http://127.0.0.1:3000/
```

```powershell Windows theme={null}
curl.exe http://127.0.0.1:3000/
```

The server returns `Running` and logs its WebSocket endpoint:

```sh Shell theme={null}
msb logs playwright-server
```

Connect a matching Playwright client to `ws://127.0.0.1:3000/`. Keep the client and server versions aligned.

> [!WARNING]
>
> The remote server has no application-level authentication. This guide binds the host side to loopback intentionally. Do not publish it on `0.0.0.0` or an internet-facing interface without an authenticated proxy.

**3. Clean up**

Remove the sandboxes:

```sh Shell theme={null}
msb rm -f playwright-shot playwright-server
```

Delete the copied screenshot only if you no longer need it:

```sh macOS & Linux theme={null}
rm ./example.png
```

```powershell Windows theme={null}
Remove-Item ./example.png
```

## Reference

* [Playwright Docker images](https://playwright.dev/docs/docker)
* [Playwright remote connections](https://playwright.dev/docs/api/class-browsertype#browser-type-connect)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.