Home >>Selenium Tutorial >Selenium WebDriver - Navigation Commands

Selenium WebDriver - Navigation Commands

Selenium WebDriver - Navigation Commands

WebDriver provides several basic browser control commands enabling the user to move backward or forward throughout the history of the browser.

Much as WebDriver's browser methods, we can also use WebDriver's navigation methods by typing driver.navigate) (in the Eclipse section.

Provided are some of Selenium WebDriver's most widely used User Navigation Commands.

1. Navigate To Command

Method:

to(String arg0) : void

This method loads a new web page into the current browser window inside WebDriver. String is acknowledged as a parameter, and returns void.

The corresponding load / navigate instruction for a new web page may be written as:

driver.navigate().to("www.phptpoint.com");  

2. Forward Command

Method:

to(String arg0) : void  

This method in WebDriver allows the web browser to click on the forward button in the current browser window. It embraces nothing and it wants none.

The respective instruction that directs you through one page in the background of the app can be written as:

driver.navigate().forward();  

3. Back Command

Method:

back() : void  

This approach in WebDriver helps the web browser to press on the back button in the existing browser window. It embraces nothing and it wants none.

You may compose the respective command which takes you back to browser history by one page as:

driver.navigate().back();

4. Refresh Command

Method:

refresh() : void  

This process refreshes / reloads the latest web page in the same user window inside WebDriver. It embraces nothing and it wants none.

You may compose the respective command which takes you back to browser history by one page as:

driver.navigate().refresh();  

Let us find a sample test script that will cover much of WebDriver 's Navigation Commands.

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 link "This is a link" (This link will forward you to the javaTpoint website)
  • Use the back command to return to homepage
  • Go back to the website of javaTpoint, using forward command
  • Using the order to return to the home page
  • Refresh the browser with the Update command
  • Open your Browser

We use a dummy web site under the URL for our test purpose:

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

Step 1. Launch Eclipse IDE and open the current "Demo Test" test suite which we built in the WebDriver tutorial section of WebDriver Installation.

Step 2. Right click on the "src" folder and create a new Class File from New > Class. Give your Class name as "Navigation_command" and click on "Finish" button.

Step 3. Let's get to the coding ground.

  • To trigger the Firefox browser, Gecko driver must be downloaded and the system property configured for the Gecko driver.

Here's the sample code for Gecko driver setting system property:

// System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" )

We have to configure Gecko Driver after that using the Desired Capabilities class.

Here is the sample application for initializing gecko driver using class DesiredCapabilities.

// Initialize Gecko Driver using Desired Capabilities Class  
DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
capabilities.setCapability("marionette",true);  
WebDriver driver= new FirefoxDriver(capabilities);  

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

// System Property for Gecko Driver   
System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );  
          
// Initialize Gecko Driver using Desired Capabilities Class  
DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
capabilities.setCapability("marionette",true);  
WebDriver driver= new FirefoxDriver(capabilities);  
  • After that we will compose the code that will automate our second test scenario (get the URL you want)

Below is the sample code for navigating to the URL you want to use:

//Navigate to the desired URL  
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");  
  • To automate our third test scenario, we must first identify the "This is a link" link on the dummy test page in a unique way.

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

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

It will open a window that contains all the different codes that are involved in the "This is a link" link development. From the inspector text box select the name of the connection text.

The Java Syntax is written as: for the unique identification of a web element via its Link Text

driver.findElement(By.linkText (<linktext>)

For this purpose we would use the value of the Link Text to find the Link Text on the sample web page:

driver.findElement(By.linkText (<"This is a Link">))

Now we have to compose the code that will press on the Text Link.

Here is the sample code which you need to click on the text link.

// Click on the Link Text using click() command driver.findElement(By.linkText("This is a Link")).click();

Clicking on this link will move the browser window to the phptpoint website's official web page.

  • To simplify our fourth test scenario, we need to undo the behavior our third test scenario is doing. To do so, we must use the Back command to reverse the action done by tapping on the text of the link.

Here is the sample code to go back to home page after being led to the website of phptpoint.

// Go back to Home Page  
 driver.navigate().back();  
  • Now, the next test scenario allows us to go back to the operation of our third test scenario, i.e. the window should be directed to the javaTpoint web site again.

Here's the sample application to go to phptpoint's official web site again.

// Go forward to Registration page  
driver.navigate().forward();
  • Now, to simplify our sixth test case, use the To command, we'll need to navigate to the dummy website homepage again.

Below is the sample application for restoring to homepage.

// Go back to Home page  
driver.navigate().to(appUrl);  
  • To refresh the browser window, use the Refresh command as:
// Refresh browser  
driver.navigate().refresh();  
  • Finally, the given code snippet will terminate the process and close the browser.
driver.close();

Combining all the above code blocks together, we can get the source code needed to execute our "Navigation_command" test script

Anything similar will appear in the final test script:

(We inserted comments in-section to explicitly explain the steps)

import org.openqa.selenium.By;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.firefox.FirefoxDriver;    
import org.openqa.selenium.remote.DesiredCapabilities;    
    
public class Navigation_command {    
    
    public static void main(String[] args) {    
            
        // System Property for Gecko Driver     
        System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );    
                
        // Initialize Gecko Driver using Desired Capabilities Class    
            DesiredCapabilities capabilities = DesiredCapabilities.firefox();    
            capabilities.setCapability("marionette",true);    
            WebDriver driver= new FirefoxDriver(capabilities);    
                
            // Launch Website    
            driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");     
  
            //Click on the Link Text using click() command    
            driver.findElement(By.linkText("This is a Link")).click();  
  
            //Go back to Home Page  
            driver.navigate().back();   
  
            //Go forward to Registration page  
            driver.navigate().forward();  
  
            // Go back to Home page  
            driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");  
  
            //Refresh browser  
            driver.navigate().refresh();  
        
            //Closing browser  
            driver.close();   
    }  
}  

No Sidebar ads