Many companies still use Jenkins. Especially enterprises in banking, insurance, and telecom. If your company uses Jenkins, here is how to set up Playwright. The core idea is the same as GitHub Actions -- install, run, report. The syntax is different.
pipeline {
agent {
docker {
image 'mcr.microsoft.com/playwright:v1.48.0-jammy'
args '-u root'
}
}
environment {
CI = 'true'
HOME = '/root'
}
stages {
stage('Install') {
steps {
sh 'npm ci'
}
}
stage('Run Tests') {
steps {
sh 'npx playwright test'
}
}
}
post {
always {
// Archive HTML report
publishHTML(target: [
reportDir: 'playwright-report',
reportFiles: 'index.html',
reportName: 'Playwright Report',
keepAll: true,
alwaysLinkToLastBuild: true
])
// Archive trace files
archiveArtifacts artifacts: 'test-results/**/*',
allowEmptyArchive: true
// Publish JUnit results (needs junit reporter)
junit testResults: 'test-results/results.xml',
allowEmptyResults: true
}
failure {
// Notify on failure
echo 'Tests failed! Check the Playwright report.'
}
cleanup {
cleanWs()
}
}
}If Docker is not available on your Jenkins agent, install browsers directly. You need root access for system dependencies.
pipeline {
agent any
tools {
nodejs 'Node-20' // Configured in Jenkins Global Tool Config
}
environment {
CI = 'true'
}
stages {
stage('Install Dependencies') {
steps {
sh 'npm ci'
}
}
stage('Install Browsers') {
steps {
// Requires sudo or root on the agent
sh 'npx playwright install --with-deps'
}
}
stage('Run Tests') {
steps {
sh 'npx playwright test'
}
}
}
post {
always {
publishHTML(target: [
reportDir: 'playwright-report',
reportFiles: 'index.html',
reportName: 'Playwright Report'
])
}
}
}Jenkins understands JUnit XML natively. Add the junit reporter to your Playwright config for test trend graphs.
export default defineConfig({
reporter: process.env.CI
? [
['html', { open: 'never' }],
['junit', { outputFile: 'test-results/results.xml' }],
['list'],
]
: [['html']],
});Use the Docker agent approach whenever possible. It eliminates the need to install browsers on Jenkins agents and ensures consistency. The -u root flag is required because Playwright needs root to install system dependencies inside the container.
Q: How do you run Playwright tests in Jenkins?
A: Two approaches. First, use the Playwright Docker image as the Jenkins agent -- this is cleaner and more portable. Define the pipeline with agent docker block pointing to the official Playwright image. Second, use a bare Jenkins agent with Node.js tool configured, and run npx playwright install --with-deps. For reporting, use publishHTML to archive the HTML report and the junit reporter for test trend graphs. Always set CI=true in the environment block.
Key Point: Use Playwright Docker image as Jenkins agent for clean setup. Add publishHTML for reports and junit reporter for test trends.
Key Point: For Jenkins, use the Playwright Docker image as the agent. Add publishHTML for reports and junit reporter for test trend graphs.