playwright: [TIP] Run Playwright Tests on unsupported Linux distributions

To run Playwright on unsupported Linux distribution, you run run Playwright server in Docker while keeping your tests running on the host system:

Run Playwright Server in Docker

docker run -p 3000:3000 --rm --init -it mcr.microsoft.com/playwright:v1.41.0-jammy /bin/sh -c "cd /home/pwuser && npx -y playwright@1.41.0 run-server --port 3000 --host 0.0.0.0"

It will output following when ready

Listening on ws://127.0.0.1:3000/

Point Playwright client to the server on Docker

If running @playwright/test, use environment variable, no other changes are needed.

PW_TEST_CONNECT_WS_ENDPOINT=ws://127.0.0.1:3000/ npx playwright test

Alternatively, for other applications and language ports, use browserType.connect API to use remote Playwright.

const browser = await playwright['chromium'].connect('ws://127.0.0.1:3000/');

Network

Make sure that the host network is available in your Docker container if you are testing local servers:

docker run --add-host=hostmachine:host-gateway

This will make hostmachine point to the host’s localhost. Note that all the tests would also need to use hostmachine in the URLs to access servers on the host. Alternatively, you can follow unconventional route and do docker run --add-host=localhost:host-gateway at your own risk.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 18
  • Comments: 23 (1 by maintainers)

Most upvoted comments

This is great, exactly what we needed, but the first thing that happens when you start the playwright server is that it goes out to npmjs to install the playwright-core dependency. Which doesn’t work in say a CI/CD env with no outbound internet access.

Ideally, the container should be self-contained. Why not install that dependency when creating the container so that it is fully self-contained and works in an offline environment?

woke up this morning and that dawned on me… this is run as a service and i need to execute npx in a different window. duh. Tried that and then could see a client/server pw version mismatch; not sure why but changed the version in docker.compose… and it is working now.

thanks for getting back to me.

For everyone else using this script, I had problems with WebSocket errors, had to add a few lines to wait for the server to be ready in the Docker script before finishing:

#! /bin/bash

if ! curl --output /dev/null --silent --head --fail http://localhost:9009/status
then
  echo "Starting PlayWright Server"
  docker run \
    -d -p 9009:9009 \
    --add-host=hostmachine:host-gateway \
    --rm --init -it \
    mcr.microsoft.com/playwright:v1.41.1-jammy \
    /bin/bash -c "cd /home/pwuser && npx -y playwright@1.41.1 run-server --port 9009"

  # Wait for server to start
  while ! curl --output /dev/null --silent --head --fail http://localhost:9009/status; do
    sleep 0.2
  done
fi

@jfrantzius for Java you can just use BrowserType.connect().

The link above shows connectOverCDP which is about CDP and Chrome only while this issue is about connect() which supports all the browser types Playwright supports.

Answer to self: in the Java client API this works differently, and without the environment variable, as shown here: https://github.com/RG9/remote-playwright-demo/blob/main/src/main/java/org/example/App.java#L18

This only runs the Playwright Webserver in a dockerized environment, but the actual test execution, and therefore, the browser dependencies, still runs in the host, correct?

To run Playwright with dockerized browsers, one could run this inside their PW tests directory:

docker run \
--add-host=hostmachine:host-gateway \
--rm \
--init \
-it \
-v $(pwd):/home/pwuser/tests \
mcr.microsoft.com/playwright:v1.41.0-jammy \
/bin/sh -c "cd /home/pwuser && npm install @playwright/test@1.41.0 && npx playwright install && npx playwright test"

Only caveat with dockerized browser approach is that you can only run it headless. You loose out on many cool Playwright features such as --headed, or --ui.

Ideally, Playwright should be able to install the browsers and dependencies in Linux to run the test natively so that we can have headed runs.

Unfortunately, the container doesn’t work at all for me. I have a fresh setup of Playwright with npm init playwright@latest and followed the instructions above. The container starts fine (Listening on ws://127.0.0.1:3000/) but the tests are just stuck.

>  PW_TEST_CONNECT_WS_ENDPOINT=ws://127.0.0.1:3000/ npx playwright test       
Running 6 tests using 6 workers
[6/6] [chromium] › example.spec.ts:10:5 › get started link

When running npx playwright test by itself, the command finishes almost immediately and has 4 of 6 passing tests (webkit failing due to missing dependencies).

6.5.9-arch2-1 Linux