kakakakakku blog

Weekly Tech Blog: Keep on Learning!

Playwright for Python: GitHub Actions のエラー時にトレース情報を取得する

前回の記事では Playwright for PythonTrace Viewer を使って,ブラウザ操作中に「何が起きていたのか❓」を詳細に確認できることを紹介した(以下のリンク参照).さらに pytest 連携を使って,自動テスト実行中にトレース情報を取得できることも紹介した.今回は GitHub Actions と Trace Viewer と pytest を組み合わせて,CI 実行中にテストがエラーになったらトレース情報を GitHub Actions の「アーティファクト」に保存するサンプルを作る❗️

kakakakakku.hatenablog.com

GitHub Actions ワークフロー

以下のようにワークフローを作った.ポイントは pytest コマンドを実行するときに --tracing retain-on-failure を指定しているところと,if: failure() でテストがエラーになったときにトレース情報をアーティファクトに保存しているところ.他は自由に書き換えて使ってもらえれば〜

name: pytest

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  pytest:
    name: pytest
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.8', '3.9', '3.10']
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          pip install pytest pytest-playwright
          pip install -r requirements.txt
      - name: Set up Playwright
        run: |
          playwright install
      - name: Test with pytest
        run: |
          pytest --tracing retain-on-failure
      - name: Upload Trace
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: playwright-trace ${{ matrix.python-version }}
          path: test-results/
          retention-days: 7

GitHub Actions ワークフローの構文に関しては以下のドキュメントなどを参考にした.

実際に 成功する pytest コードエラーになる pytest コード 実行すると期待通りにアーティファクトを取得できた!

以下は取得したアーティファクト playwright-trace 3.10.zip を展開した trace.zipplaywright show-trace コマンドで表示したところ!ちゃんと見れてる〜

$ playwright show-trace ~/Downloads/test-playwright-py-test-playwright-chromium/trace.zip