Step-by-step example with detailed workflow explanation
Playwright is a powerful tool for end-to-end testing in modern browsers (Chromium, Firefox, WebKit). It automates interactions like clicking buttons, filling forms, and validating page content. With GitHub Actions, you can run these tests automatically on every code change!
Let’s create a workflow that:
Runs Playwright tests on every push
to GitHub.
Generates a test report.
Uploads the report as an artifact for debugging.
Step 1: Set Up Your Playwright Project
Initialize a Node.js project:
npm init -y
Install Playwright:
npm install --save-dev @playwright/test
Create a sample test file (e.g., tests/example.spec.js
):
const { test, expect } = require('@playwright/test');
test('Check GitHub title', async ({ page }) => {
await page.goto('https://github.com');
await expect(page).toHaveTitle('GitHub: Let’s build from here · GitHub');
});
Run tests locally to verify:
npx playwright test
Step 2: Create the GitHub Actions Workflow
Create .github/workflows/playwright.yml
and add this fully explained configuration:
name: Playwright Tests
on: [push] # Trigger on every push to any branch
jobs:
test-browsers:
# Run tests on a GitHub-hosted Ubuntu machine
runs-on: ubuntu-latest
# Configure timeouts and retries (optional)
timeout-minutes: 10
strategy:
retry: 1 # Retry failed tests once
steps:
# Step 1: Checkout code from the repo
- name: Checkout repository
uses: actions/checkout@v4
# Step 2: Set up Node.js with a specific version
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20' # Playwright works best with Node 18+
# Step 3: Install dependencies (including Playwright)
- name: Install dependencies
run: npm ci # Uses package-lock.json for exact versions
# Step 4: Install Playwright browsers & system dependencies
- name: Install Playwright
run: npx playwright install --with-deps
# Step 5: Run Playwright tests
- name: Run tests
run: npx playwright test
# Step 6: Upload HTML test report as an artifact (if tests fail)
- name: Upload test report
if: failure() # Only upload on failure
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: playwright-report/
name: Playwright Tests
Friendly name for your workflow (visible in the Actions tab).
on: [push]
Triggers the workflow every time code is pushed to any branch.
runs-on: ubuntu-latest
Uses a clean Ubuntu virtual machine provided by GitHub.
timeout-minutes
and retry
Prevents hung workflows and retries flaky tests once.
Steps Explained:
Checkout repository: Copies your code into the workflow environment.
Setup Node.js: Installs Node.js v20 (required for Playwright).
Install dependencies: Uses npm ci
for a faster, secure install.
Install Playwright: Installs browsers (Chromium, Firefox, WebKit) and system dependencies (like libraries for headless mode).
Run tests: Executes all Playwright tests in the tests/
folder.
Upload test report: If tests fail, uploads the playwright-report/
folder as a downloadable artifact for debugging.
GitHub spins up a fresh Ubuntu machine.
Your code is cloned, Node.js is set up, and dependencies are installed.
Playwright downloads browsers (takes 1-2 minutes on first run).
Tests run headlessly (no visible browser UI).
Results:
Pass: Workflow completes with a green checkmark.
Fail: Red X appears, and you can download the playwright-report
to see screenshots, traces, and error logs.
Add this step after Install dependencies to cache Playwright browsers:
- name: Cache Playwright browsers
uses: actions/cache@v4
with:
path: |
~/.cache/ms-playwright
key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}
Test login flows, checkout processes, or API calls.
Run tests across multiple browsers in parallel.
Integrate with CI/CD pipelines to block broken code from deploying.
Catch visual regressions: Playwright takes screenshots on failure.
Test cross-browser compatibility: Run against Chromium, Firefox, and WebKit.
Debug easily: Download HTML reports with traces and videos.
Final Tip: Check the Playwright GitHub Action docs for advanced configurations like parallel testing or sharding!
(Now go automate those tests—your team will love you for it!) 🚀
Join Jagadhiswaran on Peerlist!
Join amazing folks like Jagadhiswaran and thousands of other people in tech.
Create ProfileJoin with Jagadhiswaran’s personal invite link.
0
10
0