Bird
Raised Fist0
Selenium Pythontesting~8 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main role of the hub in Selenium Grid architecture?
easy
A. To run the browser instances directly
B. To store test results locally
C. To receive test requests and distribute them to the appropriate nodes
D. To act as a database for test data

Solution

  1. Step 1: Understand the hub's function in Selenium Grid

    The hub acts as a central point that receives test requests from clients.
  2. Step 2: Identify how the hub manages nodes

    The hub distributes these requests to the right nodes that have the required browser and platform.
  3. Final Answer:

    To receive test requests and distribute them to the appropriate nodes -> Option C
  4. Quick Check:

    Hub controls nodes [OK]
Hint: Hub directs tests to nodes, it doesn't run browsers itself [OK]
Common Mistakes:
  • Thinking the hub runs browsers directly
  • Confusing hub with node responsibilities
  • Assuming hub stores test data
2. Which of the following is the correct command to start a Selenium Grid hub using the Selenium Server jar file?
easy
A. java -jar selenium-server.jar -role hub
B. java -jar selenium-server.jar -role node
C. java -jar selenium-server.jar -start hub
D. java -jar selenium-server.jar -node start

Solution

  1. Step 1: Recall the command syntax for starting a hub

    The correct command uses '-role hub' to specify the hub role.
  2. Step 2: Compare options with correct syntax

    java -jar selenium-server.jar -role hub matches the correct syntax: 'java -jar selenium-server.jar -role hub'.
  3. Final Answer:

    java -jar selenium-server.jar -role hub -> Option A
  4. Quick Check:

    Hub starts with '-role hub' flag [OK]
Hint: Hub uses '-role hub' flag in command [OK]
Common Mistakes:
  • Using '-role node' instead of '-role hub'
  • Incorrect flag order or spelling
  • Using '-start' or '-node start' which are invalid
3. Given the following Selenium Grid setup code snippet, what will be the output when the test runs?
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

hub_url = "http://localhost:4444/wd/hub"
driver = webdriver.Remote(command_executor=hub_url, desired_capabilities=DesiredCapabilities.CHROME)
print(driver.capabilities['browserName'])
driver.quit()
medium
A. safari
B. firefox
C. edge
D. chrome

Solution

  1. Step 1: Analyze the desired capabilities used

    The code requests a Chrome browser by using DesiredCapabilities.CHROME.
  2. Step 2: Understand the output of driver.capabilities['browserName']

    This returns the name of the browser the remote driver is connected to, which should be 'chrome'.
  3. Final Answer:

    chrome -> Option D
  4. Quick Check:

    DesiredCapabilities.CHROME = 'chrome' output [OK]
Hint: DesiredCapabilities.CHROME means browserName is 'chrome' [OK]
Common Mistakes:
  • Assuming default browser is Firefox
  • Confusing capability keys
  • Not quitting driver causing errors
4. You started a Selenium Grid node with the command:
java -jar selenium-server.jar -role node -hub http://localhost:4444/grid/register
But the node is not registering with the hub. What is the most likely cause?
medium
A. The hub URL is incorrect or the hub is not running
B. The node command should use '-role hub' instead of '-role node'
C. The selenium-server.jar file is corrupted
D. The node must be started before the hub

Solution

  1. Step 1: Check the hub URL and hub status

    If the node cannot register, often the hub URL is wrong or the hub is not running.
  2. Step 2: Verify command correctness

    The command uses '-role node' correctly; the jar file corruption or start order is less common causes.
  3. Final Answer:

    The hub URL is incorrect or the hub is not running -> Option A
  4. Quick Check:

    Node needs correct hub URL and running hub [OK]
Hint: Check hub URL and hub running status first [OK]
Common Mistakes:
  • Mixing up node and hub roles in commands
  • Assuming jar file corruption without evidence
  • Starting node before hub is usually okay
5. You want to run tests on multiple browsers simultaneously using Selenium Grid. Which setup correctly describes how the hub and nodes should be configured?
hard
A. Multiple nodes running on the same machine without a hub
B. One hub running on a central machine, multiple nodes registered with different browsers on different machines
C. One node running multiple hubs, each controlling a browser
D. Multiple hubs running on different machines, each with one node registered

Solution

  1. Step 1: Understand Selenium Grid architecture

    The hub acts as a central controller, and nodes register to it with different browsers.
  2. Step 2: Identify correct multi-browser setup

    Multiple nodes on different machines with different browsers register to one hub to run tests in parallel.
  3. Final Answer:

    One hub running on a central machine, multiple nodes registered with different browsers on different machines -> Option B
  4. Quick Check:

    Single hub, multiple nodes with browsers [OK]
Hint: Hub central, nodes distributed with browsers [OK]
Common Mistakes:
  • Thinking multiple hubs are needed for parallel tests
  • Confusing roles of hub and node
  • Running nodes without a hub
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard