End-to-End Playwright Python Automation Framework Guide

End-to-End Playwright Python Automation Framework Guide


Introduction

Web automation is essential for efficient software testing, ensuring seamless user experiences across multiple browsers and platforms. This guide presents a scalable, maintainable Playwright Python automation framework designed for cross-browser testing, cloud platform execution (BrowserStack/LambdaTest), and environment-specific configurations.

By implementing the Page Object Model (POM), reusable utilities, logging, reporting, and environment management, this framework provides an enterprise-grade automation solution.

?? GitHub Repository: GitHub Repository


Framework Structure

Playwright_Python_Automation/
│
├── config/
│   ├── __init__.py
│   ├── config.py            # Environment configuration
│   ├── browser_capabilities.py  # Browser/cloud capabilities
│   └── environments/
│       ├── .env.dev          # Dev environment variables
│       ├── .env.qa            # QA environment variables
│       └── .env.prod        # Prod environment variables
│
├── elements/
│   ├── facebookCreateuser_page.json      # create user page locators
│   └── facebooklogin_page.json       # login page locators
│
├── pages/
│   ├── __init__.py
│   ├── base_page.py         # Base page class
│   ├── facebook_createuser_page.py        # Login page actions
│   └── facebook_login_page.py         # Home page actions
│
├── fixtures/
│   ├── __init__.py
│   └── pages.py             # Page object fixtures
│
├── utils/
│   ├── __init__.py
│   ├── file_reader.py       # Read test json data
│   ├── functions.py         # Reusable functions
│   ├── waits.py               # Custom wait strategies
│   └── logger.py             # Logging configuration
│
├── testscases/
│   ├── __init__.py
│   ├── conftest.py          # Pytest fixtures & hooks
│   └── facebook/
│       ├── test_facebook_createUser.py    # create user cases
│       └── test_facebook_login.py         # login page test cases
│  
├── testdata/
│   │  
│   └── facebook/
│       ├── facebook_createuser_data.json    # create user  test data
│       └── facebook_login_data.json         # login page test data
│              
├── pytest.ini               # Pytest configurations
├── requirements.txt         # Dependencies
│
├── reports/                 # Test reports (auto-generated)
└── Logs/                    # Execution logs (auto-generated)
        

Framework Components and Implementation

1. Configuration Management

Manages environment variables, base URLs, and browser capabilities.

Environment Configuration (config/config.py)


  • Loads .env files dynamically based on --env (dev/qa/prod).
  • Provides facebook_base_url for navigation.


Browser Capabilities (config/browser_capabilities.py)


Defines browser resolutions for local/cloud execution.


2. Page Object Model (POM)

Separates locators, actions, and test logic.

Locators (elements/facebooklogin_page.json)


Base Page (pages/base_page.py)


Loads locators dynamically from JSON files.

Login Page (pages/facebook_login_page.py)


3. Fixtures and Test Execution

Browser Fixture (conftest.py)


  • Initializes Playwright browser and config for test sessions.

Test Cases (testscases/facebook/test_facebook_login.py)



  • Uses @pytest.mark.smoke for test categorization.
  • Leverages login_page fixture for execution.


4. Utilities

Wait Strategies (utils/waits.py)


Logging (utils/logger.py)



Key Features

Cross-Browser Testing

  • Supports Chrome, Firefox, WebKit, BrowserStack, LambdaTest.
  • Example:

pytest testscases\facebook\ --cloud local --browser-engine chromium --env dev

pytest testscases\facebook\ --cloud local --browser-engine Firefox --env dev
        


Environment Configuration

  • Switch between dev/qa/prod dynamically.
  • Example:

pytest testscases\facebook\ --cloud local --browser-engine chromium --env qa

pytest testscases\facebook\ --cloud local --browser-engine chromium --env dev

pytest testscases\facebook\ --cloud local --browser-engine chromium --env prod
        

Cloud Integration

  • Supports BrowserStack/LambdaTest.
  • Example:

pytest testscases\facebook\ --cloud browserstack --env dev 

pytest testscases\facebook\ --cloud lambdatest--env dev 
        

Reports and Logs

  • Generates HTML reports via pytest-html.
  • Example:

pytest --html=reports/report.html
        

Retry Mechanism

  • Handles flaky tests with pytest-rerunfailures.
  • Example (pytest.ini):

addopts = --reruns 2
        

Conclusion

This Playwright framework offers a scalable and maintainable approach to web automation, ensuring test reliability and enterprise-level robustness. It integrates cross-browser support, cloud execution, environment management, reporting, and logging, making it ideal for large-scale automation projects.

?? Explore the Full Source Code on GitHub: GitHub Repository


要查看或添加评论,请登录

Dipankar Dandapat的更多文章