Học Playwright tiếng Việt, Cộng đồng Playwright cho người Việt

Vọc Vạch Playwright

[Vọc Playwright] Sharding

https://playwright.dev/docs/test-sharding

Giới thiệu

  • Mặc định Playwright chạy test song song với nhau để tối ưu.
  • Trong trường hợp bạn vẫn muốn tối ưu hơn nữa, thì bạn cần dùng sharding để chạy test trên nhiều máy khác nhau.

Sharding test giữa các máy khác nhau

  • Để sharding test giữa các máy khác nhau, bạn cần thêm tham số vào command line --shard=x/y.
  • Ví dụ để chạy trên 4 máy khác nhau, thì trên mỗi máy, ta chạy command như sau:
npx playwright test --shard=1/4
npx playwright test --shard=2/4
npx playwright test --shard=3/4
npx playwright test --shard=4/4
  • Lưu ý: Playwright chỉ có thể shard các test chạy parallel được.

Merge report từ nhiều shard

  • Ở ví dụ trước, mỗi shard sẽ sinh ra các report của riêng mình.
  • Để có thể xem dữ liệu được từ tất cả các shard, bạn có thể merge chúng lại.
  • Đầu tiên thì bạn cần đổi report type về blob:
// playwright.config.ts
export default defineConfig({
  testDir: './tests',
  reporter: process.env.CI ? 'blob' : 'html',
});
  • Tiếp theo thì bạn cần lấy tất cả các blob report ra và để cùng vào một folder, ví dụ all-blob-reports.
  • Sau đó chạy lệnh:
npx playwright merge-reports --reporter html ./all-blob-reports
  • Chạy lệnh này sẽ sinh ra HTML report ở trong thư mục playwright-report

Ví dụ trong GitHub Actions

  • GitHub Actions hỗ trợ sharding giữa nhiều jobs sử dụng <job_id>.strategy.matrix.
  • Ví dụ dưới hướng dẫn bạn cách config một job cho các test trên 4 máy khác nhau, sau đó merge vào chung một report.
  • Đầu tiên, thêm vào file config: reporter: process.env.CI ? 'blob' : 'html'
  1. Thêm option maxtrix:
maxtrix:
  shardIndex: [1, 2, 3, 4]
  shardTotal: [4]
  1. Sau đó chúng ta chạy test với tuỳ chọn --shard=${{ matrix.shardIndex}} / ${{matrix.shardTotal}}

  2. Cuối cùng thì upload blob lên GitHub Actions Artifacts.

// .github/workflows/playwright.yml
name: Playwright Tests
on:
  push:
    branches: [ main, master ]
  pull_request:
    branches: [ main, master ]
jobs:
  playwright-tests:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        shardIndex: [1, 2, 3, 4]
        shardTotal: [4]
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: 18
    - name: Install dependencies
      run: npm ci
    - name: Install Playwright browsers
      run: npx playwright install --with-deps

    - name: Run Playwright tests
      run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}

    - name: Upload blob report to GitHub Actions Artifacts
      if: ${{ !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: blob-report-${{ matrix.shardIndex }}
        path: blob-report
        retention-days: 1
  • Sau khi tất cả các shard chạy xong, bạn có thể chạy các một job riêng để merge report lại
jobs:
...
  merge-reports:
    # Merge reports after playwright-tests, even if some shards have failed
    if: ${{ !cancelled() }}
    needs: [playwright-tests]

    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: 18
    - name: Install dependencies
      run: npm ci

    - name: Download blob reports from GitHub Actions Artifacts
      uses: actions/download-artifact@v4
      with:
        path: all-blob-reports
        pattern: blob-report-*
        merge-multiple: true

    - name: Merge into HTML Report
      run: npx playwright merge-reports --reporter html ./all-blob-reports

    - name: Upload HTML report
      uses: actions/upload-artifact@v4
      with:
        name: html-report--attempt-${{ github.run_attempt }}
        path: playwright-report
        retention-days: 14
  • Kết quả cuối cùng trông như này:

GitHub Action

Merge-reports CLI

  • Ở phía trên có giới thiệu dùng lệnh này để merge test report:
npx playwright merge-reports path/to/blob-reports-dir
  • Khi bạn merge report từ các hệ điều hành khác nhau, bạn cần đưa ra các config merge riêng biệt để phân biệt thư mục nào là thư mục test gốc.

  • Một số options:

    • --reporter reporter-to-use: dùng reporter nào. Các reporter cách nhau bởi dấu phẩy.
      • Ví dụ:
      npx playwright merge-reports --reporter=html,github ./blob-reports
      
    - `--config path/to/config/file`: đường dẫn đến file config cho việc merge report. Lưu ý là config này có thể khác config chạy test nha.
    - Ví dụ:
    
    ```bash
    npx playwright merge-reports --config=merge.config.ts ./blob-reports
    
    // merge.config.ts
    export default {
      testDir: 'e2e',
      reporter: [['html', { open: 'never' }]],
    };
    

Trả lời