Selenium WebDriver - Navigation Controls Commands

Navigation Controls Commands in Selenium WebDriver

WebDriver is used to hold the browser history and perform browser navigation operations like, back, forward, and refresh.

Whenever a WebDriver launches a browser, all the add-ons and history will be disabled automatically.

Like, the browser controls provided by WebDriver, we can also invoke the navigation control methods by typing driver.navigate(). In the Eclipse console window.

Navigation Controls Commands

Note: Navigation commands are declared when the method has a ‘Navigation’ keyword.

Some of the most commonly used Navigation methods for Selenium WebDriver is as follows:-

  • to()
  • back()
  • forward()
  • refresh()
Navigation methods for Selenium Web Driver
  • navigate().to() Command

The navigate().to() method is used to load a new web page in the existing browser window.

Syntax:

 to(String arg0) : void  

The return type of ‘to’ command is as follow:

Parameter Return Type
String void

To load/navigate a new web page using to() command:

driver.navigate().to();

Note: The get command of the browser commands and navigate to command has the same functionality; both commands are used to load the web pages in the web browser.

E.g., we are loading the official website of Tutorial and example.

driver.navigate().to("https://www.tutorialandexample.com");  
  • back() Command:

The back method is used to enable the web browser while clicking on the back button in the existing browser window.

Syntax:

back() : void  

The return type of back command is as follow:

Parameter Return Type
No parameter usage No return type

This command takes you back by one page on the browser's history.

driver.navigate().back();  
  • Forward Command :

The forward method enables the web browser to click on the forward button in the existing browser window.

Syntax:

forward(Stringarg0):void

The return type of forward command is as follow:

Parameter Return Type
No parameter usage no return type

This command takes you forward by one page on the browser's history.

driver.navigate().forward();
  • Refresh Command:

The refresh method is used to refresh/reloads the current web page in the existing browser window.

Syntax:

 refresh() : void

The return type of refresh command is as follow:

Parameter Return Type
No parameter usage String

This method will refresh your page.

 driver.navigate().refresh();

Let us take a sample test script in which will try to cover mostly used Navigation Commands:

In this test, we will automate the following test scenarios:

Steps Actions Method used input Expected result
1. Invoke the Google Chrome Browser       The Chrome browser must be opened.
2. open URL: https://www.google.com   get() www.google.com The google search page must be opened.
3. navigate to URL: https://www.tutorialandexample.com   navigate.to() www.tutorialandexample.com The tutorial and example home page must be displayed.
4. Come back to the google home page using the back command.   back()   After using the back command, the google home page must be opened.
5. Now we are going back to the Tutorial and example website using forward command.   forward()   After using the forward command, the tutorial and example home page must be displayed.
6. Refresh the existing page using the Refresh command.   refresh()   The Refresh Command refresh the current page.
7. Close the Browser.   close()   The browser should be closed.

For our testing purpose, we are using Google and tutorial and example websites to perform all the navigation commands.

We are creating our test cases step by step to give you a complete understanding of how to use navigation Commands in WebDriver.

  • Open the Eclipse IDE and open the existing test suite new_test, which we have created in WebDriver Installation section of WebDriver tutorial.
  • Right-click on the folder called src and create a new Class File from New > Class.
how to use navigation Commands in WebDriver
  • Give the Class name as Navigation_controls and click on the Finish button.
New Java Class

Step1:

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

You can also refer to WebDriver on Chrome Browser to learn how to download and set the System property for Chrome driver.

Hence, here is the code for setting the system property for google chrome:

//set the system property of Google Chrome System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe");   

To initialize the Chrome driver using Chrome Driver class:

// create driver object for CHROME browser
 WebDriver driver=new ChromeDriver(); 

Step2:

To launch the website:

driver.get("https://www.google.com");

Step3:

Now, we are going to navigate our second website using navigate to command.

//to load the new web page in the existing browser
 driver.navigate().to("https://www.tutorialandexample.com"); 

Step4:

The next test scenario is to come back to google home page using the back command

// coming back to the google page using back command.
    driver.navigate().back();
  //print a simple statement to verify that the particular command is working or not.
 System.out.println("back command is performed"); 

Step5:

To automate our next test scenario, which is going back to the tutorial and examplewebsite using the forward command.

// going back to the tutorial and example page using forward command.
 driver.navigate().forward();
 System.out.println("forward command is performed"); 

Step6:

To refresh the existing web page, we are using the refresh command.

//refresh the existing page
 driver.navigate().refresh();
 System.out.println(" refresh command is performed"); 

Step7:

Finally, we terminate the process and close the browser.

//Closing the browser
 driver.close();

Combining all of the above code blocks, we will get the required source code to execute our test script Navigation _controls.

The final test script will look like this-

package testpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Navigation_controls {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe"); 
// create driver object for CHROME browser
WebDriver driver=new ChromeDriver();
 //launch the website
 driver.get("https://www.google.com");
 //to load the new web page in the existing browser
 driver.navigate().to("https://www.tutorialandexample.com");                   
 //come back to the google search page 
 driver.navigate().back();
System.out.println("back command is performed");
 // going back to the tutorial and example page using forward command
driver.navigate().forward();
 System.out.println("forward command is performed");
 //refresh the existing page 
driver.navigate().refresh();
System.out.println(" refresh command is performed");
 //Closing the browser 
driver.close();
}
}
  • To run the test script in Eclipse, right-click on the console window and then click Run as > Java application.
  • The test script will be launched in the chrome browser and automate all the test scenarios.
  • And the output console window will show the results of all the print commands.
console window will show the results of all the print commands