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
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username_input = (By.ID, 'username')
self.password_input = (By.ID, 'password')
self.login_button = (By.ID, 'loginBtn')
def enter_username(self, username):
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.username_input)).send_keys(username)
def enter_password(self, password):
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).send_keys(password)
def click_login(self):
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_button)).click()
class DashboardPage:
def __init__(self, driver):
self.driver = driver
self.logout_button = (By.ID, 'logoutBtn')
def is_logout_visible(self):
return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.logout_button)) is not None
def test_login_functionality():
driver = webdriver.Chrome()
driver.get('https://example.com/login')
login_page = LoginPage(driver)
login_page.enter_username('testuser')
login_page.enter_password('Test@1234')
login_page.click_login()
# Verify URL contains '/dashboard'
WebDriverWait(driver, 10).until(EC.url_contains('/dashboard'))
assert '/dashboard' in driver.current_url, f"Expected '/dashboard' in URL but got {driver.current_url}"
dashboard_page = DashboardPage(driver)
assert dashboard_page.is_logout_visible(), "Logout button should be visible on dashboard"
driver.quit()
if __name__ == '__main__':
test_login_functionality()This script uses Selenium with Python to automate the regression test for login functionality.
We use the Page Object Model to keep locators and actions organized in LoginPage and DashboardPage classes.
Explicit waits ensure the script waits for elements to appear or be clickable, avoiding flaky tests.
The test opens the login page, enters credentials, clicks login, then verifies the URL contains '/dashboard' and the logout button is visible.
Finally, the browser is closed to clean up.