Skip to content

Chapter 16 · CI Integration and Docker ​

Chapter goals: Run the E2E suite in GitHub Actions, use the official container to guarantee environment consistency, and automatically retain traces and other diagnostic artifacts on failure.

16.1 CI in Three Steps ​

The official docs summarize CI integration as three steps:

  1. Make browsers runnable on the CI machine — use the official Docker image on Linux, or install system dependencies via CLI;
  2. Install Playwright — pip install playwright && playwright install --with-deps;
  3. Run tests — pytest.

The --with-deps flag installs OS-level dependency libraries (libnss3, etc.) in one go — skipping it is the #1 reason for "works locally, breaks in CI with missing library" errors.

16.2 GitHub Actions Basic Workflow ​

Official example workflow (Python edition):

16.4 Handling Flaky Network Dependencies ​

For tests hitting external services, add retry and timeout guards:

yaml
- name: Run tests with retries
  run: |
    pip install pytest-rerunfailures
    pytest -n auto --reruns 2 --reruns-delay 5 \
      --timeout 30000 --tracing retain-on-failure

The combination of xdist parallelism (-n auto), automatic retry (--reruns 2), and trace retention gives CI a solid baseline.

16.5 Chapter Summary ​

  • CI needs three things: browser installation (--with-deps), test execution (pytest), artifact upload;
  • --tracing retain-on-failure + if: ${{ !cancelled() }} ensures traces are always available for debugging;
  • Official Docker containers eliminate environment drift;
  • Add pytest-rerunfailures for transient network flakiness.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. What does playwright install --with-deps do that plain playwright install does not?

2. Why use if: ${{ !cancelled() }} on the artifact upload step?

3. What is the primary benefit of running Playwright tests inside the official Docker container?

4. Which combination best handles transient network flakiness in CI?

🛠️ Hands-On Exercises ​

  1. Write a GitHub Actions workflow that runs the Playwright suite on push to main with --tracing retain-on-failure and artifact upload.
  2. Run the same workflow locally using the official Docker container (mcr.microsoft.com/playwright/python) and compare results.
  3. Introduce a flaky network dependency (mock a slow API); add --reruns 2 and observe how many cases survive the retry.

Proceed to Chapter 17: APIRequestContext and API Testing.