Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Browser Navigation Commands in Selenium

Navigation commands are commands that are used to perform different operations such as backward, Forward, refresh, wait, etc in the browser’s history.

These commands can be accessed by creating a driver object using the reference variable of WebDriver just like browser methods. Let’s see the list of navigation commands provided by Selenium WebDriver.

To view a list of navigation commands, just type driver.navigate() in the Eclipse panel. Let’s see the following program source code.
Program source code 1:

package seleniumProject; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
public class ListOfNavigationCommands 
{ 
 public static void main(String[] args) 
 { 
   WebDriver driver = new FirefoxDriver(); 
   driver.navigate(). 
 } 
}


List of Navigation Commands:

 
Some of the most frequently used browser navigation commands provided by Selenium WebDriver is as follows:

Navigate Command

Method:

  navigate(Object o) : Navigation

The navigate method is present in the web driver interface. It returns an object of type Navigation, which is nested interface of WebDriver interface.

➲ We can attain a Navigation object by calling the navigate() method on the WebDriver object.

There are four methods in the navigation class that is frequently used. They are as follows:

To Command

Method:
  to(String arg) : void
➲ The to() method takes a String parameter and returns nothing.
➲ This method is used to navigate the URL of the web page in the current browser window. It will just navigate to the web page but not wait till the whole page gets loaded.
Syntax:
          driver.navigate().to(URL); // driver is a reference variable of WebDriver.

For example:
  driver.navigate().to(http://www.scientecheasy.com); 
// Or 
  String url = "http://www.scientecheasy.com"; driver.navigate().to(url);

Forward Command

Method:

  forward() : void

➲ The forward() method neither accepts nor returns anything.
➲ This method is used to move forward by one page on the browser’s history.
Syntax:
         driver.navigate().forward();

Back Command

Method:
  back() : void

➲ The back() method neither accepts nor returns anything.
➲ This method is used to move back by one page on the browser’s history.
Syntax:
        driver.navigate().back();

Refresh Command

Method:
  refresh() : void

➲ The refresh() method neither accepts nor returns anything.
➲ This method is used to refresh the current web page.
➲ It performs the same function as pressing F5 in the browser. 
Syntax:
       driver.navigate().refresh();

Let us take a sample test scenario that will be based on the getTitle(), length(), navigation Commands (such as to(), forward(), back(), and refresh()) provided by WebDriver.

Test scenarios to automate:

  • 1. Launch Firefox Browser.
  • 2. Navigate to URL: https://www.google.com
  • 3. Wait for Page to load completely.
  • 4. Click on the Gmail link text.
  • 5. Get the title of current web page.
  • 6. Get the length of title of current web page.
  • 7. Move back to the home page using the back command.
  • 8. Get title of the back page.
  • 9. Get length of title of the back page.
  • 10. Again go back to the previous page using forward command.
  • 11. Get title and length of the title of forward page.
  • 12. Now refresh the web page using refresh command.
  • 13. Close the browser.

Follow all the steps in the following program source code to understand the program clearly. So, let’s start coding.
Program source code:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class NavigationCommandExample 
{ 
 public static void main(String[] args) 
 { 
// Create a driver object to launch the Firefox browser. 
   WebDriver driver = new FirefoxDriver(); 

// Declare a variable URL with data type String and stores the URL of the web page. 
   String URL = "http://www.google.com"; 
// Call navigate().to() command to navigate the URL of the web page. 
   driver.navigate().to(URL); // It takes parameter URL as a String and returns nothing. 
// Wait for Page to load completely. 
   driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS ); 

// Click on Gmail link text. 
// Call findElement method using WebDriver reference variable 'driver'. 
   WebElement element = driver.findElement(By.linkText("Gmail")); // The return type of findElement method is WebElement. 
   element.click(); 
   String titlePage1 = driver.getTitle(); 
   System.out.println("Title of web page: " +titlePage1); 

// Move to the previous web page. 
// Call navigate().back() command to move back by one page on the browser's history. 
   driver.navigate().back(); 
 try 
{ 
  Thread.sleep(1000); 
} 
catch (InterruptedException e) 
{ 
  e.printStackTrace(); 
 } 
String titleOfBackpage = driver.getTitle(); 
System.out.println("Title of back page: " +titleOfBackpage); 
int titleLengthBP = titleOfBackpage.length(); 
System.out.println("Length of title of back page: " +titleLengthBP); 

// Move to the next web page. 
// Call navigate().forward() command to move forward by one page on the browser's history. 
   driver.navigate().forward(); // It takes nothing as parameter and returns nothing. 
   String titleOfForwardpage = driver.getTitle(); 
   System.out.println("Title of forward page: " +titleOfForwardpage); 
   int titleLengthFP = titleOfForwardpage.length(); // Return type of the length() method is an integer. 
   System.out.println("Length of title of forward page: " +titleLengthFP); 

// Refresh the current webpage. 
// Call navigate().refresh() command to refresh the current page. 
   driver.navigate().refresh(); // accepts nothing as a parameter and returns nothing. 
   System.out.println("Refresh is successfully"); 
   String titleOfRefreshpage = driver.getTitle(); // line 1. 
   int titleLengthRP = titleOfRefreshpage.length(); // line 2. // Since return type of length() method is integer. Therefore, we will store it using a variable with data type integer. 

  System.out.println("Length of title of refresh page: " +titleLengthRP); // The above two lines (line 1 and line 2) of code can also be written in one step like this. 
// int titleLengthRP = driver.getTitle().length(); 

// Close the browser. 
   driver.close(); // It takes nothing as a parameter and returns nothing. 
  } 
}
Output: 
      Title of web page: Gmail - Free Storage and Email from Google 
      Title of back page: Google 
      Length of title of back page: 6 
      Title of forward page: Gmail - Free Storage and Email from Google 
      Length of title of forward page: 42 
      Refresh is successfully 
      Length of title of refresh page: 42

 Explanations:

1. When the statement WebDriver driver=new FirefoxDriver(); will be executed, the driver will simply launch the firefox browser.
2. When the statement driver.navigate().to(URL); will be executed, it will navigate to the URL of the web page.
3. The findElement() method will find a web element on the current web page by using the locator (linkText) provided in the parameter list.
The method click() will click on the WebElement. You will learn WebElement in more detail in the next tutorial.

4. The getTitle() method will fetch the title of the web page and print it on the console.
5. When driver.navigate().back(); command will be executed, the driver will move back one page on the browser’s history.
6. navigate().forward() command will move forward by one page on the browser’s history.
7. navigate().refresh() command will refresh the current page.
8. close() method will close the browser.

Difference between driver.get() and driver.navigate().to() methods


The difference between get() and navigate() methods of Selenium WebDriver are as follows:

driver.get()
driver.navigate().to()
1. The driver.get() method is used to open a specified URL of web application and wait until the whole page gets loaded. 1. The driver.navigate().to() method is used to navigate a specified URL of web application and it does not wait till the whole page gets loaded.
2. It does not maintain the browser history and cookies. 2. It maintains the browser history and cookies.
3. We can not go forward or backward between the pages using the driver.get(). 3. We can easily navigate between the pages forward, back, and refresh using driver.navigate().

Final words

Hope that this tutorial has covered almost all important points related to navigation commands in Selenium WebDriver. I hope that you will have understood this topic and enjoyed in automating scenarios.
Thanks for reading!!!
Next ⇒ Selenium WebElement commands⇐ PrevNext ⇒

The post Browser Navigation Commands in Selenium appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Browser Navigation Commands in Selenium

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×