0
0
Selenium Pythontesting~10 mins

Action methods in page class in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, uses an action method from a page class to click a button, and verifies the button click changes the page text 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 SamplePage:
    def __init__(self, driver):
        self.driver = driver
        self.button_locator = (By.ID, "click-me")
        self.message_locator = (By.ID, "message")

    def click_button(self):
        button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.button_locator)
        )
        button.click()

    def get_message_text(self):
        message = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.message_locator)
        )
        return message.text

class TestActionMethods(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://example.com/samplepage")
        self.page = SamplePage(self.driver)

    def test_click_button_changes_message(self):
        self.page.click_button()
        text = self.page.get_message_text()
        self.assertEqual(text, "Button clicked!")

    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 and ready-PASS
2Navigates to https://example.com/samplepageSample page loads with a button labeled 'Click me' and a hidden message area-PASS
3Calls page.click_button() which waits for button to be clickable and clicks itButton is clicked, triggering message display-PASS
4Calls page.get_message_text() which waits for message to be visible and retrieves textMessage area shows text 'Button clicked!'Verify message text equals 'Button clicked!'PASS
5Test assertion checks if retrieved text matches expectedText matches expectedassertEqual(text, 'Button clicked!')PASS
6Browser closes and test endsBrowser window closed-PASS
Failure Scenario
Failing Condition: Button with ID 'click-me' is not found or not clickable within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the method click_button() do in the page class?
AOpens the browser and navigates to the page
BRetrieves the text from the message area
CWaits for the button to be clickable and clicks it
DCloses the browser after the test
Key Result
Using action methods in a page class helps keep test code clean and reusable by separating page interactions from test logic.