Monday 11 May 2015

Example of Navigation Methods in Web driver API



Example of Navigation Methods in Web driver API
Navigate.To(URL)
Method/Function Name: navigate.to(URL)
Syntax: driver.navigate().to(URL);
Uses: Navigate To is a   method for loading a new web page in the current browser(FF,ie…so on) window. This action have done using a HTTP GET operation, and the method will block the next execution until the load is complete.
Parameters: URL – Should be fully qualified URL like http://silenumnmisc.blogspot.in/
 “ie”:-
@Test
private void navigationToURLIestMeethod()
{
driver= new FirefoxDriver();
driver.navigate().to("http://silenumnmisc.blogspot.in/");
}
Navigate.To(String)
Method or Function Name: navigate.to(String)
Syntax: driver.navigate().to(String);
Uses: navigate() function or methods load a new web page in the current browser window like FF, ie, Safari….this is a overloaded version of to(String) that makes it easy to pass in to this URL(http://silenumnmisc.blogspot.in/).
Parameters example: URL String
 “ie”:-
 @Test
private void navigationToStringIeTestMethod()
{
driver= new FirefoxDriver();
driver.navigate().to(URL);
}
Navigate.Back() Method
Method Name/Function : navigate().back()
Syntax: driver.navigate().back();
Uses: It is used for to move back a single "item" in the web browser history and it will not perform any action if you are on the first page viewed like no previous maintained by browser.
Parameters: not applicable
 “ie”:-
@Test
private void navigationBackIeMethod()
{
driver= new FirefoxDriver();
driver.navigate().to(URL);
driver.findElement(By.linkText("Forgot your password?")).click();
//Below is the statement to return in to back stage
driver.navigate().back();
}
In the above ie, while working with navigate back method, we need to travel to one single page in the browser history(to maintain browser history).
We have opened the first page is facebook.com page and then traveling to forgot password page. Currently the browser has two pages maintain in the history. If we currently use navigate.back() method, it will redirect the user to facebook.com page.
Navigate.Forward()
Method Name/Function: navigate().forward()
Syntax: driver.navigate().forward();
Uses: Moveing a single "item" forward in the web browser history and it won’t perform any action if we are in the latest page viewed.
Parameters: Not applicable
 “ie”
@Test
private void navigationForwardIe()
 {
driver= new FirefoxDriver();
driver.navigate().to(URL);
driver.findElement(By.linkText("Forgot your password?")).click();
driver.navigate().back();
driver.navigate().forward();
}
In the above ie, while work with navigate forward method, we require to travel to one or more pages in the browser history.
Initially first page we have opened is facebook.com page and then traveled to forget password page. Currently the browser has two pages in the history. If we now use navigate.back method, it will redirect the particular user to facebook.com page and now again if we use navigate.forward(), it will redirect the user to forgot password page.
Navigate.Refresh()
Method Name: navigate().refresh()
Syntax: driver.navigate().refresh();
Uses: It will refreshe the current web page
Parameters: Not Applicable
 “ie”:-
@Test
private void navigationRefreshIeMethod()
{
driver= new FirefoxDriver();
driver.navigate().to(URL);
driver.findElement(By.linkText("Forgot your password?")).click();
driver.findElement(By.id("identify_email")).sendKeys("abc@email.com");
driver.navigate().refresh();
}
Below is the sample script to perform page refresh using Action Driver class
@Test
private void testRefreshPage() {
driver= new FirefoxDriver();
driver.navigate().to("http://silenumnmisc.blogspot.in/");
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
}
Only some scenario where we eager need to check if the data is getting cleared once when the page refreshes and for few websites, it will show an alert when the user tries to refresh the page without saving the form which user has submitted data.
In the above ie, we are now refreshing the web page under test after entering the values using sendkeys() function.

No comments:

Post a Comment