Test Overview
This test uses the Page Factory pattern to locate and interact with a login page's username, password fields, and login button. It verifies that after login, the page shows a welcome message.
This test uses the Page Factory pattern to locate and interact with a login page's username, password fields, and login button. It verifies that after login, the page shows a welcome message.
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 from selenium.webdriver.support.page_factory import PageFactory import unittest class LoginPage: username = (By.ID, "username") password = (By.ID, "password") login_button = (By.ID, "loginBtn") def __init__(self, driver): self.driver = driver PageFactory.init_elements(self.driver, self) def login(self, user, pwd): self.username.clear() self.username.send_keys(user) self.password.clear() self.password.send_keys(pwd) 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.login("testuser", "testpass") # Wait for welcome message welcome = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.ID, "welcomeMsg")) ) self.assertEqual(welcome.text, "Welcome, testuser!") 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 | - | PASS |
| 2 | LoginPage object initializes and locates username, password, and login button elements using PageFactory | Username, password fields and login button are found on the page | - | PASS |
| 3 | Test inputs 'testuser' in username field and 'testpass' in password field, then clicks login button | Login form is submitted | - | PASS |
| 4 | Waits up to 10 seconds for welcome message element with ID 'welcomeMsg' to appear | Welcome message element appears with text 'Welcome, testuser!' | Check that welcome message text equals 'Welcome, testuser!' | PASS |
| 5 | Test ends and browser closes | Browser window is closed | - | PASS |