Test Overview
This test verifies locating web elements using XPath axes: parent, following-sibling, and preceding-sibling. It checks that the correct elements are found and their text matches expected values.
This test verifies locating web elements using XPath axes: parent, following-sibling, and preceding-sibling. It checks that the correct elements are found and their text matches expected values.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class XPathAxesTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/xpath-axes-demo"); } @Test public void testXPathAxes() { // Locate child element WebElement child = driver.findElement(By.id("child1")); // Use parent axis to get parent element WebElement parent = child.findElement(By.xpath("parent::*")); assertEquals("parent-div", parent.getAttribute("id")); // Use following-sibling axis to get next sibling WebElement nextSibling = parent.findElement(By.xpath("following-sibling::*[1]")); assertEquals("sibling-div", nextSibling.getAttribute("id")); // Use preceding-sibling axis to get previous sibling of nextSibling WebElement preceding = nextSibling.findElement(By.xpath("preceding-sibling::*[1]")); assertEquals("parent-div", preceding.getAttribute("id")); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window opens at https://example.com/xpath-axes-demo | - | PASS |
| 2 | Find element with id 'child1' | Page loaded with element <div id='child1'> | Element with id 'child1' is found | PASS |
| 3 | Find parent element of 'child1' using XPath 'parent::*' | Parent element with id 'parent-div' is located | Parent element's id attribute equals 'parent-div' | PASS |
| 4 | Find following sibling of parent element using XPath 'following-sibling::*[1]' | Following sibling element with id 'sibling-div' is located | Following sibling's id attribute equals 'sibling-div' | PASS |
| 5 | Find preceding element of following sibling using XPath 'preceding-sibling::*[1]' | Preceding element with id 'parent-div' is located | Preceding element's id attribute equals 'parent-div' | PASS |
| 6 | Test ends and browser closes | Browser window closes | - | PASS |