TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Selenium WebDriver - Navigation Commands

Selenium WebDriver - Navigation Commands with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to SELENIUM

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:

to(String arg0) : void

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:

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

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

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

2. Forward Command

Method:

to(String arg0) : void

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:

 driver.navigate().forward();

3. Back Command

Method:

back() : void

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:

driver.navigate().back();

4. Refresh Command

Method:

refresh() : void

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:

driver.navigate().refresh();

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 javaTpoint website)
  • Come back to the Home page using the back command
  • Again go back to the javaTpoint 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:

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

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

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

// 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 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:

//Navigate to the desired URL
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
  • 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:

driver.findElement(By.linkText ()

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

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

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.

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

On click, the link will redirect the browser window to the official web page of javaTpoint 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 javaTpoint website.

// Go back to Home Page
 driver.navigate().back(); 
  • 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 javaTpoint website.

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

// Go forward to Registration page
driver.navigate().forward();
  • 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.

// 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 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)

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(); 
    }
}

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.





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf