Test Overview
This test opens a webpage and uses XPath with contains() and starts-with() functions to find elements. It verifies that the correct elements are found and their text matches expected values.
This test opens a webpage and uses XPath with contains() and starts-with() functions to find elements. It verifies that the correct elements are found and their text matches expected values.
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 TestXPathFunctions(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/testpage') def test_xpath_contains_and_startswith(self): driver = self.driver wait = WebDriverWait(driver, 10) # Find element where attribute contains 'submit' submit_button = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(@id, 'submit')]"))) self.assertEqual(submit_button.text, 'Submit') # Find element where attribute starts-with 'user' username_input = wait.until(EC.presence_of_element_located((By.XPATH, "//input[starts-with(@name, 'user')]"))) self.assertEqual(username_input.get_attribute('placeholder'), 'Enter username') 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 | Browser navigates to 'https://example.com/testpage' | Page loads with test elements visible | - | PASS |
| 3 | Wait until button with id containing 'submit' is present using XPath contains() | Button element with id like 'btn-submit' is found | Check button text equals 'Submit' | PASS |
| 4 | Wait until input with name starting with 'user' is present using XPath starts-with() | Input element with name like 'username' is found | Check input placeholder attribute equals 'Enter username' | PASS |
| 5 | Browser closes and test ends | Browser window closed | - | PASS |