LayoutTitle

Hoverable Dropdown

Sunday, September 28, 2025

Playwright with Python – Complete Setup & CI/CD Guide

 

1. Installation

🔹 Prerequisites

  • Install Python (≥3.8)Download

  • Install Node.js (≥16) (needed for browsers) → Download

🔹 Install Playwright

# Create project folder mkdir playwright_automation && cd playwright_automation # Create virtual environment python -m venv venv source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows # Install Playwright pip install pytest pytest-playwright # Install browsers playwright install

✅ This installs Chromium, Firefox, and WebKit engines for Playwright.


2. Configuration

🔹 Project Structure

playwright_automation/ │── tests/ │ ├── test_google.py │── conftest.py │── requirements.txt │── pytest.ini
  • tests/ → contains test cases

  • conftest.py → pytest fixtures (e.g., browser setup)

  • requirements.txt → dependencies

🔹 requirements.txt

pytest pytest-playwright

🔹 pytest.ini

[pytest] addopts = -v --headed --html=report.html --self-contained-html markers = smoke: quick checks regression: full suite

3. First Test (Development)

Create tests/test_google.py:

import pytest from playwright.sync_api import sync_playwright def test_google_search(): with sync_playwright() as p: browser = p.chromium.launch(headless=True) page = browser.new_page() page.goto("https://www.google.com") page.fill("input[name='q']", "Playwright Python") page.keyboard.press("Enter") assert "Playwright" in page.title() browser.close()

Run test:

pytest tests/ --headed
  • --headed → shows browser UI

  • Default is headless (faster)


4. Advanced Setup

🔹 Using Pytest Fixtures (Reusable Browsers)

Create conftest.py:

import pytest from playwright.sync_api import sync_playwright @pytest.fixture(scope="session") def browser(): playwright = sync_playwright().start() browser = playwright.chromium.launch(headless=True) yield browser browser.close() playwright.stop() @pytest.fixture def page(browser): context = browser.new_context() page = context.new_page() yield page context.close()

Now tests/test_google.py becomes:

def test_google_search(page): page.goto("https://www.google.com") page.fill("input[name='q']", "Playwright Python") page.keyboard.press("Enter") assert "Playwright" in page.title()

5. Execution

🔹 Run All Tests

pytest --headed --html=report.html

🔹 Run Specific Marker

pytest -m "smoke"

🔹 Parallel Execution

pytest -n 4

6. Jenkins CI/CD Integration

🔹 Step 1: Install Jenkins

🔹 Step 2: Jenkins Job

  1. Create a Pipeline Job

  2. Configure Git repo (with Playwright project)

  3. Add build steps:

# Inside Jenkins pipeline pip install -r requirements.txt playwright install pytest --html=report.html --self-contained-html
  1. Post-build actions → Publish HTML report:

    • HTML directory: .

    • Index page: report.html


🔹 Jenkinsfile (Pipeline as Code)

Place this in your repo:

pipeline { agent any stages { stage('Setup') { steps { sh 'python -m venv venv' sh '. venv/bin/activate && pip install -r requirements.txt' sh '. venv/bin/activate && playwright install' } } stage('Test') { steps { sh '. venv/bin/activate && pytest --html=report.html --self-contained-html' } } } post { always { publishHTML (target: [ reportDir: '.', reportFiles: 'report.html', keepAll: true, reportName: 'Playwright Test Report' ]) } } }

7. Scaling Up

  • Cross-browser execution:

    pytest --browser firefox pytest --browser webkit
  • Parallel test execution: pytest -n auto

  • Cloud Testing: Integrate with BrowserStack/Sauce Labs

  • Reporting: Allure Reports (pip install allure-pytest)

  • Dockerize your test framework for portability


8. Real-World Project Idea

📌 Automate an e-commerce site workflow with Playwright:

  • Search → Add to cart → Checkout → Validate order in DB

  • Cover UI + API + DB layers

  • Integrate with Jenkins for nightly regression runs


In summary:

  1. Install Python + Playwright

  2. Set up virtual environment + pytest config

  3. Write first test (test_google.py)

  4. Build framework with fixtures & POM

  5. Run tests locally + in parallel

  6. Integrate with Jenkins pipeline (CI/CD)

  7. Scale with reporting, Docker, Cloud execution