Test Overview
This test opens a webpage, finds a button using XPath with the text() function, clicks it, and verifies the expected message appears.
This test opens a webpage, finds a button using XPath with the text() function, clicks it, and verifies the expected message appears.
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 TestXPathText(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/buttonpage') def test_click_button_by_text(self): driver = self.driver # Wait until the button with exact text 'Click Me' is present button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//button[text()='Click Me']")) ) button.click() # Wait for the message element to appear message = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'message')) ) self.assertEqual(message.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 navigated to 'https://example.com/buttonpage' | - | PASS |
| 2 | Wait until button with text 'Click Me' is present using XPath //button[text()='Click Me'] | Button with text 'Click Me' is visible on the page | Element located by XPath with exact text 'Click Me' | PASS |
| 3 | Click the button found by XPath | Button is clicked, page reacts to click | - | PASS |
| 4 | Wait until element with ID 'message' appears | Message element is visible on the page | Element with ID 'message' is present | PASS |
| 5 | Check that message text equals 'Button clicked!' | Message text is 'Button clicked!' | Assert message.text == 'Button clicked!' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | - | PASS |