Test Overview
This test opens a website, navigates to another page, then uses browser navigation commands: back, forward, and refresh. It verifies the page titles after each navigation step to confirm correct behavior.
This test opens a website, navigates to another page, then uses browser navigation commands: back, forward, and refresh. It verifies the page titles after each navigation step to confirm correct behavior.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; public class NavigationTest { static WebDriver driver; @BeforeAll public static void setup() { driver = new ChromeDriver(); } @Test public void testNavigationBackForwardRefresh() { driver.get("https://example.com"); String firstPageTitle = driver.getTitle(); driver.navigate().to("https://example.com/more-info"); String secondPageTitle = driver.getTitle(); driver.navigate().back(); String backPageTitle = driver.getTitle(); assertEquals(firstPageTitle, backPageTitle, "Back navigation should return to first page"); driver.navigate().forward(); String forwardPageTitle = driver.getTitle(); assertEquals(secondPageTitle, forwardPageTitle, "Forward navigation should go to second page"); driver.navigate().refresh(); String refreshedTitle = driver.getTitle(); assertEquals(secondPageTitle, refreshedTitle, "Refresh should keep the same page title"); } @AfterAll public static void teardown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, no page loaded yet | — | PASS |
| 2 | Navigate to https://example.com | Browser displays the example.com homepage | Page title is captured as firstPageTitle | PASS |
| 3 | Navigate to https://example.com/more-info | Browser displays the more-info page | Page title is captured as secondPageTitle | PASS |
| 4 | Navigate back to previous page | Browser displays the example.com homepage again | Assert page title equals firstPageTitle | PASS |
| 5 | Navigate forward to more-info page | Browser displays the more-info page again | Assert page title equals secondPageTitle | PASS |
| 6 | Refresh the current page | Browser reloads the more-info page | Assert page title equals secondPageTitle | PASS |
| 7 | Test ends and browser closes | Browser window is closed | — | PASS |
driver.navigate().back() method do in Selenium WebDriver?back() method simulates clicking the browser's back button, moving to the previous page in history.forward() moves forward, refresh() reloads the page, and closing the browser is done by driver.close().driver.navigate().refresh();.driver.refresh(), driver.navigate().reload(), and driver.refreshPage() are not valid Selenium WebDriver methods.driver.get("https://example.com/page1");
driver.get("https://example.com/page2");
driver.navigate().back();
String currentUrl = driver.getCurrentUrl();currentUrl after execution?navigate().back() moves back to page1.getCurrentUrl() returns the URL of page1.driver.get("https://example.com");
driver.navigate().forward();
String url = driver.getCurrentUrl();forward() method moves forward in browser history only if there is a forward page available.back() was called before, there is no forward page, so forward() does nothing.back() then forward(), then gets the current URL.Pass the test and this pattern moves unknown