メインコンテンツにスキップ

継続的インテグレーション

はじめに

Playwright テストは CI 環境で実行できます。一般的な CI プロバイダー向けにサンプル設定を作成しました。

CI でテストを実行するための 3 つのステップ

  1. CI エージェントがブラウザを実行できることを確認する: Linux エージェントでDocker イメージを使用するか、CLIを使用して依存関係をインストールします。

  2. Playwright をインストールする:

    pip install playwright
    playwright install --with-deps
  3. テストを実行する:

    pytest

CI 設定

コマンドラインツールは、CI ですべてのオペレーティングシステムの依存関係をインストールするために使用できます。

GitHub Actions

push/pull_request 時

テストは、main/master ブランチへの push または pull request で実行されます。ワークフローは、すべての依存関係をインストールし、Playwright をインストールしてから、テストを実行します。

.github/workflows/playwright.yml
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Ensure browsers are installed
run: python -m playwright install --with-deps
- name: Run your tests
run: pytest --tracing=retain-on-failure
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-traces
path: test-results/

コンテナ経由

GitHub Actions は、コンテナでジョブを実行することをサポートしています。jobs.<job_id>.containerオプションを使用します。これは、ホスト環境を依存関係で汚染しないようにするため、および異なるオペレーティングシステム間でスクリーンショット/ビジュアルリグレッションテストなどに対して一貫した環境を持つために役立ちます。

.github/workflows/playwright.yml
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
playwright:
name: 'Playwright Tests'
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright/python:v1.50.0-noble
options: --user 1001
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r local-requirements.txt
pip install -e .
- name: Run your tests
run: pytest

デプロイ時

これは、GitHub Deploymentsuccess 状態になった後にテストを開始します。Vercel などのサービスはこのパターンを使用しているため、デプロイされた環境でエンドツーエンドテストを実行できます。

.github/workflows/playwright.yml
name: Playwright Tests
on:
deployment_status:
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
if: github.event.deployment_status.state == 'success'
steps:
- uses: actions/checkout@v4
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Ensure browsers are installed
run: python -m playwright install --with-deps
- name: Run tests
run: pytest
env:
# This might depend on your test-runner
PLAYWRIGHT_TEST_BASE_URL: ${{ github.event.deployment_status.target_url }}

Docker

事前構築済みの Docker イメージがあります。これは、直接使用することも、既存の Docker 定義を更新するための参照として使用することもできます。

推奨設定

  1. Chromium を使用する場合は、--ipc=host を使用することも推奨されます。これがないと、Chromium がメモリ不足になり、クラッシュする可能性があります。このオプションの詳細については、Docker ドキュメントを参照してください。
  2. Chromium の起動時に他の奇妙なエラーが発生する場合。ローカルで開発するときに docker run --cap-add=SYS_ADMIN でコンテナを実行してみてください。
  3. --init Docker フラグまたはdumb-initを使用すると、PID=1 のプロセスの特別な扱いを避けるために推奨されます。これは、ゾンビプロセスの一般的な理由です。

Azure Pipelines

Windows または macOS エージェントの場合、追加の設定は不要で、Playwright をインストールしてテストを実行するだけです。

Linux エージェントの場合、Azure Pipelines がコンテナ化されたジョブの実行をサポートしているDocker コンテナを使用できます。または、コマンドラインツールを使用して、必要なすべての依存関係をインストールすることもできます。

Playwright テストを実行するには、このパイプラインタスクを使用します

trigger:
- main

pool:
vmImage: ubuntu-latest

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.11'
displayName: 'Use Python'
- script: |
python -m pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Install dependencies'
- script: playwright install --with-deps
displayName: 'Install Playwright browsers'
- script: pytest
displayName: 'Run Playwright tests'

Azure Pipelines (コンテナ化)

trigger:
- main

pool:
vmImage: ubuntu-latest
container: mcr.microsoft.com/playwright/python:v1.50.0-noble

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.11'
displayName: 'Use Python'

- script: |
python -m pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Install dependencies'
- script: pytest
displayName: 'Run tests'

CircleCI

CircleCI で Playwright を実行することは、GitHub Actions での実行と非常によく似ています。事前構築済みの Playwright Docker イメージを指定するには、次のように config で docker: を使用してエージェント定義を変更するだけです

executors:
pw-noble-development:
docker:
- image: mcr.microsoft.com/playwright/python:v1.50.0-noble

注: docker エージェント定義を使用する場合、playwright が実行されるリソースクラスを「medium」ティア こちらに指定しています。Playwright のデフォルトの動作は、ワーカーの数を検出されたコア数 (medium ティアの場合は 2) に設定することです。ワーカーの数をこれより大きくオーバーライドすると、不要なタイムアウトと失敗が発生します。

Jenkins

Jenkins はパイプライン用の Docker エージェントをサポートしています。Jenkins でテストを実行するには、Playwright Docker イメージを使用してください。

pipeline {
agent { docker { image 'mcr.microsoft.com/playwright/python:v1.50.0-noble' } }
stages {
stage('e2e-tests') {
steps {
sh 'pip install -r requirements.txt'
sh 'pytest'
}
}
}
}

Bitbucket Pipelines

Bitbucket Pipelines は、パブリックDocker イメージをビルド環境として使用できます。Bitbucket で Playwright テストを実行するには、パブリック Docker イメージ (Dockerfile を参照) を使用してください。

image: mcr.microsoft.com/playwright/python:v1.50.0-noble

GitLab CI

GitLab で Playwright テストを実行するには、パブリック Docker イメージ (Dockerfile を参照) を使用してください。

stages:
- test

tests:
stage: test
image: mcr.microsoft.com/playwright/python:v1.50.0-noble
script:
...

ブラウザのキャッシュ

ブラウザバイナリのキャッシュは推奨されません。キャッシュの復元にかかる時間は、バイナリのダウンロードにかかる時間と同程度であるためです。特に Linux では、オペレーティングシステムの依存関係をインストールする必要があります。これらはキャッシュできません。

それでも CI 実行間でブラウザバイナリをキャッシュしたい場合は、Playwright バージョンのハッシュに対して、CI 設定でこれらのディレクトリをキャッシュしてください。

ブラウザ起動のデバッグ

Playwright は、実行中にデバッグログを出力するために DEBUG 環境変数をサポートしています。pw:browser に設定すると、Error: Failed to launch browser エラーのデバッグに役立ちます。

DEBUG=pw:browser pytest

ヘッドあり実行

デフォルトでは、Playwright はブラウザをヘッドレスモードで起動します。ヘッドありモードでテストを実行する方法については、テストの実行ガイドを参照してください。

Linux エージェントでは、ヘッドあり実行には Xvfb がインストールされている必要があります。Docker イメージと GitHub Action には Xvfb がプリインストールされています。Xvfb を使用してヘッドありモードでブラウザを実行するには、実際のコマンドの前に xvfb-run を追加します。

xvfb-run pytest