Professional Test Automation Framework using Python's unittest (PyUnit) and Selenium WebDriver.
- π― PyUnit (unittest) - Python's built-in testing framework
- π Selenium WebDriver - Multi-browser automation
- π HTML Reports - Beautiful test execution reports
- π Loguru Logging - Professional logging system
- π CI/CD Ready - Jenkins & GitHub Actions support
- πΈ Screenshots - Auto-capture on failures
- π¨ Page Object Model - Clean, maintainable architecture
- β 10 Test Cases - Comprehensive coverage
- Python 3.8 or higher
- pip (Python package manager)
# Clone or extract the framework
cd pyunit-selenium-framework
# Create virtual environment (recommended)
python3 -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Copy example environment file
cp .env.example .env
# Edit .env with your settings (optional)
nano .env# Run all tests with HTML report
python3 run_tests.py
# Or use the bash script
./run_tests.sh
# Run specific browser
./run_tests.sh chrome
./run_tests.sh firefox
./run_tests.sh edge
# Run in headed mode (see browser)
./run_tests.sh headed
# Run in headless mode (no UI)
./run_tests.sh headless# HTML report will be in reports/ directory
open reports/PyUnit_Selenium_Test_Report.htmlpyunit-selenium-framework/
βββ config/
β βββ __init__.py
β βββ config.py # Configuration management
β βββ logger.py # Logging configuration
βββ helpers/
β βββ __init__.py
β βββ api_helper.py # API integration helper
βββ pages/
β βββ __init__.py
β βββ base_page.py # Base page object
β βββ login_page.py # Login page object
βββ tests/
β βββ __init__.py
β βββ base_test.py # Base test class
β βββ test_login.py # Login test cases
βββ screenshots/ # Test screenshots
βββ logs/ # Execution logs
βββ reports/ # HTML test reports
βββ requirements.txt # Python dependencies
βββ run_tests.py # Test runner script
βββ run_tests.sh # Bash test runner
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore file
βββ README.md # This file
All test cases are based on https://the-internet.herokuapp.com/login
- β test_01_successful_login - Verify successful login with valid credentials
- β test_02_invalid_username - Verify login fails with invalid username
- β test_03_invalid_password - Verify login fails with invalid password
- β test_04_empty_credentials - Verify login fails with empty credentials
- β test_05_successful_logout - Verify successful logout after login
- β test_06_login_page_elements - Verify login page elements validation
- β test_07_special_characters - Verify login with special characters
- β test_08_case_sensitivity - Verify username case sensitivity
- β test_09_clear_form - Verify clear login form functionality
- β test_10_page_title - Verify page title
# Run specific test file
python3 -m unittest tests.test_login
# Run specific test class
python3 -m unittest tests.test_login.LoginTests
# Run specific test method
python3 -m unittest tests.test_login.LoginTests.test_01_successful_login
# Run with verbose output
python3 -m unittest tests.test_login -v# Browser Configuration
BROWSER=chrome # chrome, firefox, edge
HEADLESS=true # true or false
# Base URL
BASE_URL=https://the-internet.herokuapp.com
# Timeouts (seconds)
IMPLICIT_WAIT=10
EXPLICIT_WAIT=30
PAGE_LOAD_TIMEOUT=30
# Screenshots
SCREENSHOT_ON_FAILURE=true
# Test Data
VALID_USERNAME=tomsmith
VALID_PASSWORD=SuperSecretPassword!# Set browser
export BROWSER=firefox
python3 run_tests.py
# Set headless mode
export HEADLESS=false
python3 run_tests.py
# Set base URL
export BASE_URL=https://your-app.com
python3 run_tests.py- Location:
reports/ - Auto-generated after each test run
- Includes test results, screenshots, and execution time
- Open in browser:
open reports/PyUnit_Selenium_Test_Report.html
- Location:
logs/test_execution.log - Rotating logs (max 10MB)
- 30-day retention
- Automatic compression
- Location:
screenshots/ - Auto-captured on test failures
- Named with test name and timestamp
- Example:
test_01_successful_login_20231215_143022.png
from pages.base_page import BasePage
class MyPage(BasePage):
# Locators
ELEMENT = (By.ID, 'element-id')
def __init__(self, driver):
super().__init__(driver)
def click_element(self):
self.click(self.ELEMENT)# Navigation
navigate_to(url)
# Element Interaction
click(locator)
type(locator, text)
get_text(locator)
# Waits
wait_for_element(locator)
wait_for_element_clickable(locator)
# Visibility
is_visible(locator)
is_element_present(locator)
# Utilities
take_screenshot(name)
get_current_url()
get_title()
execute_script(script)import unittest
from tests.base_test import BaseTest
from pages.your_page import YourPage
from config.logger import test_start, test_end
class YourTests(BaseTest):
def setUp(self):
super().setUp()
self.page = YourPage(self.driver)
def test_something(self):
test_start("TC-001: Test description")
# Test steps
self.page.do_something()
# Assertions
self.assertTrue(condition, "Error message")
test_end("TC-001", "PASSED")
if __name__ == '__main__':
unittest.main()PyUnit provides rich assertion methods:
# Equality
self.assertEqual(a, b)
self.assertNotEqual(a, b)
# Boolean
self.assertTrue(condition)
self.assertFalse(condition)
# Containment
self.assertIn(item, container)
self.assertNotIn(item, container)
# Null checks
self.assertIsNone(value)
self.assertIsNotNone(value)
# Exceptions
self.assertRaises(Exception, callable)pipeline {
agent any
stages {
stage('Setup') {
steps {
sh 'python3 -m venv venv'
sh 'source venv/bin/activate && pip install -r requirements.txt'
}
}
stage('Test') {
steps {
sh 'source venv/bin/activate && python3 run_tests.py'
}
}
stage('Report') {
steps {
publishHTML([
reportDir: 'reports',
reportFiles: 'PyUnit_Selenium_Test_Report.html',
reportName: 'Test Report'
])
}
}
}
}name: PyUnit Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run tests
run: |
python3 run_tests.py
- name: Upload report
uses: actions/upload-artifact@v2
with:
name: test-report
path: reports/# The framework uses webdriver-manager which auto-downloads drivers
# If issues persist, update webdriver-manager:
pip install --upgrade webdriver-manager# Ensure you're in the project root
cd pyunit-selenium-framework
# Verify Python path
export PYTHONPATH="${PYTHONPATH}:$(pwd)"# Check browser is installed
google-chrome --version # Chrome
firefox --version # Firefox
msedge --version # Edge
# Try headed mode to see errors
export HEADLESS=false
python3 run_tests.py# Increase timeouts in .env
EXPLICIT_WAIT=60
PAGE_LOAD_TIMEOUT=60Key dependencies and their purposes:
| Package | Version | Purpose |
|---|---|---|
| selenium | 4.15.2 | Browser automation |
| webdriver-manager | 4.0.1 | Auto driver management |
| loguru | 0.7.2 | Professional logging |
| html-testRunner | 1.2.1 | HTML report generation |
| requests | 2.31.0 | API integration |
| python-dotenv | 1.0.0 | Environment variables |
β
Use Page Object Model - Keep tests clean and maintainable
β
Explicit Waits - Wait for elements, don't use sleep
β
Descriptive Test Names - Use test_01_description format
β
Independent Tests - Each test should run standalone
β
Logging - Use logger for important actions
β
Screenshots - Capture on failures for debugging
β
Configuration - Use .env for environment-specific settings
β
Assertions - Use meaningful assertion messages
- Python unittest Documentation
- Selenium Python Documentation
- Page Object Model Pattern
- Loguru Documentation
Pravin Gamit
- Senior QA Automation Engineer
- 5+ years experience in test automation
ISC License - Free to use and modify
# Setup
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Run all tests
python3 run_tests.py
# Run specific browser
./run_tests.sh chrome
# Run in headed mode
./run_tests.sh headed
# Run specific test
python3 -m unittest tests.test_login.LoginTests.test_01_successful_login
# View report
open reports/PyUnit_Selenium_Test_Report.html
# View logs
tail -f logs/test_execution.logβ Happy Testing with PyUnit + Selenium! π