Test Overview
This test opens a web page and finds a link using a part of its visible text. It then clicks the link and verifies that the new page contains expected content.
This test opens a web page and finds a link using a part of its visible text. It then clicks the link and verifies that the new page contains expected content.
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.assertTrue; public class PartialLinkTextTest { private WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @Test public void testFindElementByPartialLinkText() { driver.get("https://example.com"); WebElement link = driver.findElement(By.partialLinkText("More information")); link.click(); String pageSource = driver.getPageSource(); assertTrue(pageSource.contains("Example Domain"), "Page should contain 'Example Domain'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Browser navigates to https://example.com | Page loads showing Example Domain homepage | - | PASS |
| 3 | Find element by partial link text 'More information' | Link with text containing 'More information' is located | Element is found and not null | PASS |
| 4 | Click the found link | Browser navigates to the linked page | - | PASS |
| 5 | Get page source and check it contains 'Example Domain' | Page source is retrieved | Assert page source contains 'Example Domain' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | - | PASS |