Monday 16 May 2016

Navigation Methods in WebDriver with Examples


Navigate.To(URL)

Method Name: navigate.to(URL)
Syntax: driver.navigate().to(URL);
Purpose: This methods Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.
Parameters: URL – It should be a fully qualified URL.
Example:
@Test
 public void navigationToURLExample()
 {
  
  driver= new FirefoxDriver();
  driver.navigate().to("http://www.google.com");
 }

Navigate.To(String)

Method Name: navigate.to(String)
Syntax: driver.navigate().to(String);
Purpose: This methods Load a new web page in the current browser window. It is an Overloaded version of to(String) that makes it easy to pass in a URL.
Parameters: URL String
Example:
 
@Test
 public void navigationToStringExample()
       {
  driver= new FirefoxDriver();
  String URL="http://www.facebook.com";
  driver.navigate().to(URL);
 }

Navigate.Back()

Method Name: navigate().back()
Syntax: driver.navigate().back();
Purpose: To move back a single "item" in the web browser's history. And it will not perform any action if you are on the first page viewed.
Parameters: N/A
Example:
@Test
 public void navigationBackExample()
       {
  driver= new FirefoxDriver();
  String URL="http://www.facebook.com";
  driver.navigate().to(URL);
  driver.findElement(By.linkText("Forgot your password?")).click();
  driver.navigate().back();
 }
In the above example, to work with navigate back method, we atleast need to travel to one single page in the browser history.
The first page we have opened is facebook.com page and then traveled to forgot password page. Now the browser has two pages in the history. If we now use navigate.back(), it will redirect the user to facebook.com page.

Navigate.Forward()

Method Name: navigate().forward()
Syntax: driver.navigate().forward();
Purpose: To move a single "item" forward in the web browser's history. And it will not perform any action if we are on the latest page viewed.
Parameters: N/A
Example:
@Test
 public void navigationForwardExample()
       {
  driver= new FirefoxDriver();
  String URL="http://www.facebook.com";
  driver.navigate().to(URL);
  driver.findElement(By.linkText("Forgot your password?")).click();
  driver.navigate().back();
  Thread.sleep(1000);
  driver.navigate().forward();
 }
In the above example, to work with navigate forward method, we atleast need to travel to one or multiple pages in the browser history.
The first page we have opened is facebook.com page and then traveled to forgot password page. Now the browser has two pages in the history. If we now use navigate.back(), it will redirect the user to facebook.com page.
And now again if we navigate.forward(), it will redirect the user to forgot password page.

1 comment: