This repository contains a robust test automation framework built with Python, Playwright, and pytest. It is designed to test a web application with support for both desktop and mobile layouts, including advanced features like geolocation mocking and database verification.
- Python 3.14+
uvorpipfor dependency management
-
Clone the repository:
git clone <repository-url> cd vsc-python
-
Install dependencies:
uv sync # OR pip install -e .
-
Install Playwright browsers:
playwright install
Run all tests:
pytestRun mobile emulation tests:
pytest --mobile --target-browser webkitRun with specific geolocation:
pytest --lat 37.7749 --long -122.4194Generate Allure report:
allure serve reportThis project demonstrates several advanced patterns in test automation. Here is a breakdown of the key technical concepts implemented:
The project follows a strict POM design. The Application class (page_objects/application.py) serves as the Facade for the entire test suite.
- Central Entry Point: Tests interact primarily with the
appfixture, which is an instance ofApplication. - Component Composition: The
Applicationclass initializes specific page objects (e.g.,self.test_cases,self.demo_pages), keeping the API clean and hierarchical. - Global Handlers: The
Applicationconstructor sets up global event listeners for console errors and dialogs, ensuring that application errors are captured even if they don't fail the test immediately.
The conftest.py file is the engine of this framework, utilizing complex fixture logic:
- Fixture Factories: Fixtures like
auth_appandmobile_auth_appdynamically create browser contexts based on CLI options (mobile vs. desktop). - Session-Scoped Authentication: The
auth_storagefixture logs in once per session and saves the browser storage state (cookies/local storage) to a JSON file. Individual tests reuse this state, significantly speeding up execution. - Context Configuration: It demonstrates how to configure
browser.new_context()with specific permissions (e.g.,permissions=["geolocation"]) and device characteristics.
The framework supports mobile testing without needing real devices or Appium.
- Device Descriptors: An
IPHONE_15dictionary defines the user agent, screen size, viewport, and device scale factor. - Context Injection: This descriptor is unpacked into the browser context (
**IPHONE_15), forcing the desktop browser engine (Chromium/WebKit) to render the mobile view of the application.
Tests involving location services are deterministic thanks to Playwright's geolocation mocking.
- CLI Injection: Latitude and longitude can be passed via
--latand--longflags. - Permission Granting: The framework automatically grants the
geolocationpermission to the base URL, bypassing browser permission prompts that would otherwise block automation.
Instead of relying solely on UI verification, the framework uses a DataBase helper (helpers/db.py) to interact directly with the SQLite database.
- Usage: This is used to verify that data submitted via the UI is correctly persisted in the backend, or to clean up test data ensuring isolation.
- Step Logging: Methods in Page Objects are decorated with
@allure.step, creating a readable, step-by-step log of actions in the report. - Automatic Screenshots: A
pytest_runtest_makereporthook inconftest.pyautomatically captures a screenshot whenever a test fails and attaches it to the Allure report.