Playwright e2e

From blaxel-ai/sandbox · View interactive version

Playwright e2e is an AI agent skill from the GitHub repository blaxel-ai/sandbox. Deploy a Playwright sandbox (chromium or firefox) on Blaxel and run e2e tests against it. Use to validate that playwright hub images work end-to-end (browser connection, page navigation, DOM interaction) after changes.

How to use this skill

Skill file

Playwright E2E Test

Deploy a Playwright sandbox to Blaxel dev, wait for it to be ready, connect via Playwright, and run browser tests.

Supported sandbox templates:

Both expose the same Playwright server on port 8081 with the same protocol. The only difference is the browser type used to connect.

Prerequisites

Step 1: Pick the sandbox template

Determine which browser to test. Use SANDBOX_NAME=playwright-chromium or SANDBOX_NAME=playwright-firefox. If the user doesn't specify, default to playwright-chromium. If both need testing, run the steps below once per browser.

Step 2: Build and push the image

Use bl push to build and push the image without creating the sandbox resource:

bl push -y -t sandbox -d hub/$SANDBOX_NAME -n $SANDBOX_NAME

This builds the image from hub/$SANDBOX_NAME/Dockerfile and pushes it to the Blaxel registry.

Note: The CLI may report build timed out after 15 minutes for large images (~1.9 GB). If the logs show ✅ Build completed successfully before the timeout, the image was pushed successfully and you can proceed. The timeout is a CLI-side log monitoring limit, not a server-side failure.

Step 3: Create the sandbox with port config

Use bl apply to create the sandbox resource with the correct port declarations. Port 8081 (Playwright server) must be declared, otherwise only port 8080 (sandbox-api) is exposed and Playwright connections fail with 502.

bl apply -f - <<EOF
apiVersion: blaxel.ai/v1alpha1
kind: Sandbox
metadata:
  name: $SANDBOX_NAME
spec:
  runtime:
    image: sandbox/$SANDBOX_NAME:latest
    memory: 4096
    ports:
      - name: sandbox-api
        target: 8080
        protocol: HTTP
      - name: playwright
        target: 8081
        protocol: HTTP
EOF

Step 4: Wait for the sandbox to be ready

Poll until the sandbox status is DEPLOYED. Note: bl get sbx returns a JSON array, so access [0].

while true; do
  STATUS=$(bl get sbx $SANDBOX_NAME -ojson 2>/dev/null | jq -r '.[0].status')
  echo "Sandbox status: $STATUS"
  if [ "$STATUS" = "DEPLOYED" ]; then
    break
  fi
  sleep 5
done

Step 5: Retrieve the sandbox URL and wait for Playwright server

SANDBOX_URL=$(bl get sbx $SANDBOX_NAME -ojson | jq -r '.[0].metadata.url')

The DEPLOYED status means the image is pushed, but the VM may still be cold-starting. Poll the Playwright server until it responds:

TOKEN=$(bl token)
while true; do
  HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $TOKEN" "$SANDBOX_URL/port/8081/json" 2>/dev/null)
  echo "Playwright server HTTP status: $HTTP_STATUS"
  if [ "$HTTP_STATUS" = "200" ]; then
    break
  fi
  sleep 10
done

Step 6: Run the Playwright test

Connect with the token as a ?token= query parameter on the WebSocket URL.

Use the browser type matching the sandbox:

Write a Node.js script that:

  1. Connects to wss://{SANDBOX_HOST}/port/8081/?token={TOKEN} using the appropriate browser type
  2. Creates a new page
  3. Navigates to a test URL (e.g. https://example.com)
  4. Asserts the page loaded correctly (title, DOM elements)
  5. Closes the browser

Chromium example

const { chromium } = require('playwright');

const browser = await chromium.connect(
  `wss://${SANDBOX_HOST}/port/8081/?token=${TOKEN}`,
  { timeout: 15000 }
);

const page = await browser.newPage();
await page.goto('https://example.com');
const title = await page.title();
assert(title.includes('Example Domain'));

await page.close();
await browser.close();

Firefox example

const { firefox } = require('playwright');

const browser = await firefox.connect(
  `wss://${SANDBOX_HOST}/port/8081/?token=${TOKEN}`,
  { timeout: 15000 }
);

const page = await browser.newPage();
await page.goto('https://example.com');
const title = await page.title();
assert(title.includes('Example Domain'));

await page.close();
await browser.close();

Important notes

Step 7: Cleanup

After tests pass, optionally delete the sandbox:

bl delete sbx $SANDBOX_NAME

Troubleshooting

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.