0
0
Selenium Pythontesting~10 mins

Page class structure in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if the login page loads correctly and the login button is clickable using a page class structure in Selenium with Python.

Test Code - unittest
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.url = "https://example.com/login"
        self.login_button_locator = (By.ID, "login-btn")

    def load(self):
        self.driver.get(self.url)

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

class TestLoginPage(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.login_page = LoginPage(self.driver)

    def test_login_button_clickable(self):
        self.login_page.load()
        self.login_page.click_login()
        # Verify URL changed or login action started
        self.assertIn("dashboard", self.driver.current_url)

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open but no page loaded yet-PASS
2Navigates to https://example.com/login using LoginPage.load()Login page is loaded in the browser-PASS
3Waits until login button is clickable and clicks it using LoginPage.click_login()Login button is clicked, browser navigates to dashboard pageCheck if current URL contains 'dashboard'PASS
4Assertion verifies URL contains 'dashboard'Browser URL is https://example.com/dashboardself.assertIn("dashboard", self.driver.current_url)PASS
5Test 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?
AThe login button is visible
BThe URL contains 'dashboard'
CThe page title is 'Login'
DThe browser is closed
Key Result
Using a page class structure helps organize locators and actions, making tests easier to read and maintain.