Enhanced Selenium BDD Framework with BehaveX
October 8, 2025 ยท View on GitHub
A comprehensive Behavior-Driven Development (BDD) test automation framework using Selenium WebDriver for UI testing and Python Requests for API testing, enhanced with BehaveX for parallel execution and advanced reporting.
๐ Features
- ๐ Dual Testing Support: UI testing with Selenium + API testing with Requests
- โก Parallel Execution: Run tests in parallel using BehaveX
- ๐ Enhanced Reporting: Allure, JUnit, HTML, and custom reports
- ๐๏ธ Page Object Pattern: Reusable page objects with JSON-based element management
- ๐ง Configuration Management: Environment-specific configurations
- ๐ Cross-Browser Testing: Chrome, Firefox, Edge support
- ๐ป Headless Execution: Run tests without browser UI
- ๐ Auto-Retry: Automatic retry for failed scenarios
- ๐ธ Evidence Collection: Screenshots, logs, and execution traces
- ๐ Test Muting: Skip specific tests dynamically
- ๐ Performance Metrics: Response time tracking and analysis
๐ Project Structure
bdd-framework/
โโโ ๐ config/ # Environment configurations
โ โโโ dev_config.json
โ โโโ stg_config.json
โ โโโ prod_config.json
โโโ ๐ elements/ # Element locators (JSON)
โ โโโ facebook_login_page.json
โ โโโ *.json
โโโ ๐ features/ # BDD feature files
โ โโโ ๐ steps/ # Step definitions
โ โ โโโ facebook_login_signup_steps.py
โ โ โโโ Todos_API_Testing_Steps.py
โ โโโ facebook_login_signup.feature
โ โโโ Todos_API_Testing.feature
โโโ ๐ pages/ # Page Object classes
โ โโโ base_page.py
โ โโโ facebook_login_signup_page.py
โโโ ๐ reports/ # Test reports and artifacts
โ โโโ ๐ allure-results/
โ โโโ ๐ junit/
โ โโโ ๐ screenshots/
โ โโโ ๐ logs/
โโโ ๐ testdata/ # Test data files
โ โโโ ๐ facebook/
โ โโโ facebook_login_data.json
โ โโโ facebook_createuser_data.json
โโโ ๐ utils/ # Utility classes
โ โโโ config_manager.py
โ โโโ logger.py
โ โโโ selenium_client.py
โ โโโ api_client.py
โ โโโ allure_helper.py
โ โโโ enhanced_reporter.py
โ โโโ parallel_runner.py
โ โโโ test_retry_handler.py
โ โโโ page_factory.py
โ โโโ file_reader.py
โโโ ๐ run_tests.py # Main test runner
โโโ ๐ environment.py # Behave environment setup
โโโ ๐ requirements.txt # Dependencies
โโโ ๐ README.md # This file
๐ ๏ธ Installation & Setup
Prerequisites
Before you begin, ensure you have:
- โ Python 3.8 or higher (Download Python)
- โ pip (Python package manager)
- โ Git (Download Git)
Step-by-Step Installation
1. Clone the Repository
git clone https://github.com/DipankarDandapat/Python_Selenium_BDD_Framework.git
cd Python_Selenium_BDD_Framework
2. Create Virtual Environment
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
3. Install Dependencies
# Install all required Python packages
pip install -r requirements.txt
4. Verify Installation
# Check installation
python run_tests.py --help
๐ฏ Quick Start
Run All Tests
python run_tests.py
Run Specific Test Types
# Run only UI tests
python run_tests.py --type ui
# Run only API tests (no browser launched)
python run_tests.py --type api
# Run smoke tests
python run_tests.py --type smoke
# Run regression tests
python run_tests.py --type regression
๐ Complete Command Reference
Basic Test Execution
| Command | Description |
|---|---|
python run_tests.py | Run all tests (UI + API) |
python run_tests.py --type ui | Run UI tests only |
python run_tests.py --type api | Run API tests only |
python run_tests.py --type smoke | Run smoke tests |
python run_tests.py --type regression | Run regression tests |
Environment Configuration
| Command | Description |
|---|---|
python run_tests.py --env dev | Run in development environment |
python run_tests.py --env stg | Run in staging environment |
python run_tests.py --env prod | Run in production environment |
Browser Configuration
| Command | Description |
|---|---|
python run_tests.py --browser chrome | Use Chrome browser (default) |
python run_tests.py --browser firefox | Use Firefox browser |
python run_tests.py --browser edge | Use Edge browser |
python run_tests.py --headless | Run browser in headless mode |
python run_tests.py --headed | Run browser in headed mode |
Parallel Execution
| Command | Description |
|---|---|
python run_tests.py --parallel 4 | Run with 4 parallel processes |
python run_tests.py --processes 8 | Run with 8 parallel processes |
python run_tests.py --type ui --parallel 4 | UI tests with 4 parallel processes |
python run_tests.py --type api --parallel 6 | API tests with 6 parallel processes |
Test Filtering with Tags
| Command | Description |
|---|---|
python run_tests.py --tags "@smoke" | Run tests with @smoke tag |
python run_tests.py --tags "@regression" | Run tests with @regression tag |
python run_tests.py --tags "@ui,@smoke" | Run UI smoke tests |
python run_tests.py --tags "@api,@regression" | Run API regression tests |
python run_tests.py --tags "~@wip" | Skip work-in-progress tests |
Reporting Options
| Command | Description |
|---|---|
python run_tests.py --allure | Generate Allure report |
python run_tests.py --junit | Generate JUnit reports |
python run_tests.py --allure --junit | Generate both reports |
python run_tests.py --verbose | Verbose output |
Advanced Execution Options
| Command | Description |
|---|---|
python run_tests.py --dry-run | Dry run without executing tests |
python run_tests.py --stop | Stop after first failure |
python run_tests.py --no-capture | Don't capture stdout/stderr |
python run_tests.py --tags-help | Show tags help |
Combined Examples
| Command | Description |
|---|---|
python run_tests.py --type api --env stg --parallel 4 --allure | API tests on staging with parallel execution and Allure |
python run_tests.py --type ui --browser firefox --headless --tags "@smoke" | UI smoke tests on Firefox headless |
python run_tests.py --type regression --parallel 8 --junit --stop | Regression tests with parallel execution, JUnit reports, stop on failure |
๐ง Configuration
Environment Configuration Files
Create environment-specific JSON files in config/ directory:
config/dev_config.json
{
"UI_BASE_URL": "https://www.facebook.com",
"API_BASE_URL": "https://dipankardandapat.xyz",
"BROWSER": "chrome",
"HEADLESS": false,
"IMPLICIT_WAIT": 10,
"PAGE_LOAD_TIMEOUT": 30,
"API_TIMEOUT": 30,
"SCREENSHOT_ON_FAILURE": true,
"LOG_LEVEL": "INFO",
"API_KEY": "your-api-key-here",
"PARALLEL_WORKERS": 4
}
Element Locators
Store element locators in JSON files in elements/ directory:
elements/facebook_login_page.json
{
"email": {
"type": "css",
"value": "#email"
},
"password": {
"type": "id",
"value": "pass"
},
"loginButton": {
"type": "xpath",
"value": "//button[@name='login']"
}
}
Supported Locator Types
css- CSS Selectorxpath- XPathid- Element IDname- Name attributeclass- Class nametag- Tag namelink- Link textpartial_link- Partial link text
๐ Writing Tests
UI Test Example
features/facebook_login_signup.feature
@ui
Feature: Facebook Login Functionality
As a Facebook user
I want to log in to my account
So that I can access my feed and interact with friends
@smoke
Scenario: Successful Login with Valid Credentials
Given I am on the Facebook login page
When I enter valid username and password
And I click the login button
Then I should be logged in successfully
@regression
Scenario: Unsuccessful Login with Invalid Password
Given I am on the Facebook login page
When I enter valid username and invalid password
And I click the login button
Then I should see an error message indicating invalid credentials
API Test Example
features/Todos_API_Testing.feature
@api
Feature: Todos API Testing
As a QA engineer
I want to test the Todos REST API thoroughly
So that I can ensure its functionality and data integrity
@smoke @todos
Scenario: Get all todos from API
Given the API client is configured for the Todos API
When I send GET request to "api/v1/todos"
Then the response status code should be 200
And the response should contain todos data structure
@regression
Scenario: Create a new todo via API
Given I have valid todo data
When I send POST request to "api/v1/todos" with the todo data
Then the response status code should be 201
And the response should contain the created todo details
Step Definitions
UI Steps (features/steps/facebook_login_signup_steps.py)
from behave import *
from features.pages.facebook_login_signup_page import FacebookLoginSignupPage
from utils.page_factory import get_page_object
@given("I am on the Facebook login page")
def step_impl(context):
login_page = get_page_object(context, FacebookLoginSignupPage)
login_page.navigate_to_facebook()
login_page.verify_page_title("Facebook โ log in or sign up")
API Steps (features/steps/Todos_API_Testing_Steps.py)
from behave import given, when, then
@given('the API client is configured for the Todos API')
def step_impl(context):
assert context.api_client is not None, "API client was not initialized"
@when('I send GET request to "{endpoint}"')
def step_impl(context, endpoint):
context.response = context.api_client.get(endpoint)
๐ Reporting
Allure Reports
# Generate and view Allure report
python run_tests.py --allure
allure serve reports/allure-results
# Generate static Allure report
allure generate reports/allure-results -o reports/allure-report --clean
# Open generated report
allure open reports/allure-report
JUnit Reports
JUnit reports are automatically generated in reports/junit/ directory when using --junit flag.
Custom HTML Reports
Custom HTML reports with charts and metrics are generated in reports/ directory.
HTML Report Dashboard

