Navigation Methods in Webdriver with Examples


Navigate.To(URL)


This method 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.

package snippet;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Snippet {
@Test
public void navigationToURLExample() {
FirefoxDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.google.com");
}
}

Navigate.Back()
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.

package snippet;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Snippet {
@Test
public void navigationToURLExample() {
FirefoxDriver 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()

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.

package snippet;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Snippet {
@Test
public void navigationToURLExample() throws InterruptedException {
FirefoxDriver 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.

Navigate.Refresh()

It refreshes the current web page

package snippet;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Snippet {
@Test
public void navigationToURLExample() throws InterruptedException {
FirefoxDriver driver = new FirefoxDriver();
String URL = "http://www.facebook.com";
driver.get(URL);
driver.findElement(By.linkText("Forgot your password?")).click();
driver.findElement(By.id("identify_email")).sendKeys("sample@email.com");
driver.navigate().refresh();
}
}

Below is the sample code to perform page refresh using Action Driver class

package snippet;

import org.openqa.selenium.Keys;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class Snippet {
@Test
public void navigationToURLExample() throws InterruptedException {
FirefoxDriver driver = new FirefoxDriver();
driver.navigate().to("http://google.com");
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
}
}


There are few scenarios where we need to check if the data is getting cleared once when the page refreshes. And for few websites , it will display an alert when the user tries to refresh the page without saving the form which user has entered data.

In the above example, we are just refreshing the web page after entering the values using sendkeys().