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

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

はじめに

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

プッシュ/プルリクエスト時

テストは、main/master ブランチへのプッシュまたはプルリクエスト時に実行されます。ワークフローは、すべての依存関係をインストールし、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.54.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 イメージがあります。最高のパフォーマンスを確保するために、推奨 Docker 設定に従ってください。

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.54.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 イメージを指定するには、設定でエージェント定義を次のようにdocker:で変更するだけです。

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

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

Jenkins

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

pipeline {
agent { docker { image 'mcr.microsoft.com/playwright/python:v1.54.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.54.0-noble

GitLab CI

GitLab で Playwright テストを実行するには、当社の公開 Docker イメージ(Dockerfile を参照)を使用してください。

stages:
- test

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

ブラウザのキャッシュ

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

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

ブラウザ起動のデバッグ

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

DEBUG=pw:browser pytest

ヘッダー付きで実行

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

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

xvfb-run pytest