Skip to content

Playwright

Video: Run your Playwright tests on Hearth (1:27).

Hearth speaks Playwright's own remote protocol. Your tests call connect instead of launch; everything after that is ordinary Playwright. No plugin, no wrapper.

The URL

ws://<farm>/playwright/<engine>?token=<key>
wss://<farm>/playwright/<engine>?token=<key>     # behind TLS
  • <engine> is chromium, firefox or webkit (chrome and safari are accepted as aliases).
  • The key can go in the URL as ?token=, or in a header if your client lets you set one: Authorization: Bearer <key> or x-hearth-key: <key>. Headers keep the key out of logs that print URLs.
  • Optional query parameters:
Parameter What it does
version=1.65 Use the pool for this Playwright major.minor instead of reading it from the client's User-Agent
pool=<name> Use this pool by name; it must serve the engine over Playwright

Each connection is one session and gets a whole browser of its own, in its own pod. You may open as many contexts and pages as you like; browser.close() ends the session.

Versions must match

Playwright's client and server must have the same major.minor. The gateway reads the version from the client's User-Agent (Playwright/1.65.1 ...) and sends the session to a pool that runs the same major.minor. If there is none, it refuses before the connection opens:

this farm runs Playwright 1.65 for chromium, not 1.60; use a matching client version

Fix it by pinning your client to a version the farm runs (kubectl -n hearth get browserpools shows them), or by adding a pool for your version; see Pools and scaling. The default chart runs 1.65, so use a 1.65.x client.

Node.js

npm install playwright@1.65.1
import { chromium } from "playwright";

const browser = await chromium.connect(`wss://hearth.example.com/playwright/chromium`, {
  headers: { Authorization: `Bearer ${process.env.HEARTH_KEY}` },
});
const context = await browser.newContext({ locale: "fr-FR", timezoneId: "Europe/Paris" });
const page = await context.newPage();
await page.goto("https://example.com");
await browser.close();

With Playwright Test, set connectOptions in playwright.config.ts and the built-in browser, context and page fixtures use the farm:

import { defineConfig } from "@playwright/test";

const ws = process.env.HEARTH_URL!.replace(/^http/, "ws");

export default defineConfig({
  use: {
    // Playwright Test asks for a headless browser unless told otherwise, and a headless session
    // has no video and no live view. Headed is what the farm is for.
    headless: false,
    connectOptions: {
      wsEndpoint: `${ws}/playwright/chromium`,
      headers: { Authorization: `Bearer ${process.env.HEARTH_KEY}`, "x-hearth-name": "checkout suite" },
    },
  },
});

Set headless: false

Playwright Test sends headless: true by default, and the farm honours it: the session runs, but it has no recording and the live view stays empty. Set headless: false in use (it costs nothing on the farm: the browser draws on the pod's virtual screen, not yours).

Runnable: examples/playwright-node.

Python

pip install playwright==1.65.1
import os
from playwright.sync_api import sync_playwright

ws = os.environ["HEARTH_URL"].replace("http", "ws", 1)
with sync_playwright() as p:
    browser = p.chromium.connect(
        f"{ws}/playwright/chromium",
        headers={"Authorization": f"Bearer {os.environ['HEARTH_KEY']}"},
    )
    page = browser.new_page()
    page.goto("https://example.com")
    browser.close()

With pytest-playwright, override the browser fixture in conftest.py. Runnable: examples/playwright-python.

Java

<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>playwright</artifactId>
  <version>1.65.1</version>
</dependency>
try (Playwright playwright = Playwright.create()) {
  Browser browser = playwright.chromium().connect(
      ws + "/playwright/chromium",
      new BrowserType.ConnectOptions().setHeaders(Map.of("Authorization", "Bearer " + key)));
  Page page = browser.newPage();
  page.navigate("https://example.com");
}

Runnable: examples/playwright-java.

.NET

dotnet add package Microsoft.Playwright --version 1.65.1
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.ConnectAsync(
    $"{ws}/playwright/chromium",
    new BrowserTypeConnectOptions { Headers = new Dictionary<string, string> { ["Authorization"] = $"Bearer {key}" } });
var page = await browser.NewPageAsync();
await page.GotoAsync("https://example.com");

Runnable: examples/playwright-dotnet.

Session options

Set per-session options the Playwright way, on the context: viewport, locale, timezoneId, geolocation with permissions, httpCredentials, proxy, offline, extraHTTPHeaders, recordHar, device descriptors from playwright.devices, and so on. They work unchanged because the client drives a real browser.

Traces, screenshots, downloads and HARs you ask for in your test (context.tracing.stop({ path }), page.screenshot({ path })) are written on the client side, as with a local browser.

Name the session

Give a session a name, such as the test case, to find it later in the UI and the API. Send the x-hearth-name header, or ?name= in the URL if your client cannot set headers:

const browser = await chromium.connect(`${ws}/playwright/chromium`, {
  headers: { Authorization: `Bearer ${key}`, "x-hearth-name": "checkout applies a coupon" },
});

The name is shown in the session list and on the session's page. It is up to 200 characters; control characters are removed. GET /api/sessions?name=<name> finds the sessions with that name, and each session has its own page at <farm>/sessions/<id>.

Not built yet

Passing farm-side session options in the URL or a hearth-options header (as sketched in the design) is not implemented. Use the context options above.

Launch options and headless

Browser launch options travel in the x-playwright-launch-options header as JSON. Hearth merges them with what it needs:

  • Headed by default. The browser runs on the pod's virtual screen (1280 x 800), so the session can be recorded and watched live. For Chromium, Hearth sets --window-size and --window-position to fill the screen and drops any you passed.
  • headless: true is honoured. The session then has no video and the live view shows an empty screen.
  • Only headless, args, slowMo, timeout, proxy, ignoreDefaultArgs, firefoxUserPrefs and chromiumSandbox are kept. Options that pick a binary or set the environment (executablePath, env, downloadsPath, ...) are dropped, and so are browser flags that launch other programs or open remote debugging; see Security.
const browser = await chromium.connect(`${ws}/playwright/chromium`, {
  headers: {
    Authorization: `Bearer ${key}`,
    "x-playwright-launch-options": JSON.stringify({ headless: true, args: ["--lang=fr"] }),
  },
});

CDP (Chromium only)

Tools that speak the Chrome DevTools Protocol can use connectOverCDP:

ws://<farm>/cdp/chromium?token=<key>
const browser = await chromium.connectOverCDP(`${ws}/cdp/chromium?token=${key}`);
const context = browser.contexts()[0] ?? (await browser.newContext());
const page = await context.newPage();

For CDP sessions the pod launches Playwright's Chromium itself, headed, with a fresh profile and remote debugging, then splices your connection to it. The session is recorded like any other. CDP sessions use the newest Chromium pool unless you pass ?version= or ?pool=. /cdp/firefox and /cdp/webkit answer 404.

Queueing and timeouts

  • If no browser is free, the gateway holds your connection open until one is, up to the pool's queueTimeoutSeconds (120 s by default). Then it answers HTTP 429 with Retry-After. If you set a connect timeout on the client, make it longer than the queue timeout.
  • A team over its maxSessions gets HTTP 429 at once, with Retry-After: 10.
  • A session runs at most the pool's maxSessionSeconds (one hour by default); then the pod ends it and marks it failed with max duration reached.

See Troubleshooting for every refusal and its fix.

What has not been tested

Playwright's exposeNetwork option (reaching services on the client machine from the farm's browser) passes its header through the gateway, but it has not been tested with Hearth.