0
0
Selenium Javatesting~5 mins

Multi-page navigation flow in Selenium Java

Choose your learning style9 modes available
Introduction

Multi-page navigation flow helps test if a user can move through different pages on a website smoothly.

When testing an online shopping site from homepage to product page to checkout.
When verifying a user can log in, go to their profile, and then log out.
When checking a multi-step form that spans several pages.
When ensuring links between pages work correctly.
When testing navigation menus that lead to different sections.
Syntax
Selenium Java
driver.get("https://example.com/page1");
driver.findElement(By.id("nextPageButton")).click();
String currentUrl = driver.getCurrentUrl();

Use driver.get() to open a page.

Use findElement and click() to move to the next page.

Examples
Navigate from home page to About Us page by clicking a link.
Selenium Java
driver.get("https://example.com/home");
driver.findElement(By.linkText("About Us")).click();
Navigate to login page and submit login form to go to user dashboard.
Selenium Java
driver.get("https://example.com/login");
driver.findElement(By.name("username")).sendKeys("user1");
driver.findElement(By.name("password")).sendKeys("pass123");
driver.findElement(By.cssSelector("button[type='submit']")).click();
Sample Program

This test opens the homepage, clicks to go to the products page, then clicks a product to see details. It checks URLs and page titles to confirm navigation worked.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MultiPageNavigationTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            // Open homepage
            driver.get("https://example.com/home");
            // Click link to go to products page
            driver.findElement(By.linkText("Products")).click();
            // Verify URL contains 'products'
            String url = driver.getCurrentUrl();
            if (!url.contains("products")) {
                System.out.println("Test Failed: Not on products page");
                return;
            }
            // Click first product to go to product details
            driver.findElement(By.cssSelector(".product-item:first-child a")).click();
            // Verify product details page loaded
            String title = driver.getTitle();
            if (!title.contains("Product Details")) {
                System.out.println("Test Failed: Product details page not loaded");
                return;
            }
            System.out.println("Test Passed: Multi-page navigation flow works");
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always wait for page elements to load before interacting to avoid errors.

Use clear locators like By.id or By.cssSelector for stable tests.

Close the browser with driver.quit() to free resources.

Summary

Multi-page navigation tests check if users can move through pages correctly.

Use Selenium commands to open pages and click links or buttons.

Verify navigation by checking URLs or page titles.