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.

Navigate.Refresh()

Method Name: navigate().refresh()
Syntax: driver.navigate().refresh();
Purpose: It refreshes the current web page
Parameters: N/A

Example:

@Test
	public void navigationRefreshExample()
        {
		driver= new FirefoxDriver();
		String URL="http://www.facebook.com";
		driver.navigate().to(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

@Test
	public void testRefreshPage() {
		
		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().

Selenium Tutorials: 

Comments

I am not able to execute navigate method in my eclipse IDE.

thanks for sharing the info . So there are 4 commands in navigate
1) to();
2) forward()
3) back()
4) refresh();

It would be better to mention Navigate.To like navigate.to() everwhere i.e. N and T should be small

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.