Sample HTML report showing test summary, charts, and detailed results
๐ Advanced Features
Parallel Execution Schemes
# Scenario-level parallelization (default)
python run_tests.py --parallel 4 --parallel-scheme scenario
# Feature-level parallelization
python run_tests.py --parallel 2 --parallel-scheme feature
Test Retry Configuration
Configure automatic retry in behave.ini:
[behavex]
retry=2
retry_delay=1
Environment Variables
# Set environment variables
export TEST_ENV=stg
export BROWSER=firefox
export HEADLESS=true
# Run tests with environment variables
python run_tests.py
Custom Test Groups
# Run specific test groups
python run_tests.py --tags "@group1,@group2"
# Exclude specific groups
python run_tests.py --tags "~@exclude_group"
๐ Troubleshooting
Common Issues
Browser not starting?
- Check browser drivers are installed
- Verify browser version compatibility
- Ensure webdriver-manager can download drivers
API tests failing?
- Check network connectivity
- Verify API endpoints and authentication
- Check API response formats
Parallel execution issues?
- Ensure tests are independent
- Check system resources (CPU, memory)
- Verify no shared state between tests
Element not found?
- Check element locators in JSON files
- Verify page loading timeouts
- Check for iframes or dynamic content
Debug Mode
# Run with debug logging
python run_tests.py --verbose --no-capture
# Run single scenario for debugging
python run_tests.py --tags "@debug" --no-capture
Logs and Artifacts
- Logs:
logs/directory - Screenshots:
reports/screenshots/directory (on failure) - Allure results:
reports/allure-results/ - JUnit reports:
reports/junit/
๐ค Best Practices
1. Test Organization
- Use consistent tagging strategy
- Group related scenarios in features
- Separate UI and API tests with tags
2. Page Objects
- Keep locators in JSON files
- Implement reusable page methods
- Use page factory for lazy initialization
3. Configuration Management
- Use environment-specific configs
- Store sensitive data in environment variables
- Version control configuration templates
4. Reporting
- Attach screenshots on failures
- Include API request/response details
- Use descriptive scenario names
5. Parallel Execution
- Design tests to be independent
- Avoid shared state between tests
- Use appropriate parallel scheme
๐ Performance Tips
- Use headless mode for CI/CD pipelines
- Implement smart waits instead of fixed sleeps
- Use parallel execution for large test suites
- Optimize test data setup and teardown
- Use API calls for test data preparation when possible
๐ CI/CD Integration
GitHub Actions Example
name: Test Execution
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: python run_tests.py --type all --headless --parallel 4 --allure
- name: Generate Allure report
run: allure generate reports/allure-results -o reports/allure-report --clean
- name: Upload Allure report
uses: actions/upload-artifact@v3
with:
name: allure-report
path: reports/allure-report
๐ License
This framework is provided for educational and testing purposes. Please ensure compliance with the terms of service for any applications under test.