This is a robust and scalable automation framework built using Playwright and Python. It follows the Page Object Model (POM) design pattern and supports cross-browser testing, cloud testing (BrowserStack/LambdaTest), and environment-specific configurations.
- Cross-Browser Testing: Supports Chromium, Firefox, and WebKit browsers.
- Cloud Integration: Works with BrowserStack and LambdaTest for cloud-based testing.
- Environment Configuration: Easily switch between
dev,qa, andprodenvironments. - Page Object Model: Clean separation of test logic and page-specific actions.
- Custom Logging: Integrated logging for better debugging and traceability.
- Parallel Execution: Supports parallel test execution using
pytest-xdist. - HTML Reports: Generates detailed HTML reports for test execution.
- Reusable Utilities: Common functions like waits, screenshots, and assertions.
project-root/
│
├── 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 # Login page locators
│ └── facebooklogin_page.json # Home 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)
- Python 3.8 or higher
- Playwright (
pip install playwright) - BrowserStack/LambdaTest credentials (for cloud testing)
-
Clone the Repository:
git clone https://github.com/DipankarDandapat/Playwright_Python_Framework.git cd Playwright_Python_Framework -
Install Dependencies:
pip install -r requirements.txt playwright install
-
Set Up Environment Variables:
- Create
.env.dev,.env.qa, and.env.prodfiles in theconfig/environments/folder. - Example
.env.dev:BASE_URL=https://dev.example.com BROWSERSTACK_USERNAME=your_username BROWSERSTACK_ACCESS_KEY=your_access_key LT_USERNAME=your_username LT_ACCESS_KEY=your_access_key TIMEOUT=30
- Create
-
Run Tests:
- Local Browser with chromium:
pytest testscases/facebook/ --cloud local --browser-engine chromium --env dev - headless Off mode:
pytest testscases/facebook/ --cloud local --browser-engine chromium --headless false --env dev
- BrowserStack:
pytest testscases/facebook/ -s -v --cache-clear --cloud browserstack --env prod
- LambdaTest:
pytest testscases/facebook/ -s -v --cache-clear --cloud lambdatest --env prod
- Local Browser with chromium:
import pytest
@pytest.mark.smoke
@pytest.mark.regression
@pytest.mark.parametrize("case", testcasedata["positive"])
def test_valid_login(facebook_login_page,case):
facebook_login_page.navigate_to_facebook()
facebook_login_page.enter_credentials(case["usename"], case["password"])
facebook_login_page.click_loginbutto()from .base_page import BasePage
class FacebookLoginPage(BasePage):
def __init__(self, page: Page,app_config):
super().__init__(page)
self.app_config = app_config
def navigate_to_facebook(self):
"""Navigate to the login page."""
self.page.goto(f"{self.app_config.facebook_base_url}")
def enter_credentials(self, emailid: str, password: str):
"""Enter email id and password."""
self.enter_text("email", emailid)
self.enter_text("password", password)
def click_loginbutto(self):
"""Click the login button."""
self.click("loginButton")pytest testscases\facebook\ --cloud local --browser-engine chromium --env devpytest testscases\facebook\test_facebook_createUser.py --cloud local --browser-engine chromium --env devpytest -n 4 tests/pytest --html=reports/report.html[pytest]
addopts = -v -s --html=reports/report.html --self-contained-html
testpaths = tests
log_cli = true
log_cli_level = INFOdef get_browser_capabilities(provider: str, test_name: str, app_config) -> dict:
"""Get browser capabilities for both cloud and local browsers."""
base_caps = {
"name": test_name,
"build": "Playwright Build",
"project": "Playwright Automation",
}
if provider == "browserstack":
return {
**base_caps,
"browserName": "chrome",
"os": "Windows",
"osVersion": "11",
"browserVersion": "latest",
"client.playwrightVersion": "1.42.0",
"browserstack.username": app_config.browserstack_username,
"browserstack.accessKey": app_config.browserstack_access_key,
"viewport": {"width": 1920, "height": 1080}
}
elif provider == "lambdatest":
return {
**base_caps,
"platform": "Windows 11",
"browserName": "Chrome",
"version": "latest",
"selenium_version": "4.8.0",
"pw:version": "1.42.0",
"LT_USERNAME":app_config.lambdatest_username,
"LT_ACCESS_KEY":app_config.lambdatest_access_key,
"viewport": {"width": 1920, "height": 1080}
}
elif provider == "local":
return {
**base_caps,
"viewport": {"width": 1920, "height": 1080} # Default resolution for local browsers
}
return {}Logs are stored in the logs/ directory. Each test run generates a new log file with timestamps.
Example Log:
09_03_2025 11:25:59 AM -(conftest.py:121)- [INFO] Testcase.....test_valid_login[case0].....Start now ..........................................................
09_03_2025 11:25:59 AM -(base_page.py:31)- [INFO] Loaded elements from: D:\Playwright_Python_Framework\elements\facebooklogin_page.json
09_03_2025 11:26:01 AM -(base_page.py:76)- [INFO] Waiting for element 'email' to be visible
09_03_2025 11:26:01 AM -(base_page.py:97)- [INFO] Entering text 'admin' in 'email'
09_03_2025 11:26:01 AM -(base_page.py:76)- [INFO] Waiting for element 'password' to be visible
09_03_2025 11:26:01 AM -(base_page.py:97)- [INFO] Entering text 'admin123' in 'password'
09_03_2025 11:26:01 AM -(base_page.py:76)- [INFO] Waiting for element 'loginButton' to be visible
09_03_2025 11:26:01 AM -(base_page.py:82)- [INFO] Waiting for element 'loginButton' to be clickable
09_03_2025 11:26:01 AM -(base_page.py:90)- [INFO] Clicking on 'loginButton'
09_03_2025 11:26:06 AM -(conftest.py:124)- [INFO] Testcase.....test_valid_login[case0].....End now ............................................................
Watch this video to learn how to:
- Run tests locally and on cloud platforms.
- View logs and reports.
- Analyze test results with screenshots and videos.
- Debug failed tests efficiently.
[Playwright Python Framework Demo]
demo.mp4
HTML reports are generated in the reports/ directory. Open report.html to view detailed test results including screenshots of failed tests.
The HTML report includes:
- Summary of passed/failed tests
- Test execution time
- Screenshots of failures
- Links to video recordings
- Detailed logs and error messages
Example of integrating screenshots into your tests:
def test_feature(page, screenshot_on_failure):
try:
# Test code here
assert condition
except AssertionError:
page.screenshot(path=f"reports/screenshots/{test_name}_failure.png")
raise- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
- Playwright for the powerful automation library.
- BrowserStack and LambdaTest for cloud testing support.
- pytest for test execution and reporting.
For questions or feedback, please reach out to:
- Dipankar Dandapat
- Email: d.dandapat96@gmail.com
- GitHub: Dipankar Dandapat
