π’ Test Reporters: Your Test Results Storytellers
The Newspaper Analogy π°
Imagine you run a toy factory. Every day, quality checkers test hundreds of toys. But how do you know what happened? You need a reporter β someone who writes down everything that happened and tells you the story!
In Playwright, Test Reporters are like newspaper reporters for your tests. They watch your tests run and then write a βnews reportβ about what passed, what failed, and why.
π Built-in Reporters: Your News Team
Playwright comes with a team of reporters ready to work. Each one writes their report in a different style!
Meet the Team:
| Reporter | What It Does | Best For |
|---|---|---|
| list | Simple text list | Quick checks |
| line | One-liner updates | CI pipelines |
| dot | Just dots (. or F) | Minimal output |
| html | Beautiful webpage | Sharing results |
| json | Computer-readable | Other tools |
| junit | XML format | Jenkins/CI |
How to Pick a Reporter
// playwright.config.js
export default {
reporter: 'list'
};
Thatβs it! One line tells Playwright which reporter to use.
π HTML Reporter: The Picture Book
The HTML Reporter creates a beautiful, colorful webpage showing all your test results. Itβs like turning your test results into a picture book!
Why Kids (and Adults) Love It:
- Pretty colors β Green for pass, red for fail
- Click to explore β See details of any test
- Screenshots β See what went wrong
- Videos β Watch the test replay!
Setting It Up
// playwright.config.js
export default {
reporter: [
['html', {
outputFolder: 'my-report',
open: 'never'
}]
]
};
Opening Your Report
npx playwright show-report
This opens the report in your browser. Itβs like opening a book full of pictures!
graph TD A["Tests Run"] --> B["HTML Reporter Watches"] B --> C["Creates Pretty Webpage"] C --> D["You Open in Browser"] D --> E["See Colors & Screenshots!"]
π JSON Reporter: The Robotβs Notebook
The JSON Reporter writes everything in a special language that computers understand perfectly. Itβs like a robot taking notes!
When to Use JSON:
- Building dashboards
- Sending results to other tools
- Storing results in databases
- Creating custom reports
Example Setup
// playwright.config.js
export default {
reporter: [
['json', {
outputFile: 'results.json'
}]
]
};
What JSON Looks Like
{
"suites": [{
"title": "Login Tests",
"specs": [{
"title": "should login",
"ok": true,
"duration": 1234
}]
}]
}
Itβs organized data that programs can easily read and use!
π JUnit Reporter: The Factory Standard
JUnit is like a universal language that all CI/CD systems understand. Itβs been around forever and works everywhere!
Where JUnit Works:
- Jenkins β The most popular CI tool
- GitHub Actions β Reads JUnit automatically
- Azure DevOps β Native support
- CircleCI β Built-in parsing
Setup Example
// playwright.config.js
export default {
reporter: [
['junit', {
outputFile: 'results.xml'
}]
]
};
What JUnit XML Looks Like
<testsuites>
<testsuite name="Login" tests="3">
<testcase name="login works"/>
<testcase name="logout works"/>
</testsuite>
</testsuites>
Think of JUnit as the universal translator that lets any CI system understand your test results!
π¨ Custom Reporters: Be Your Own Storyteller!
What if the built-in reporters donβt tell the story YOU want? Create your own custom reporter!
A Custom Reporter is Like:
- A diary that writes exactly what you care about
- A special notification system
- Your own news channel
Building Your First Custom Reporter
// my-reporter.js
class MyReporter {
onBegin(config, suite) {
console.log('π Starting tests!');
}
onTestEnd(test, result) {
const emoji = result.status === 'passed'
? 'β
' : 'β';
console.log(`${emoji} ${test.title}`);
}
onEnd(result) {
console.log('π All done!');
}
}
export default MyReporter;
Using Your Custom Reporter
// playwright.config.js
export default {
reporter: './my-reporter.js'
};
Available Events You Can Listen To:
| Event | When It Fires |
|---|---|
onBegin |
Tests start |
onTestBegin |
Each test starts |
onTestEnd |
Each test ends |
onEnd |
All tests done |
onError |
Something breaks |
graph TD A["onBegin"] --> B["onTestBegin"] B --> C["Test Runs..."] C --> D["onTestEnd"] D --> E{More Tests?} E -->|Yes| B E -->|No| F["onEnd"]
π Multiple Reporters: The Dream Team
Why pick just one reporter when you can have them ALL working together?
Real-World Example:
// playwright.config.js
export default {
reporter: [
['list'],
['html', { open: 'never' }],
['json', { outputFile: 'results.json' }],
['junit', { outputFile: 'results.xml' }]
]
};
What Happens:
- list β Shows progress in terminal
- html β Creates pretty report for humans
- json β Saves data for your dashboard
- junit β Feeds results to Jenkins
All at the same time! Itβs like having four reporters at a press conference, each writing for a different newspaper.
graph TD A["Your Tests"] --> B["Playwright"] B --> C["list - Terminal"] B --> D["html - Browser"] B --> E["json - Data File"] B --> F["junit - CI System"]
Mix Built-in + Custom
// playwright.config.js
export default {
reporter: [
['list'],
['./my-slack-reporter.js'],
['html']
]
};
Now you get terminal output, Slack notifications, AND an HTML report!
π― Quick Decision Guide
| Your Need | Use This |
|---|---|
| Quick terminal check | list or line |
| Share with team | html |
| Feed to dashboard | json |
| CI/CD integration | junit |
| Special needs | Custom reporter |
| All of the above | Multiple reporters! |
π Remember!
- Reporters = News writers for your tests
- Built-in = Ready to use, pick your style
- HTML = Beautiful pictures for humans
- JSON = Robot-readable data
- JUnit = Universal CI language
- Custom = Your own special reporter
- Multiple = Use as many as you need!
Your tests work hard. Make sure someone is there to tell their story! π°β¨
