0
0
Selenium Pythontesting~10 mins

Why Grid enables parallel execution in Selenium Python - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test demonstrates how Selenium Grid allows running multiple tests at the same time on different browsers or machines. It verifies that tests can start and finish independently without waiting for each other.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
import threading
import time

def run_test(node_url: str, browser_name: str):
    options = webdriver.ChromeOptions() if browser_name == 'chrome' else webdriver.FirefoxOptions()
    driver: WebDriver = webdriver.Remote(
        command_executor=node_url,
        options=options
    )
    driver.get('https://example.com')
    heading = driver.find_element(By.TAG_NAME, 'h1').text
    assert heading == 'Example Domain', f'Heading mismatch on {browser_name}'
    time.sleep(2)  # Simulate test steps
    driver.quit()

if __name__ == '__main__':
    node1 = 'http://localhost:4444/wd/hub'
    node2 = 'http://localhost:4445/wd/hub'
    thread1 = threading.Thread(target=run_test, args=(node1, 'chrome'))
    thread2 = threading.Thread(target=run_test, args=(node2, 'firefox'))
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and two threads are created for parallel executionNo browsers open yet, threads ready to run tests on different nodes-PASS
2Thread 1 opens Chrome browser via Selenium Grid node at http://localhost:4444/wd/hubChrome browser window opens on node1, navigates to https://example.com-PASS
3Thread 2 opens Firefox browser via Selenium Grid node at http://localhost:4445/wd/hubFirefox browser window opens on node2, navigates to https://example.com-PASS
4Thread 1 finds the heading element and asserts its text is 'Example Domain'Chrome browser shows page with heading 'Example Domain'Heading text equals 'Example Domain'PASS
5Thread 2 finds the heading element and asserts its text is 'Example Domain'Firefox browser shows page with heading 'Example Domain'Heading text equals 'Example Domain'PASS
6Both threads wait 2 seconds simulating test steps independentlyBoth browsers remain open and active on their respective nodes-PASS
7Both threads close their browsers and endNo browsers open, threads complete execution-PASS
Failure Scenario
Failing Condition: One node URL is incorrect or node is down, causing browser not to open
Execution Trace Quiz - 3 Questions
Test your understanding
What allows the two tests to run at the same time in this example?
AUsing Selenium Grid with multiple nodes
BRunning tests sequentially in one thread
CUsing only one browser instance
DRunning tests on the same node one after another
Key Result
Using Selenium Grid with multiple nodes enables running tests in parallel on different browsers or machines, which saves time and improves test efficiency.