Reporters (HTML, List, JUnit, Blob, JSON & Custom Reporter)
automationplaywright
Reporter quyết định kết quả test được hiển thị và lưu trữ thế nào sau khi chạy. Playwright đi kèm sẵn HTML report kèm trace/screenshot, các reporter console (list/line/dot), JUnit/JSON để CI ingest, blob để merge khi sharding — và cho phép chạy nhiều reporter đồng thời hoặc tự viết custom reporter.
Khái niệm & Bản chất
- Reporter là module nhận stream sự kiện trong suốt test run (bắt đầu suite, kết thúc từng test, kết thúc run) rồi xuất kết quả ra console, file, hoặc UI tĩnh.
- Có thể chạy nhiều reporter cùng lúc — ví dụ
listđể xem console +htmlđể debug +junitcho Jenkins, cấu hình bằng mảng. - HTML reporter sinh web app tĩnh trong
playwright-report/— xem bằngnpx playwright show-report, chứa trace viewer, screenshot, video, attachment của từng test. - Blob reporter xuất file
.zipchứa raw result + toàn bộ artefact — KHÔNG render ngay, được thiết kế đểmerge-reportsgộp lại khi chạy shard trên nhiều máy. - Ngoài built-in còn có hệ sinh thái bên thứ ba:
allure-playwright,monocart-reporter,@reportportal... cài qua npm và dùng như reporter thường.
Cách hoạt động & API chính
- Config:
reporter: 'line'cho một reporter, hoặc[['list'], ['html', { open: 'never' }]]cho nhiều reporter kèm option. CLI--reporter=line,junitoverride config. - Các reporter built-in:
list(từng test một dòng),line(gọn hơn, cập nhật tại chỗ),dot(mỗi test một ký tự — hợp suite lớn),html,json,junit,blob,github(annotate lỗi lên PR của GitHub Actions),null(tắt output). jsonvàjunitcầnoutputFileđể ghi ra file; không khai thì in ra stdout (lẫn với reporter console khác).- Option hay dùng của
html:outputFolder(đổi thư mục),open('always' | 'on-failure' | 'never'),attachmentsBaseURL(trỏ artefact lên storage ngoài). - Custom reporter là một class export default implement interface
Reporter— các hook:onBegin,onTestBegin,onTestEnd,onEnd,onError,printsToStdio().
Ví dụ & Cú pháp thực tế
- Cấu hình nhiều reporter cho CI hoàn chỉnh:
``
ts // playwright.config.ts import { defineConfig } from '@playwright/test'; export default defineConfig({ reporter: [ ['list'], // log từng test ra console ['html', { open: 'never' }], // playwright-report/index.html ['junit', { outputFile: 'results/junit.xml' }], // để Jenkins/GitLab ingest ['json', { outputFile: 'results/results.json' }], // xử lý bằng script khác ['github'], // annotate fail lên PR (chỉ tác dụng trên GHA) ], });`` - Xem HTML report và tuỳ biến qua env:
``
bash npx playwright show-report # serve playwright-report/ local CI=1 npx playwright test # trên CI, html reporter tự open:'never' PLAYWRIGHT_HTML_OPEN=never npx playwright test # tắt auto-mở sau run fail`` - Custom reporter tối giản — in từng test với status và duration:
``
ts // reporters/my-reporter.ts import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter'; class MyReporter implements Reporter { onTestEnd(test: TestCase, result: TestResult) { const status = result.status.toUpperCase().padEnd(7); console.log(${status} ${test.title} (${result.duration}ms)); } onEnd({ status, duration }: { status: string; duration: number }) { console.log(Run kết thúc: ${status} sau ${duration}ms); } } export default MyReporter; // dùng trong config: reporter: [['./reporters/my-reporter.ts']]``
Best practices & Khi nào dùng
- Local:
list(hoặcline) cho console +htmlđể mở khi cần debug — đừng tắt html vì nó là nơi xem trace/screenshot. - CI:
githubcho GitHub Actions,junitcho Jenkins/GitLab test report,blobkhi sharding,jsonnếu cần xử lý kết quả bằng script. - Đặt
open: 'never'cho html trong config để không bị auto mở browser sau mỗi run fail ở local; xem chủ động bằngnpx playwright show-report. dotphù hợp khi suite hàng nghìn test — console gọn, vẫn thấy tiến độ.- Custom reporter chỉ nên viết khi cần tích hợp đặc thù (đẩy kết quả vào hệ thống nội bộ, gửi Slack summary) — với nhu cầu phổ thông, JSON reporter + script nhỏ đã đủ.
Gotchas, So sánh & Lưu ý
- HTML reporter render ngay tại thời điểm chạy — khi shard trên nhiều máy, mỗi máy sinh một report riêng KHÔNG gộp được; đó là lý do tồn tại blob + merge-reports.
- Khi dùng nhiều reporter,
json/junitkhông cóoutputFilesẽ in JSON thô ra stdout — trộn lẫn output củalistvà làm log CI khó đọc. githubreporter chỉ có tác dụng trong GitHub Actions (đọc env của runner); chạy local nó tự im lặng, không hại gì.- So với JUnit: HTML report giàu thông tin hơn nhiều (trace, video, attachment) nhưng là file tĩnh khó integrate; JUnit là XML chuẩn mà mọi CI hiểu — thường dùng CẢ HAI.
- Custom reporter nhận
FullConfigtrongonBegin— đọc được mọi option config nhưng KHÔNG can thiệp được vào việc chạy test. - Allure/ReportPortal cho dashboard lịch sử theo thời gian (trend flaky, duration) — thứ mà HTML report một-lần-chạy không có; cân nhắc khi team cần metrics dài hạn.
