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
import unittest
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.email_input = (By.ID, 'email')
self.password_input = (By.ID, 'password')
self.login_button = (By.ID, 'loginBtn')
self.error_message = (By.ID, 'errorMsg')
def enter_email(self, email):
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).clear()
self.driver.find_element(*self.email_input).send_keys(email)
def enter_password(self, password):
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).clear()
self.driver.find_element(*self.password_input).send_keys(password)
def click_login(self):
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_button)).click()
def get_error_message(self):
return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.error_message)).text
class DashboardPage:
def __init__(self, driver):
self.driver = driver
self.logout_button = (By.ID, 'logoutBtn')
def click_logout(self):
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.logout_button)).click()
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get('https://example.com/login')
self.login_page = LoginPage(self.driver)
self.dashboard_page = DashboardPage(self.driver)
def tearDown(self):
self.driver.quit()
def test_login_valid_and_invalid(self):
# Valid login
self.login_page.enter_email('user@example.com')
self.login_page.enter_password('Password123!')
self.login_page.click_login()
WebDriverWait(self.driver, 10).until(EC.url_contains('/dashboard'))
self.assertIn('/dashboard', self.driver.current_url, 'Failed to navigate to dashboard after login')
# Logout
self.dashboard_page.click_logout()
WebDriverWait(self.driver, 10).until(EC.url_contains('/login'))
# Invalid email
self.login_page.enter_email('invalidemail')
self.login_page.enter_password('Password123!')
self.login_page.click_login()
error_text = self.login_page.get_error_message()
self.assertEqual(error_text, 'Please enter a valid email address', 'Incorrect error message for invalid email')
# Empty password
self.login_page.enter_email('user@example.com')
self.login_page.enter_password('')
self.login_page.click_login()
error_text = self.login_page.get_error_message()
self.assertEqual(error_text, 'Password cannot be empty', 'Incorrect error message for empty password')
if __name__ == '__main__':
unittest.main()The code uses Selenium WebDriver with Python's unittest framework to automate the login test case.
We define Page Object Model classes LoginPage and DashboardPage to keep locators and actions organized.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests.
The test method test_login_valid_and_invalid covers all manual test steps: valid login, logout, invalid email, and empty password cases.
Assertions check the URL after login and verify error messages for invalid inputs with clear messages.
Setup and teardown methods open and close the browser cleanly for each test run.