Integrating Selenium Grid to Python Framework

01.06.23 09:23 AM - By topgrep21

Please note the code snippet, for an example of how to integrate Selenium Grid into your Python framework using the selenium package

PS: Refer the article on Python framework to get the codesnippet for the framework

Update WebDriver initialization:

  • Modify your webdriver_factory.py module to include the following code

from selenium import webdriver

def create_remote_driver(browser_name, platform):

  capabilities = {

  "browserName": browser_name,

  "platform": platform

  }

  return webdriver.Remote(

  command_executor='http://localhost:4444/wd/hub',

  desired_capabilities="capabilities

  )

Modify test configuration:

  • Update your test configuration in the config.py module to include the desired browser and platform information for the Selenium Grid node

GRID_BROWSER = 'chrome'  # The desired browser (e.g., 'chrome', 'firefox')
GRID_PLATFORM = 'PLATFORM'  # The desired platform (e.g., 'WINDOWS', 'LINUX')

Update test scripts:

  • Update your test scripts in the tests/ directory to use the remote driver created by the webdriver_factory.py module:

from utils.webdriver_factory import create_remote_driver
from utils.config import GRID_BROWSER, GRID_PLATFORM

def test_example():
  driver = create_remote_driver(GRID_BROWSER, GRID_PLATFORM)
  # Rest of your test code using the remote driver
  driver.quit()

With these changes, your framework will utilize the Selenium Grid infrastructure for distributed and parallel testing across multiple browsers and platforms.

Start the Selenium Grid hub:

  • Open a terminal or command prompt and run the following command:

selenium-server-standalone -role hub

  • This command starts the Selenium Grid hub.

    Start Selenium Grid nodes:

    • Open separate terminals or command prompts for each desired browser and platform combination.
    • In each terminal, run the following command for each node:
    • selenium-server-standalone -role node -hub http://localhost:4444/grid/register

topgrep21