×

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

 

 


Related Topics

Xpath locator in Selenium IDE

X PATH LOCATOR: Xpath is used to identify the element using any attribute or visible test. Syntax for X PATH:- //html tag [@attribute=’value’] e.g. - //div [@class=’label’]                    OR //html tag [text () =’Visible Text’] e.g.-//div [text...

2 minutes read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Selenium WebDriver- Microsoft Edge Browser

Selenium WebDriver- Microsoft Edge Browser: In this tutorial, we will learn how to launch the Microsoft Edge browser in the Selenium WebDriver. Before we start automating our test script with Microsoft Edge...

4 minutes read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

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...

5 minutes read.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

3 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

Selenium WebDriver Tutorial

Seleniun WebDriver Introduction Selenium WebDriver is the essential tool of Selenium Tool Suite because it can directly communicate the browser without any server.  Selenium WebDriver is a browser automation framework that accepts...

3 minutes read.

Selenium WebDriver-CSS Selector: Tag Class and Attribute

Selenium WebDriver-CSS Selector: Tag, Class, and Attribute We will locate the web element with the help of Tag, class, and attribute CSS selector locator in this section of the tutorial. Where Tag,...

5 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

7 minutes read.

Selenium WebDriver - Firefox or Gecko (Marionette) browser

Selenium web driver-Firefox or Gecko (Marionette) browser In this tutorial, we are going to learn how to run the Selenium WebDriver test script in the Firefox Browser using the Gecko Driver. Before going...

4 minutes read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 minutes read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute read.

CSS Selector: Class in selenium IDE

CSS Selector: Class Identify a web element with the help of Class as a CSS Selector is like using id attribute. The only difference between Class and id is in their...

2 minutes read.