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.