docs/examples/browser-automation/playwright.mdx
Headless Chromium does not require GPU acceleration. This example 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](/examples/sandboxing/warm-workers) for repeated jobs. </Note><Tooltip tip="Headless capture works on microsandbox cloud after omitting replace-on-create from this command."><span className="msb-badge-limited">Limited on cloud <Icon icon="circle-info" size={11} /></span></Tooltip>
<CodeGroup> ```sh macOS & Linux 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 [email protected] screenshot \ --browser chromium \ https://example.com \ /root/example.png && stat -c "%s bytes" /root/example.png ' ```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 [email protected] screenshot \
--browser chromium \
https://example.com \
/root/example.png &&
stat -c "%s bytes" /root/example.png
'
Copy the artifact out of the stopped sandbox:
msb cp playwright-shot:/root/example.png ./example.png
The smoke test used for this example 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.
</Step> <Step title="Optional: run a remote server"><Tooltip tip="Publishing the browser server to a port on the computer running the client is not available on microsandbox cloud."><span className="msb-badge-local">Local-only <Icon icon="circle-info" size={11} /></span></Tooltip>
<CodeGroup> ```sh macOS & Linux 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 [email protected] run-server \ --host 0.0.0.0 \ --port 3000 ' ```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 [email protected] run-server \
--host 0.0.0.0 \
--port 3000
'
Verify the forwarding path:
<CodeGroup> ```sh macOS & Linux curl http://127.0.0.1:3000/ ```curl.exe http://127.0.0.1:3000/
The server returns Running and logs its WebSocket endpoint:
msb logs playwright-server
Connect a matching Playwright client to ws://127.0.0.1:3000/. Keep the client and server versions aligned.
Remove the sandboxes:
msb rm -f playwright-shot playwright-server
Delete the copied screenshot only if you no longer need it:
<CodeGroup> ```sh macOS & Linux rm ./example.png ```Remove-Item ./example.png