0
0
Selenium Pythontesting~8 mins

Grid architecture (hub and node) in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Grid architecture (hub and node)
Folder Structure
selenium_grid_project/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   ├── selenium_grid_manager.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── browsers.yaml
├── reports/
│   └── test_report.html
├── conftest.py
└── pytest.ini
    
Test Framework Layers
  • Grid Manager Layer: Handles connection to Selenium Grid hub and manages nodes. (utils/selenium_grid_manager.py)
  • Page Object Layer: Contains page classes representing UI pages with locators and actions. (pages/)
  • Test Layer: Contains test cases using pytest that run tests via the grid. (tests/)
  • Utilities Layer: Helper functions and utilities for common tasks. (utils/helpers.py)
  • Configuration Layer: Holds environment, browser, and grid settings. (config/)
  • Reporting Layer: Stores test reports generated after test runs. (reports/)
Configuration Patterns

Use YAML files to store environment URLs, browser types, and Selenium Grid hub URL.

# config/config.yaml
environment: "https://example.com"
browser: "chrome"
grid_hub_url: "http://localhost:4444/wd/hub"
    

Use pytest fixtures in conftest.py to read config and create remote WebDriver sessions connected to the grid hub.

import pytest
import yaml
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

@pytest.fixture(scope='session')
def config():
    with open('config/config.yaml') as f:
        return yaml.safe_load(f)

@pytest.fixture
def driver(config):
    browser = config['browser']
    hub_url = config['grid_hub_url']

    if browser == 'chrome':
        capabilities = DesiredCapabilities.CHROME.copy()
    elif browser == 'firefox':
        capabilities = DesiredCapabilities.FIREFOX.copy()
    else:
        capabilities = DesiredCapabilities.CHROME.copy()

    driver = webdriver.Remote(command_executor=hub_url, desired_capabilities=capabilities)
    yield driver
    driver.quit()
    
Test Reporting and CI/CD Integration

Use pytest-html plugin to generate HTML reports saved in reports/ folder.

# pytest.ini
[pytest]
addopts = --html=reports/test_report.html --self-contained-html
    

Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on code push and publish reports.

Example GitHub Actions step to run tests:

- name: Run Selenium Grid Tests
  run: |
    pytest --html=reports/test_report.html --self-contained-html
  
Framework Design Principles
  • Use Page Object Model: Keep UI locators and actions separate from tests for easy maintenance.
  • Centralize Grid Connection: Manage Selenium Grid hub URL and capabilities in one place for flexibility.
  • Parameterize Browsers: Allow running tests on different browsers by changing config only.
  • Isolate Tests: Each test gets a fresh WebDriver session to avoid state leaks.
  • Generate Clear Reports: Use HTML reports to easily see test results and failures.
Self Check

Where would you add a new page object class for the "User Profile" page in this framework structure?

Key Result
Use Selenium Grid hub and nodes with a layered Python pytest framework to run tests remotely and in parallel.