0
0
Selenium Pythontesting~10 mins

Element locators in page class in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, uses a page class to find a login button by its locator, clicks it, and verifies the next page shows a welcome message.

Test Code - Selenium
Selenium Python
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.login_button_locator = (By.ID, "login-btn")

    def click_login(self):
        login_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.login_button_locator)
        )
        login_button.click()

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://example.com/login")

    def test_click_login_button(self):
        page = LoginPage(self.driver)
        page.click_login()
        welcome_text = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.ID, "welcome-msg"))
        ).text
        self.assertEqual(welcome_text, "Welcome User")

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to load URL-PASS
2Browser navigates to https://example.com/loginLogin page is loaded with login button visible-PASS
3LoginPage object is created with driverPage class has locator for login button stored-PASS
4Waits until login button is clickable and clicks itLogin button is clicked, page starts loading next content-PASS
5Waits until welcome message element is visibleWelcome message element with text 'Welcome User' is visibleCheck that welcome message text equals 'Welcome User'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Login button is not found or not clickable within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button?
AThat the URL changes to the home page
BThat the welcome message with text 'Welcome User' appears
CThat the login button disappears
DThat the page title changes to 'Dashboard'
Key Result
Using a page class to store element locators keeps test code clean and easy to maintain by separating page details from test logic.