Test Overview
This test automates a user navigating through multiple pages on a website. It verifies that each page loads correctly by checking the page title after navigation.
This test automates a user navigating through multiple pages on a website. It verifies that each page loads correctly by checking the page title after navigation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; 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; import java.time.Duration; public class MultiPageNavigationTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testMultiPageNavigation() { // Step 1: Open homepage driver.get("https://example.com"); wait.until(ExpectedConditions.titleIs("Example Domain")); assertEquals("Example Domain", driver.getTitle()); // Step 2: Click on More Information link WebElement moreInfoLink = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='https://www.iana.org/domains/example']"))); moreInfoLink.click(); // Step 3: Wait for new page and verify title wait.until(ExpectedConditions.titleContains("IANA")); assertEquals(true, driver.getTitle().contains("IANA")); // Step 4: Navigate back to homepage driver.navigate().back(); wait.until(ExpectedConditions.titleIs("Example Domain")); assertEquals("Example Domain", driver.getTitle()); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open with no page loaded | - | PASS |
| 2 | Navigates to https://example.com | Homepage loads with title 'Example Domain' | Check page title equals 'Example Domain' | PASS |
| 3 | Finds 'More Information' link by CSS selector and clicks it | Browser navigates to https://www.iana.org/domains/example | Wait until page title contains 'IANA' | PASS |
| 4 | Verifies page title contains 'IANA' | Page loaded with title containing 'IANA' | Assert page title contains 'IANA' | PASS |
| 5 | Navigates back to previous page | Browser returns to https://example.com homepage | Wait until page title is 'Example Domain' | PASS |
| 6 | Verifies page title is 'Example Domain' again | Homepage displayed with correct title | Assert page title equals 'Example Domain' | PASS |
| 7 | Test ends and browser closes | Browser window closed | - | PASS |