0
0
Selenium Pythontesting~15 mins

Running tests on Grid in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Run a Selenium test on Selenium Grid
Preconditions (3)
Step 1: Open a remote browser session on Selenium Grid using Chrome
Step 2: Navigate to http://example.com/login
Step 3: Enter 'testuser' in the username field with id 'username'
Step 4: Enter 'Password123' in the password field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait for the page to load and verify the URL contains '/dashboard'
✅ Expected Result: User is successfully logged in and the browser navigates to the dashboard page on the remote Grid node
Automation Requirements - selenium
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Best Practices:
Use Remote WebDriver to connect to Selenium Grid Hub
Use explicit waits to wait for page load or elements
Use By.ID locators for elements
Close the browser session after test
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def test_login_on_grid():
    grid_url = "http://localhost:4444/wd/hub"
    capabilities = {
        "browserName": "chrome"
    }

    driver = webdriver.Remote(command_executor=grid_url, desired_capabilities=capabilities)

    try:
        driver.get("http://example.com/login")

        # Enter username
        username_field = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "username"))
        )
        username_field.send_keys("testuser")

        # Enter password
        password_field = driver.find_element(By.ID, "password")
        password_field.send_keys("Password123")

        # Click login button
        login_button = driver.find_element(By.ID, "loginBtn")
        login_button.click()

        # Wait for URL to contain '/dashboard'
        WebDriverWait(driver, 10).until(
            EC.url_contains("/dashboard")
        )

        # Assertion
        assert "/dashboard" in driver.current_url, "Login failed or dashboard not loaded"

    finally:
        driver.quit()

This test uses Selenium's Remote WebDriver to connect to the Selenium Grid Hub at http://localhost:4444/wd/hub. It specifies Chrome as the browser.

The test navigates to the login page, waits explicitly for the username field to appear, then fills in the username and password fields using By.ID locators.

After clicking the login button, it waits explicitly until the URL contains /dashboard, indicating successful login.

The assertion checks the URL to confirm the dashboard page loaded.

Finally, the browser session is closed with driver.quit() to clean up resources.

Common Mistakes - 4 Pitfalls
Using local WebDriver instead of Remote WebDriver for Grid
Using implicit waits or no waits before interacting with elements
Hardcoding XPath locators instead of using stable locators like ID
Not closing the browser session after test
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations to run the login test multiple times on the Grid.

Show Hint