Skip to content

A comprehensive Behavior-Driven Development (BDD) test automation framework using Selenium WebDriver for UI testing and Python Requests for API testing

Notifications You must be signed in to change notification settings

DipankarDandapat/Python_Selenium_BDD_Framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Enhanced Selenium BDD Framework with BehaveX

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

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 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.

About

A comprehensive Behavior-Driven Development (BDD) test automation framework using Selenium WebDriver for UI testing and Python Requests for API testing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published