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:

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

CommandDescription
python run_tests.pyRun all tests (UI + API)
python run_tests.py --type uiRun UI tests only
python run_tests.py --type apiRun API tests only
python run_tests.py --type smokeRun smoke tests
python run_tests.py --type regressionRun regression tests

Environment Configuration

CommandDescription
python run_tests.py --env devRun in development environment
python run_tests.py --env stgRun in staging environment
python run_tests.py --env prodRun in production environment

Browser Configuration

CommandDescription
python run_tests.py --browser chromeUse Chrome browser (default)
python run_tests.py --browser firefoxUse Firefox browser
python run_tests.py --browser edgeUse Edge browser
python run_tests.py --headlessRun browser in headless mode
python run_tests.py --headedRun browser in headed mode

Parallel Execution

CommandDescription
python run_tests.py --parallel 4Run with 4 parallel processes
python run_tests.py --processes 8Run with 8 parallel processes
python run_tests.py --type ui --parallel 4UI tests with 4 parallel processes
python run_tests.py --type api --parallel 6API tests with 6 parallel processes

Test Filtering with Tags

CommandDescription
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

CommandDescription
python run_tests.py --allureGenerate Allure report
python run_tests.py --junitGenerate JUnit reports
python run_tests.py --allure --junitGenerate both reports
python run_tests.py --verboseVerbose output

Advanced Execution Options

CommandDescription
python run_tests.py --dry-runDry run without executing tests
python run_tests.py --stopStop after first failure
python run_tests.py --no-captureDon't capture stdout/stderr
python run_tests.py --tags-helpShow tags help

Combined Examples

CommandDescription
python run_tests.py --type api --env stg --parallel 4 --allureAPI 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 --stopRegression 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 Selector
  • xpath - XPath
  • id - Element ID
  • name - Name attribute
  • class - Class name
  • tag - Tag name
  • link - Link text
  • partial_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

HTML Report Example

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.