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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/samplepage | Sample page loads with a button labeled 'Click me' and a hidden message area | - | PASS |
| 3 | Calls page.click_button() which waits for button to be clickable and clicks it | Button is clicked, triggering message display | - | PASS |
| 4 | Calls page.get_message_text() which waits for message to be visible and retrieves text | Message area shows text 'Button clicked!' | Verify message text equals 'Button clicked!' | PASS |
| 5 | Test assertion checks if retrieved text matches expected | Text matches expected | assertEqual(text, 'Button clicked!') | PASS |
| 6 | Browser closes and test ends | Browser window closed | - | PASS |