Web Test Automation Using Python, Pytest, and Selenium
- Thirupathi Rajan
- Apr 4
- 3 min read
Introduction
Automation testing is a critical part of software quality assurance, ensuring applications work
as expected across different environments. In this blog, I will share insights into our
Selenium test automation framework using Python and Pytest, with a focus on:
Pytest adoption
Page Object Model (POM) implementation
Data-driven testing
Custom reporting
Pytest Adoption for Flexible Test Execution
We leveraged Pytest to create a dynamic and scalable test framework. Some key benefits
include:
Dynamic Execution
Test cases run based on predefined conditions, allowing flexibility.
Command-Line Configurations
We can pass browser type (Chrome, Edge, Firefox) and environment (staging,
production, etc.) as arguments, making the test execution process more adaptable.
Conditional Execution
Test cases can be enabled, skipped, or executed with different configurations
depending on runtime parameters.
Page Object Model (POM) for Better Maintainability
To enhance test structure and maintainability, we implemented Page Object Model (POM),
which promotes code reusability and separation of concerns.
Reusable Utility Layer
A base layer handles core interactions with web elements, reducing redundancy.
Page-Specific Methods
Each web page has a dedicated Python file that encapsulates UI interactions and
business logic.
Cross-File Method Usage
Some methods are shared across multiple pages and test cases, ensuring
consistency.
Structured Test Execution
Test scripts in the test folder call functions from the pages folder, keeping test logic
organized and improving maintainability.
Implementing Data-Driven Testing with Excel
To increase test coverage and reusability, we incorporated data-driven testing by
fetching test data dynamically from an external source.
Excel-Based Test Data Storage
Test inputs are stored in an Excel sheet, making it easy to modify data without
altering the test script.
Reusable Data Extraction Method
A common function extracts data, ensuring it can be used across multiple test cases.
Dynamic Execution Based on Input Data
By modifying the Excel file, new test scenarios can be executed without updating the
script.
Custom Reporting for Better Test Insights
Pytest’s built-in reporting is minimal, so we implemented a custom reporting system for
improved visibility into test results.
Capturing Detailed Test Outcomes
We store individual test scenario results separately for better tracking.
Storing Results in JSON Format
Passed and failed test cases are recorded in a structured JSON file.
Converting JSON to HTML Reports
Extracted data is formatted into an interactive HTML report for easy review.
Interactive HTML Report Features
Graphical Insights – Pie charts visualize the pass/fail ratio.
Module-Based Summaries – The report categorizes tests by modules to track progress.
Drill-Down Test Details – Clicking on test counts reveals specific test case results,
making debugging easier.
Optimized Test Execution with Parallelism
By enabling parallel execution in Pytest, we significantly reduced test execution time and
improved efficiency.
Challenges and Learnings
Handling Dynamic Web Elements
Implemented robust wait mechanisms to improve test stability.
Cross-Browser Execution
Enabled tests to run on multiple browsers via command-line arguments.
Scalability & Maintainability
The POM structure and data-driven approach reduced maintenance efforts and
improved efficiency.
Multi-Instance Browser Testing
We implemented a unique multi-instance testing approach where:
1️⃣ Instance 1 launches Browser 1, modifies certain conditions.
2️⃣ Instance 2 launches Browser 2, validates the changes, and then closes.
3️⃣ Instance 1 continues testing with Browser 1, ensuring smooth validation across different
instances.
Conclusion
Our Python-Pytest-Selenium automation framework has become a key part of our
release validation process, ensuring reliable, efficient, and scalable testing.
Pytest adoption enabled dynamic execution, browser selection, and environment
configuration from the CLI.
Page Object Model (POM) improved test maintainability by separating UI interactions
from test logic.
Data-driven testing with Excel enhanced flexibility and test coverage.
Custom reporting provided structured, interactive insights for better debugging and
tracking.
Multi-instance browser testing allowed for cross-instance validation of changes
dynamically.
By leveraging these strategies, we have streamlined automation workflows, improved
reporting accuracy, and enhanced test efficiency.
Comments