Test Overview
This test opens a webpage and locates a button using both absolute and relative XPath. It verifies that the button is clickable and has the expected text.
This test opens a webpage and locates a button using both absolute and relative XPath. It verifies that the button is clickable and has the expected text.
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 TestXPath(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/testpage') def test_button_xpath(self): driver = self.driver wait = WebDriverWait(driver, 10) # Locate button using absolute XPath abs_xpath = '/html/body/div/main/section/button' button_abs = wait.until(EC.element_to_be_clickable((By.XPATH, abs_xpath))) self.assertEqual(button_abs.text, 'Click Me') # Locate button using relative XPath rel_xpath = '//section/button' button_rel = wait.until(EC.element_to_be_clickable((By.XPATH, rel_xpath))) self.assertEqual(button_rel.text, 'Click Me') 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, ready to load the test page | - | PASS |
| 2 | Browser navigates to 'https://example.com/testpage' | Page loads with a main section containing a button labeled 'Click Me' | - | PASS |
| 3 | Wait until the button is clickable using absolute XPath '/html/body/div/main/section/button' | Button element is found and clickable | Check that button text equals 'Click Me' | PASS |
| 4 | Wait until the button is clickable using relative XPath '//section/button' | Button element is found and clickable | Check that button text equals 'Click Me' | PASS |
| 5 | Test ends and browser closes | Browser window closes | - | PASS |