Skip to main content

持續整合

簡介

Playwright 測試可以在 CI 環境中執行。我們已為常見的 CI 提供者建立了範例設定。

在 CI 上執行測試的三個步驟:

  1. 確保 CI 代理可以執行瀏覽器:在 Linux 代理中使用我們的 Docker 映像,或使用 CLI 安裝您的相依性。

  2. 安裝 Playwright

    pip install playwright
    playwright install --with-deps
  3. 執行您的測試

    pytest

CI 設定

命令列工具可用於在 CI 中安裝所有作業系統相依性。

GitHub Actions

在 push/pull_request 時

測試將在主要/主分支的推送或 pull request 時執行。workflow 將安裝所有相依性、安裝 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.55.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 這樣的服務使用這種模式,因此您可以在他們部署的環境上執行端對端測試。

.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 代理,您可以使用我們的 Docker 容器搭配 Azure Pipelines 支援執行容器化工作。或者,您可以使用命令列工具來安裝所有必要的相依性。

要執行 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.55.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.55.0-noble

注意:使用 docker 代理定義時,您將 playwright 執行的資源類別指定為 'medium' 級別這裡。Playwright 的預設行為是將 workers 數量設定為偵測到的核心數(在 medium 級別的情況下為 2)。將 workers 數量覆寫為大於此數量將導致不必要的逾時和失敗。

Jenkins

Jenkins 支援管線的 Docker 代理。使用 Playwright Docker 映像在 Jenkins 上執行測試。

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

GitLab CI

要在 GitLab 上執行 Playwright 測試,請使用我們的公開 Docker 映像(請參閱 Dockerfile)。

stages:
- test

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

快取瀏覽器

不建議快取瀏覽器執行檔,因為還原快取所需的時間與下載執行檔所需的時間相當。特別是在 Linux 下,需要安裝作業系統相依性,這些相依性無法快取。

如果您仍想在 CI 執行之間快取瀏覽器執行檔,請在您的 CI 設定中針對 Playwright 版本的雜湊值快取這些目錄

偵錯瀏覽器啟動

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