Navigate commands in Selenium WebDriver

Navigate in selenium: As we work with web pages, we may need to move forward or back of the pages to verify something or to analyze the page behavior. Navigate feature is used in selenium web driver to achieve these functionalities.

    • driver.navigate().back();
    • driver.navigate().forward();
    • driver.navigate().refresh();
    • driver.navigate().to(“https://selenium99.com”);



driver.navigate().back() – Used to move page backward by one level. It emulates the browsers Back button.

driver.navigate().forward() – Used to move page forward by one level.

driver.navigate().refresh() – Used for current page refresh.

driver.navigate().to(” “) – This method loads the given URL. Input parameter for this method is a URL in the form of String.

 

Example:

package com;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Navigate {

static WebDriver driver;
String homepageTitle = “”;
String nextTitle = “”;

@BeforeTest
public void before() throws Exception {
// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();

// implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// this opens the url
driver.get(“https://selenium99.com/”);
Thread.sleep(1000);
homepageTitle = driver.getTitle();
}

@Test
public void navigate_selenium() throws Exception {
driver.navigate().to(“https://selenium99.com/selenium-tutorials/”);
nextTitle = driver.getTitle();
Thread.sleep(1000);

//back
driver.navigate().back();
Thread.sleep(1000);
Assert.assertTrue(“Page has moved backwards.”, homepageTitle.equals(driver.getTitle()));

//forward
driver.navigate().forward();
Thread.sleep(1000);
Assert.assertTrue(“Page has moved forwards.”, nextTitle.equals(driver.getTitle()));

//refresh
driver.navigate().refresh();
Thread.sleep(1000);
Assert.assertTrue(“Page has refreshed successfully.”, nextTitle.equals(driver.getTitle()));
}


@AfterTest
public void tearDown() {

driver.close();
}
}

Output on the console:

PASSED: navigate_selenium

Explanation: In this script,

1. First we are navigating to homepage and storing the page title in a variable named homepageTitle.

2. Then navigating to a different page and storing the page title in a variable named nextTitle.

3. Moving backwards and verifying the title of the page by using an Assertion.

4. Moving forwards and verifying the title of the page by using an Assertion.

5. Refreshing the page and verifying the title of the page by using an Assertion.


Other selenium topics:

TestNG and annotations

How to handle mouse hover events using Selenium WebDriver

WebElement actions in selenium WebDriver

Top 25 selenium interview questions

How to take screenshot using Selenium WebDriver