Master Selenium Testing with Python: A Step-by-Step Guide with Pytest and Allure
Want to automate web browser testing using Python? This guide provides a practical example of how to set up and run Selenium tests with Python, using Pytest and Allure reports for efficient and insightful results. Learn how to structure your project, manage dependencies, and generate comprehensive test reports.
Why Use Selenium and Python for Test Automation
- Automate repetitive tasks: Automate web browser interactions, saving time and reducing manual effort.
- Ensure web application quality: Identify bugs and regressions early, leading to more reliable software.
- Cross-browser compatibility: Test your application across different browsers and platforms.
- Detailed Reporting: Allure reports provide clear visualizations and detailed information about test execution.
Project Structure
The example project includes the following key directories:
- data: Stores test data, making tests more maintainable and readable.
- enums: Contains enumerations for different test scenarios.
- pages: Implements the Page Object Model, encapsulating web page interactions.
- tests: Holds the actual test scripts using Pytest.
- utilities: Provides helper functions and common utilities for the tests.
Tech Stack Breakdown: Powerful Tools for Robust Testing
This project leverages a range of powerful tools to streamline your Selenium testing process:
- Selenium: Automates web browsers for comprehensive UI testing.
- Pytest: A flexible and extensible testing framework for Python.
- Allure: Generates detailed and visually appealing test reports.
- Requests: Simplifies making HTTP requests for API testing or interacting with web services.
- Other Essential Libraries:
assertpy
,dataclasses-json
,pytest-base-url
,python-dotenv
, And more to enhance testing capabilities.
Step-by-Step Setup Guide: Get Your Environment Ready
Follow these steps to set up your testing environment:
-
Clone the Repository:
-
Create a Virtual Environment: Ensure project dependencies are isolated.
-
Windows:
-
Mac:
-
-
Install Poetry: A tool for dependency management.
-
Install Project Dependencies:
-
Create a
.env
File: Securely store sensitive information. Add the following properties to the.env
file:Parameter Description Example Value EMAIL Your email address for authentication "[email protected]"
PASSWORD Your secret password for authentication "your_secret_password"
VRT_APIURL Visual Regression Tracker API URL "https://vrt.example.com/api"
VRT_PROJECT Visual Regression Tracker Project ID "project_id"
VRT_CIBUILDID Visual Regression Tracker Build Number "build_number"
VRT_BRANCHNAME Visual Regression Tracker Branch Name "main"
VRT_APIKEY Visual Regression Tracker API Key "your_api_key"
VRT_ENABLESOFTASSERT Enable Soft Assertions True
(orFalse
)MAILINATOR_API_KEY API Key for Mailinator service "your_mailinator_api_key"
MAILINATOR_DOMAIN_NAME Domain name for Mailinator "your_mailinator_domain"
Execute your Selenium Python Tests
-
Basic Execution:
If you don't select a browser, chrome will be used as default.
-
Run tests based on tags:
Analyze Test Results with Allure Reporting
-
Install Allure Commandline: View test results locally or integrate with CI/CD pipelines.
-
Windows: (Using Scoop)
-
Mac:
-
-
View Results Locally:
-
View Results Online: View allure results via Github pages
Maximize Code Quality with Pre-Commit Hooks
These checks run automatically before each commit, ensuring code quality and consistency.
-
Run Pre-Commit Checks Automatically:
-
Bump Pre-Commit Hooks Version:
-
Run Pre-Commit Checks Manually On The Entire Project:
Dive Deeper: Selenium Python and Pytest Resources
Explore these articles for more insights and advanced techniques:
- Test Automation - How To Build a CI/CD Pipeline Using Pytest and GitHub Actions
- Test Automation - How To Attach Public IP Address to Allure report using Pytest and Requests
- Test Automation - Selenium Example Python Project 2022 Updates
- Test Automation - How To Add Git Version Control Data To Allure Report in Python
- Test Automation - How To Attach Session Storage, Local Storage, Cookies, and Console logs To Allure Report in Selenium Python
- Test Automation - How To Capture Full-Page Screenshots In Selenium Python Using Chrome DevTools Protocol
- Test Automation - How To Edit Cookies in Selenium Python
- Test Automation - Pre-Merge Testing with GitHub Actions: A Step-by-Step Guide
- Test Automation - How To Use Custom User Agent in Selenium Python or Playwright Python to Avoid Security Bots
- Test Automation - How to Use Dynamic Base URLs with Selenium And Playwright Python in GitHub Actions
By following this guide, you can effectively leverage Selenium and Python for robust and insightful web application testing.