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

Vọc Vạch Playwright

[Vọc Playwright] – Reporter (phần 2)

Tiếp tục phần 1: Reporter (phần 1)

Blob reporter

  • Blob reporter chứa tất cả thông tin về test run, có thể dùng để sinh ra các loại report.
  • Chức năng chính của blob reporter là dùng để merge report từ các sharded test.
  • Để dùng reporter blob thì chạy từ command line:
npx playwright test --reporter=blob
  • Mặc định thì report sẽ được ghi vào thư mục blob-reporter.
  • Report file name sẽ có format:
    • report-<hash>.zip
    • report-<hash>-<shard_number>.zip
  • Hash là giá trị được tính tóan từ các input: --grep, --grepInverted, --project và các filter pass vào từ commandline.
  • Mục tiêu của hash là sẽ sinh ra các report khác nhau dựa trên các report name.
  • Output file name có thể được overriden bằng cách dùng biến môi trường PLAYWRIGHT_BLOB_OUTPUT_FILE hoặc dùng file config:
import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: [['blob', { outputFile: `./blob-report/report-${os.platform()}.zip` }]],
});
  • Blob support một số loại tham số như sau:
Tên biến môi trường Reporter Config Option Mô tả Default
PLAYWRIGHT_BLOB_OUTPUT_DIR outputDir Thư mục dùng để lưu report. Dữ liệu cũ sẽ bị xóa đi trước khi dữ liệu mới được tạo ra. blob-report
PLAYWRIGHT_BLOB_OUTPUT_NAME fileName Report file name. report-<project>-<hash>-<shard_number>.zip
PLAYWRIGHT_BLOB_OUTPUT_FILE outputFile Đường dẫn đầy đủ đến output file. Nếu định nghĩa tham số này, output dir và output file name sẽ bị bỏ qua undefined

JUnit reporter

  • JUnit reporter là kiểu report dạng xml
  • Dùng bằng cách pass vào terminal
PLAYWRIGHT_JUNIT_OUTPUT_NAME=results.xml npx playwright test --reporter=junit
  • Dùng bằng cách sửa config file:
import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: [['junit', { outputFile: 'results.xml' }]],
});
  • JUnit reporter có các biến môi trường và option như sau:
Tên biến môi trường Reporter Config Option Mô tả Giá trị mặc định
PLAYWRIGHT_JUNIT_OUTPUT_DIR Directory to save the output file. Ignored if output file is not specified. cwd or config directory.
PLAYWRIGHT_JUNIT_OUTPUT_NAME outputFile Base file name for the output, relative to the output dir. JUnit report is printed to the stdout.
PLAYWRIGHT_JUNIT_OUTPUT_FILE outputFile Full path to the output file. If defined, PLAYWRIGHT_JUNIT_OUTPUT_DIR and PLAYWRIGHT_JUNIT_OUTPUT_NAME will be ignored. JUnit report is printed to the stdout.
stripANSIControlSequences Whether to remove ANSI control sequences from the text before writing it in the report. By default output text is added as is.
includeProjectInTestName Whether to include Playwright project name in every test case as a name prefix. By default not included.
PLAYWRIGHT_JUNIT_SUITE_ID Value of the id attribute on the root <testsuites/> report entry. Empty string.
PLAYWRIGHT_JUNIT_SUITE_NAME Value of the name attribute on the root <testsuites/> report entry. Empty string.

Github Action annotations

  • Có kiểu reporter riêng support cho github, anh em có thể pass vào trong file config
import { defineConfig } from '@playwright/test';

export default defineConfig({
  // 'github' for GitHub Actions CI to generate annotations, plus a concise 'dot'
  // default 'list' when running locally
  reporter: process.env.CI ? 'github' : 'list',
});

Custom reporter

  • Bạn cũng có thể tự mình tạo ra 1 reporter riêng thông qua implement Reporter class.
  • Theo kinh nghiệm của mình thì bạn cần hiểu kĩ về flow hoạt động của một test, các hooks. Đặc biệt là chỉ có onTestEnd support async.
import type {
  FullConfig, FullResult, Reporter, Suite, TestCase, TestResult
} from '@playwright/test/reporter';

class MyReporter implements Reporter {
  onBegin(config: FullConfig, suite: Suite) {
    console.log(`Starting the run with ${suite.allTests().length} tests`);
  }

  onTestBegin(test: TestCase, result: TestResult) {
    console.log(`Starting test ${test.title}`);
  }

  onTestEnd(test: TestCase, result: TestResult) {
    console.log(`Finished test ${test.title}: ${result.status}`);
  }

  onEnd(result: FullResult) {
    console.log(`Finished the run: ${result.status}`);
  }
}

export default MyReporter;
  • Sửa file config sử dụng reporter của bạn
import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: './my-awesome-reporter.ts',
});
  • Hoặc có thể pass trực tiếp vào terminal:
npx playwright test --reporter="./myreporter/my-awesome-reporter.ts"

Reporter bên thứ ba

Trả lời