0
0
Selenium Javatesting~15 mins

Navigation (back, forward, refresh) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify browser navigation using back, forward, and refresh buttons
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/page1'
Step 2: Click on the link with id 'nextPageLink' to go to 'https://example.com/page2'
Step 3: Use the browser back navigation to return to 'https://example.com/page1'
Step 4: Verify the current URL is 'https://example.com/page1'
Step 5: Use the browser forward navigation to go to 'https://example.com/page2'
Step 6: Verify the current URL is 'https://example.com/page2'
Step 7: Refresh the current page
Step 8: Verify the page title is still 'Page 2 Title'
✅ Expected Result: The browser navigates back and forward correctly, and the page refresh reloads the current page without changing the URL or title.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert current URL after back navigation is 'https://example.com/page1'
Assert current URL after forward navigation is 'https://example.com/page2'
Assert page title after refresh is 'Page 2 Title'
Best Practices:
Use explicit waits to ensure page loads before assertions
Use By.id locator for the link element
Use WebDriver's navigate() methods for back, forward, and refresh
Keep test steps clear and separated
Automated Solution
Selenium Java
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 java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class NavigationTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        try {
            // Step 1: Navigate to page1
            driver.get("https://example.com/page1");

            // Step 2: Click link to go to page2
            WebElement nextPageLink = wait.until(ExpectedConditions.elementToBeClickable(By.id("nextPageLink")));
            nextPageLink.click();

            // Wait for page2 to load
            wait.until(ExpectedConditions.urlToBe("https://example.com/page2"));

            // Step 3: Navigate back to page1
            driver.navigate().back();

            // Wait for page1 to load
            wait.until(ExpectedConditions.urlToBe("https://example.com/page1"));

            // Step 4: Assert URL is page1
            assertEquals("https://example.com/page1", driver.getCurrentUrl(), "URL after back navigation should be page1");

            // Step 5: Navigate forward to page2
            driver.navigate().forward();

            // Wait for page2 to load
            wait.until(ExpectedConditions.urlToBe("https://example.com/page2"));

            // Step 6: Assert URL is page2
            assertEquals("https://example.com/page2", driver.getCurrentUrl(), "URL after forward navigation should be page2");

            // Step 7: Refresh the page
            driver.navigate().refresh();

            // Wait for page2 title
            wait.until(ExpectedConditions.titleIs("Page 2 Title"));

            // Step 8: Assert page title after refresh
            assertEquals("Page 2 Title", driver.getTitle(), "Page title after refresh should be 'Page 2 Title'");

            System.out.println("Test Passed: Navigation back, forward, and refresh works correctly.");
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate browser navigation.

First, it opens https://example.com/page1. Then it clicks the link with id nextPageLink to go to page 2.

It waits explicitly for the URL to change to page 2 before continuing.

Next, it uses driver.navigate().back() to go back to page 1 and waits for the URL to confirm the navigation.

Assertions check the current URL after back and forward navigation to ensure correctness.

Then it navigates forward to page 2 again and refreshes the page.

After refresh, it waits for the page title to be correct and asserts it.

Explicit waits ensure the page is fully loaded before assertions, preventing flaky tests.

The test ends by closing the browser.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using hardcoded XPath for the link instead of stable locators
Not verifying the URL after navigation
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different pairs of URLs and expected titles for navigation and refresh verification.

Show Hint