Integrating Playwright with Jenkins
Integrating Playwright with Jenkins
Integrating Playwright with Jenkins allows you to automate your browser tests in a CI/CD pipeline. This is useful for running end-to-end tests on every code push or pull request.
Here’s a complete guide to set up and run Playwright tests in Jenkins:
✅ Prerequisites
Jenkins installed (locally or on a server).
Node.js installed on your Jenkins machine.
Playwright test project ready (i.e., npm init playwright@latest).
Git installed (for pulling the repo).
๐งฑ Step-by-Step Integration Guide
1. Install Playwright and Set Up Tests (Locally or in Repo)
If you haven't yet:
bash
Copy
Edit
npm init playwright@latest
This sets up:
playwright.config.ts
Example tests
tests/ directory
Install browsers:
bash
Copy
Edit
npx playwright install
2. Create a Jenkins Job (Freestyle or Pipeline)
Option A: Freestyle Project
Go to Jenkins Dashboard → New Item → Select Freestyle Project.
Configure the Git repository (under "Source Code Management").
In the Build section, add a Build Step → Execute shell:
bash
Copy
Edit
# Install dependencies
npm ci
# Install Playwright browsers
npx playwright install --with-deps
# Run tests
npx playwright test
๐ง Add --reporter=html to generate an HTML report:
npx playwright test --reporter=html
Option B: Declarative Pipeline (Jenkinsfile)
In your project root, create a Jenkinsfile:
groovy
Copy
Edit
pipeline {
agent any
tools {
nodejs 'NodeJS-18' // Or whatever version you configured in Jenkins
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo/playwright-project.git'
}
}
stage('Install Dependencies') {
steps {
sh 'npm ci'
sh 'npx playwright install --with-deps'
}
}
stage('Run Tests') {
steps {
sh 'npx playwright test --reporter=html'
}
}
stage('Publish Report') {
steps {
publishHTML(target: [
reportDir: 'playwright-report',
reportFiles: 'index.html',
reportName: 'Playwright Test Report'
])
}
}
}
}
Make sure to install the HTML Publisher Plugin in Jenkins if you want to publish Playwright reports.
3. Environment Requirements for Jenkins Agent
If Jenkins is running on a headless server (Linux):
Install required dependencies for Playwright (Chrome, WebKit, etc.) using:
bash
Copy
Edit
npx playwright install --with-deps
Or manually:
bash
Copy
Edit
sudo apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libxkbcommon0
4. View Test Results
If using --reporter=html, Playwright will generate an HTML report in playwright-report/. You can:
Archive it as an artifact
Serve it using the Jenkins HTML Publisher Plugin
✅ Summary
Task How to Do It
Install Playwright in Jenkins npm ci + npx playwright install --with-deps
Run tests npx playwright test
Generate report --reporter=html
Publish report Jenkins HTML Publisher Plugin
Automation method Freestyle build or Jenkins Pipeline (Jenkinsfile)
Would you like a sample GitHub repo or Dockerfile for Jenkins + Playwright?
Learn Playwright Training Course in Hyderabad
Read More
Using Environment Variables in Playwright Tests
Cross-Browser Testing with Playwright
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment