Multi-page navigation flow in Selenium Java - Build an Automation Script
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.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class MultiPageNavigationTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { // Assuming chromedriver is in system PATH driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); } @Test public void testMultiPageNavigationFlow() { // Step 1: Navigate to Home page driver.get("https://example.com/home"); // Step 2: Click Login button WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))); loginBtn.click(); // Step 3: Enter email WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("emailInput"))); emailInput.sendKeys("testuser@example.com"); // Step 4: Enter password WebElement passwordInput = driver.findElement(By.id("passwordInput")); passwordInput.sendKeys("TestPass123"); // Step 5: Click Submit button WebElement submitLogin = driver.findElement(By.id("submitLogin")); submitLogin.click(); // Step 6: Wait for Dashboard page and verify URL wait.until(ExpectedConditions.urlContains("/dashboard")); String currentUrl = driver.getCurrentUrl(); Assert.assertTrue(currentUrl.contains("/dashboard"), "URL should contain '/dashboard' after login"); // Step 7: Click Profile link WebElement profileLink = wait.until(ExpectedConditions.elementToBeClickable(By.id("profileLink"))); profileLink.click(); // Step 8: Wait for Profile page and verify URL wait.until(ExpectedConditions.urlContains("/profile")); currentUrl = driver.getCurrentUrl(); Assert.assertTrue(currentUrl.contains("/profile"), "URL should contain '/profile' after clicking profile link"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with Java and TestNG to automate the multi-page navigation flow.
Setup: The @BeforeClass method initializes the ChromeDriver and WebDriverWait with a 10-second timeout. The browser window is maximized for better visibility.
Test Steps: The test method testMultiPageNavigationFlow follows the manual test steps exactly:
- Navigate to the Home page URL.
- Click the Login button using an explicit wait to ensure it is clickable.
- Wait for the email input to be visible, then enter the email.
- Enter the password.
- Click the Submit button.
- Wait until the URL contains '/dashboard' to confirm login success, then assert this condition.
- Click the Profile link after waiting for it to be clickable.
- Wait until the URL contains '/profile' and assert this condition.
Teardown: The @AfterClass method closes the browser to clean up after the test.
This structure uses explicit waits to avoid timing issues and uses TestNG assertions to verify navigation success. Locators use By.id for reliability and clarity.
Now add data-driven testing with 3 different user credentials to verify login and navigation for each user.
Practice
Solution
Step 1: Understand multi-page navigation testing
It focuses on checking if users can move from one page to another as expected.Step 2: Identify the correct purpose
Among the options, only verifying page navigation matches this goal.Final Answer:
To verify users can move through pages correctly -> Option BQuick Check:
Multi-page navigation = verify page moves [OK]
- Confusing navigation testing with API or database testing
- Thinking CSS validation is part of navigation
- Ignoring the user flow aspect
Solution
Step 1: Recall Selenium command to open URL
The correct method is driver.get(url) to load a page.Step 2: Check options for correct syntax
Only driver.get("https://example.com") is valid Selenium Java syntax.Final Answer:
driver.get("https://example.com") -> Option AQuick Check:
Open URL = driver.get() [OK]
- Using non-existent methods like navigateTo or openPage
- Trying to click URLs directly
- Confusing navigation commands with click actions
driver.get("https://example.com/page1");
driver.findElement(By.id("nextBtn")).click();
String currentUrl = driver.getCurrentUrl();What will
currentUrl contain after execution if clicking the button navigates to page2?Solution
Step 1: Understand navigation flow in code
The driver opens page1, then clicks a button that navigates to page2.Step 2: Check what getCurrentUrl() returns after navigation
It returns the URL of the current page, which should be page2 after clicking.Final Answer:
"https://example.com/page2" -> Option DQuick Check:
After click, URL updates to new page [OK]
- Assuming URL stays the same after navigation
- Confusing getCurrentUrl() with getPageSource()
- Thinking click throws exception without checking element presence
driver.get("https://example.com/home");
WebElement link = driver.findElement(By.className("nav-link"));
link.click();
String title = driver.getTitle();
assert title.equals("Home Page");Solution
Step 1: Analyze locator usage
Using By.className("nav-link") may find multiple elements, causing ambiguity.Step 2: Check other code parts
Assertion syntax is correct, driver.quit() is optional, and getTitle() is valid for title check.Final Answer:
Using className locator without checking multiple matches -> Option CQuick Check:
className may match many elements [OK]
- Ignoring that className can match multiple elements
- Thinking getTitle() is wrong for title checks
- Believing driver.quit() is mandatory in every snippet
Solution
Step 1: Outline correct multi-page test steps
Start at login page, perform login, navigate via clicks, verify URLs after each step.Step 2: Evaluate options for best practice
Use driver.get() for login page, findElement().sendKeys() for login, click menu link, verify settings URL, click logout, verify login URL follows proper navigation and verification. Others misuse refresh, JavaScript URL changes, or skip verification.Final Answer:
Use driver.get() for login page, findElement().sendKeys() for login, click menu link, verify settings URL, click logout, verify login URL -> Option AQuick Check:
Navigate and verify URLs step-by-step [OK]
- Refreshing unnecessarily after clicks
- Changing URLs via JavaScript instead of real navigation
- Clicking without verifying page changes
Ready to prove it?
Pass the test and this pattern moves unknown
