Python-Playwright

Playwright Python Test Automation Project Structure

Here is a well-organized mockup of the project structure for a Python-based UI & API testing framework leveraging Playwright, Pytest, Page Object Model (POM), and GitHub with features such as nightly builds, push builds, reusable utilities, and parallel execution.


Project Structure

project/
│
├── .github/
│   ├── workflows/
│   │   ├── nightly_build.yml
│   │   ├── push_build.yml
│   │
│   ├── CODEOWNERS
│   └── pull_request_template.md
│
├── tests/
│   ├── ui/
│   │   ├── __init__.py
│   │   ├── test_login.py
│   │   ├── test_dashboard.py
│   │
│   ├── api/
│   │   ├── __init__.py
│   │   ├── test_get_user.py
│   │   ├── test_post_login.py
│   │
│   ├── conftest.py
│
├── pages/
│   ├── __init__.py
│   ├── login_page.py
│   ├── dashboard_page.py
│
├── utilities/
│   ├── __init__.py
│   ├── config.py
│   ├── logger.py
│   ├── api_helper.py
│   ├── data_loader.py
│
├── .gitignore
├── README.md
├── pytest.ini
├── requirements.txt
└── playwright.config.json

Key Files and Examples

1. GitHub Workflows

.github/workflows/nightly_build.yml
name: Nightly Build

on:
  schedule:
    - cron: "0 0 * * *"

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.10

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run Tests
        run: pytest --alluredir=reports/ --browser=all --headless
.github/workflows/push_build.yml
name: Push Build

on:
  push:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.10

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run Tests
        run: pytest --alluredir=reports/ --browser=all --headless

2. CODEOWNERS

.github/CODEOWNERS

# Code owners for specific paths
* @username1 @username2

3. Pull Request Template

.github/pull_request_template.md

### Description

Please include a summary of the change and relevant context.

### Checklist

- [ ] Tests have been added/updated.
- [ ] Code follows the coding guidelines.
- [ ] README.md updated (if required).
- [ ] No build warnings/errors.

4. Tests

tests/ui/test_login.py
import pytest
from pages.login_page import LoginPage

@pytest.mark.ui
def test_login(setup_browser):
    login_page = LoginPage(setup_browser)
    login_page.open()
    login_page.login("username", "password")
    assert login_page.is_login_successful()
tests/api/test_get_user.py
import pytest
from utilities.api_helper import ApiHelper

@pytest.mark.api
def test_get_user_details():
    response = ApiHelper.get("/users/1")
    assert response.status_code == 200
    assert response.json()["id"] == 1

5. Pages

pages/login_page.py
from playwright.sync_api import Page

class LoginPage:
    def __init__(self, page: Page):
        self.page = page
        self.url = "https://example.com/login"
        self.username_field = "#username"
        self.password_field = "#password"
        self.login_button = "#login"

    def open(self):
        self.page.goto(self.url)

    def login(self, username, password):
        self.page.fill(self.username_field, username)
        self.page.fill(self.password_field, password)
        self.page.click(self.login_button)

    def is_login_successful(self):
        return self.page.is_visible("#dashboard")

6. Utilities

utilities/api_helper.py
import requests

class ApiHelper:
    BASE_URL = "https://api.example.com"

    @staticmethod
    def get(endpoint, params=None):
        return requests.get(f"{ApiHelper.BASE_URL}{endpoint}", params=params)

    @staticmethod
    def post(endpoint, data=None):
        return requests.post(f"{ApiHelper.BASE_URL}{endpoint}", json=data)
utilities/logger.py
import logging

def get_logger(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger

7. Config and Parallel Execution

pytest.ini
[pytest]
addopts = -v --browser=all --headed --workers=4
markers =
    ui: UI tests
    api: API tests
playwright.config.json
{
  "use": {
    "headless": true,
    "trace": "on",
    "screenshot": "on"
  }
}

8. README.md

# Python Playwright Automation Framework

## Features
- UI & API Testing
- Page Object Model
- Parallel Execution
- GitHub Integration

## How to Run
1. Install dependencies: `pip install -r requirements.txt`
2. Run tests: `pytest`
3. View reports: Check `reports/` directory.

## Folder Structure
- `pages/`: Page Object Models
- `tests/`: Test files
- `utilities/`: Helper utilities

This structure provides a solid foundation for a scalable, maintainable, and modern Python-based automation testing framework.