Test Overview
This test opens a login page, enters username and password using Page Object Model (POM), clicks login, and verifies successful login message. It shows how POM organizes test code by separating page actions from test logic.
This test opens a login page, enters username and password using Page Object Model (POM), clicks login, and verifies successful login message. It shows how POM organizes test code by separating page actions from test logic.
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.username_input = (By.ID, "username") self.password_input = (By.ID, "password") self.login_button = (By.ID, "loginBtn") def enter_username(self, username): self.driver.find_element(*self.username_input).send_keys(username) def enter_password(self, password): self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): self.driver.find_element(*self.login_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) def test_valid_login(self): self.login_page.enter_username("user1") self.login_page.enter_password("pass123") self.login_page.click_login() WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.ID, "welcomeMsg")) ) welcome_text = self.driver.find_element(By.ID, "welcomeMsg").text self.assertEqual(welcome_text, "Welcome user1!") def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open at https://example.com/login page | - | PASS |
| 2 | Test calls enter_username method to find username input and types 'user1' | Username input field contains text 'user1' | - | PASS |
| 3 | Test calls enter_password method to find password input and types 'pass123' | Password input field contains text 'pass123' | - | PASS |
| 4 | Test calls click_login method to find and click login button | Login button clicked, page starts loading next screen | - | PASS |
| 5 | Test waits up to 10 seconds for element with ID 'welcomeMsg' to appear | Welcome message element is present on page | Verify welcome message text equals 'Welcome user1!' | PASS |
| 6 | Test asserts that welcome message text is exactly 'Welcome user1!' | Welcome message text is 'Welcome user1!' | assertEqual passes | PASS |
| 7 | Test ends and browser closes | Browser window closed | - | PASS |