継続的インテグレーション
はじめに
PlaywrightテストはCI環境で実行できます。一般的なCIプロバイダ向けのサンプル構成を作成しました。
CIでテストを実行するための3ステップ
-
CIエージェントがブラウザを実行できることを確認する: Linuxエージェントでは当社のDockerイメージを使用するか、CLIを使用して依存関係をインストールしてください。
-
Playwrightをインストールする:
pip install playwright
playwright install --with-deps -
テストを実行する:
pytest
CI構成
コマンドラインツールを使用して、CIで必要なすべてのオペレーティングシステム依存関係をインストールできます。
GitHub Actions
プッシュ/プルリクエスト時
テストは、main/masterブランチへのプッシュまたはプルリクエスト時に実行されます。ワークフローは、すべての依存関係をインストールし、Playwrightをインストールしてからテストを実行します。
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
オプションを使用してサポートしています。これは、ホスト環境を依存関係で汚染しないようにするため、また、異なるオペレーティングシステム間でスクリーンショット/ビジュアルリグレッションテストなどを一貫した環境で実行するために役立ちます。
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.51.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 Deploymentがsuccess
状態になった後にテストを開始します。Vercelのようなサービスはこのパターンを使用しており、デプロイされた環境でエンドツーエンドテストを実行できます。
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.51.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.51.0-noble
注: Dockerエージェント定義を使用する場合、Playwrightが実行されるリソースクラスをここで「medium」ティアに指定しています。Playwrightのデフォルトの動作は、ワーカーの数を検出されたコア数(mediumティアの場合は2)に設定することです。この数を超えるワーカーの数をオーバーライドすると、不必要なタイムアウトや失敗が発生します。
Jenkins
JenkinsはパイプラインのDockerエージェントをサポートしています。Jenkinsでテストを実行するには、Playwright Dockerイメージを使用してください。
pipeline {
agent { docker { image 'mcr.microsoft.com/playwright/python:v1.51.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.51.0-noble
GitLab CI
GitLabでPlaywrightテストを実行するには、当社の公開Dockerイメージ(Dockerfileを参照)を使用してください。
stages:
- test
tests:
stage: test
image: mcr.microsoft.com/playwright/python:v1.51.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アクションにはXvfbがプリインストールされています。Xvfbを使用してブラウザをヘッデッドモードで実行するには、実際のコマンドの前にxvfb-run
を追加します。
xvfb-run pytest