0
0
Selenium Pythontesting~10 mins

Base page class in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page using a base page class, finds a button by its ID, clicks it, and verifies the page title changes as expected.

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 BasePage:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, locator):
        return WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(locator))

    def click(self, locator):
        element = self.find_element(locator)
        element.click()

class TestHomePage(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.base_page = BasePage(self.driver)

    def test_click_button_changes_title(self):
        self.driver.get("https://example.com")
        button_locator = (By.ID, "start-button")
        self.base_page.click(button_locator)
        WebDriverWait(self.driver, 10).until(EC.title_is("Started Page"))
        self.assertEqual(self.driver.title, "Started Page")

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and WebDriver Chrome instance is createdBrowser window opens, ready to navigate-PASS
2Browser navigates to https://example.comPage loads with a button having ID 'start-button'-PASS
3BasePage finds element with ID 'start-button' using WebDriverWaitButton element is present and visibleElement presence verified by WebDriverWaitPASS
4BasePage clicks the 'start-button'Button is clicked, page starts navigation or changes-PASS
5Wait until page title changes to 'Started Page'Page title updates to 'Started Page'Title is exactly 'Started Page'PASS
6Assert page title equals 'Started Page'Page title is 'Started Page'assertEqual passes confirming titlePASS
7Test ends and browser closesBrowser window closes-PASS
Failure Scenario
Failing Condition: Button with ID 'start-button' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the BasePage class method 'find_element' do?
AWaits up to 10 seconds for an element to be present and returns it
BClicks on an element immediately without waiting
CNavigates the browser to a URL
DCloses the browser window
Key Result
Using a base page class with reusable methods like 'find_element' and 'click' helps keep test code clean and easy to maintain.