Home » Selenium WebDriver – Navigation Commands

Selenium WebDriver – Navigation Commands

by Online Tutorials Library

Selenium WebDriver – Navigation Commands

WebDriver provides some basic Browser Navigation Commands that allows the browser to move backwards or forwards in the browser’s history.

Just like the browser methods provided by WebDriver, we can also access the navigation methods provided by WebDriver by typing driver.navigate() in the Eclipse panel.

Selenium WebDriver - Navigation Commands

Note: The methods having ‘Navigation’ as keyword are declared as Navigation commands.

Given are some of the most commonly used Browser Navigation commands for Selenium WebDriver.

1. Navigate To Command

Method:

In WebDriver, this method loads a new web page in the existing browser window. It accepts String as parameter and returns void.

The respective command to load/navigate a new web page can be written as:

Note: The get command (driver.get(URL);) which lies in the browser commands section does the same function as the navigate command

2. Forward Command

Method:

In WebDriver, this method enables the web browser to click on the forward button in the existing browser window. It neither accepts anything nor returns anything.

The respective command that takes you forward by one page on the browser’s history can be written as:

3. Back Command

Method:

In WebDriver, this method enables the web browser to click on the back button in the existing browser window. It neither accepts anything nor returns anything.

The respective command that takes you back by one page on the browser’s history can be written as:

4. Refresh Command

Method:

In WebDriver, this method refresh/reloads the current web page in the existing browser window. It neither accepts anything nor returns anything.

The respective command that takes you back by one page on the browser’s history can be written as:

Let us consider a sample test script which will cover most of the Navigation Commands provided by WebDriver.

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

  • Invoke Firefox Browser
  • Navigate to URL: https://www.testandquiz.com/selenium/testing.html
  • Click on the “This is a link” link (This link will redirect you to the tutoraspire website)
  • Come back to the Home page using the back command
  • Again go back to the tutoraspire website using forward command
  • Again come back to the Home page using To command
  • Refresh the Browser using Refresh command
  • Close the Browser

For our test purpose, we are using a dummy web page under the URL:

https://www.testandquiz.com/selenium/testing.html (You can also use this dummy web page for your Selenium Test Practices)

  • Step1. Launch Eclipse IDE and open the existing test suite “Demo_Test” which we have created in WebDriver Installation section of WebDriver tutorial.
  • Step2. Right click on the “src” folder and create a new Class File from New >Class.

Selenium WebDriver - Navigation Commands

Give your Class name as “Navigation_command” and click on “Finish” button.

Selenium WebDriver - Navigation Commands

Step3. Let’s get to the coding ground.

  • To invoke Firefox browser, we need to download Gecko driver and set the system property for Gecko driver.

Here is the sample code to set system property for Gecko driver:

After that we have to initialize Gecko Driver using Desired Capabilities Class.

Here is the sample code to initialize gecko driver using DesiredCapabilities class.

Combining both of the above code blocks, we will get the code snippet to launch Firefox browser.

  • After that we need to write the code which will automate our second test scenario (get the desired URL)

Here is the sample code to navigate to the desired URL:

  • To automate our third test scenario, first we have to uniquely identify the “This is a link” link on the dummy test page.

The method for finding a unique identification element involves inspection of HTML codes.

  1. Open URL: https://www.testandquiz.com/selenium/testing.html in your firefox browser.
  2. Right click on the “This is a link” link text and select Inspect Element.

Selenium WebDriver - Navigation Commands

It will launch a window containing all the specific codes involved in the development of the “This is a link” link. Select the name of the link text from inspector text box.

Selenium WebDriver - Navigation Commands

The Java Syntax for uniquely identifying a web element through its Link Text is written as:

Therefore, for locating the Link Text on the sample web page we will use the value of its Link Text:

Now, we need to write the code which will click on the Link Text.

Here is the sample code to click on the Link Text.

On click, the link will redirect the browser window to the official web page of tutoraspire website.

  • To automate our fourth test scenario, we have to revert the action performed by our third test scenario. To do that, we will use the Back command to undo the action performed on click of the link text.

Here is the sample code to return to the home page after being directed to the tutoraspire website.

  • Now, the next test scenario requires us to again go to the action performed by our third test scenario i.e., the window will again directed to the tutoraspire website.

Here is the sample code to go forward again to the official web page of tutoraspire website.

  • Now, to automate our sixth test scenario, we will require to again navigate to the home page of dummy website by using the To command.

Here is the sample code to go back to the home page.

  • To refresh the browser window, use the Refresh command as:
  • Finally, the given code snippet will terminate the process and close the browser.

Combining all of the above code blocks together, we will get the required source code to execute our test script “Navigation_command”.

The final test script will appear something like this:

(We have embedded comment in each section to explain the steps clearly)

To run the test script on Eclipse window, right click on the screen and click

Run as → Java application

Selenium WebDriver - Navigation Commands

Upon execution the test script will launch the Firefox browser and automate all the test scenarios.

You may also like