Tuesday 23 August 2016

Selenium: WebDriver: Navigation API

WebDriver.Navigation interface provides functions to access the browser Back, Forward, and Refresh controls. You can get an instance of Navigation API by calling navigate method on WebDriver instance.

WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
                 
Following table summarizes the methods provided by WebDriver. Navigation api.

Method
Description
void back()
Move back to the browser history.
void forward()
Move forward in browser history.
void to(String url)
Load a new web page in the current browser window. This method blocks until the page is loaded fully.
void to(URL url)
Just like ‘void to(String url)’.
void refresh()
Refresh current page.


index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Selenium Tutorial</title>
</head>

<body>
 <h1>
  <a href="https://mail.google.com">gmail</a>
 </h1>
 <h1>
  <a href="https://www.google.co.in">google</a>
 </h1>
 <h1>
  <a href="https://www.facebook.com/">facebook</a>
 </h1>
 <h1>
  <a href="https://self-learning-java-tutorial.blogspot.com/"
  >Java Tutorial</a>
 </h1>
</body>
</html>


Assume index.jsp available at http://localhost:8080/application/.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("http://localhost:8080/application/");

        WebElement gmail = driver.findElement(By.linkText("gmail"));

        System.out.println("Opening gmail");
        gmail.click();
        Thread.sleep(4000);

        System.out.println("Going back to index.jsp");
        driver.navigate().back();
        Thread.sleep(4000);

        WebElement javaTutorial = driver.findElement(By
                .partialLinkText("Tutorial"));
        System.out.println("Opening Java tutorial");
        javaTutorial.click();
        Thread.sleep(4000);

        System.out.println("Going back to index.jsp");
        driver.navigate().back();
        Thread.sleep(4000);

        System.out.println("Moving one page forward");
        driver.navigate().forward();
        Thread.sleep(4000);

        driver.quit();
    }
}


Output
Opening gmail
Going back to index.jsp
Opening Java tutorial
Going back to index.jsp
Moving one page forward





Previous                                                 Next                                                 Home

No comments:

Post a Comment