Using WebDriver

Every supported browser has its own driver class implementation. This section provides examples on how to use those different browsers.

In practice

Firefox

FirefoxDriver is part of WebDriver itself.

public void startFirefoxBrowser() {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://techblog.polteq.com/testshop/");
    System.out.println(driver.getTitle());
    driver.quit();
  }

Chrome

ChromeDriver is a separate driver executable, maintained by the Chromium team. Download url of the executable: http://code.google.com/p/chromedriver/downloads/list

public void startChromeBrowser() {
    System.setProperty("webdriver.chrome.driver", this
        .getClass().getClassLoader().getResource(
            "chromedriver.exe").getPath());
    WebDriver driver = new ChromeDriver();
    driver.get("http://techblog.polteq.com/testshop/");
    System.out.println(driver.getTitle());
    driver.quit();
  }

Internet explorer

InternetExplorerDriver is a separate executable. Download url of the executable: http://code.google.com/p/selenium/downloads/list

    public void startIEBrowser() {
        System.setProperty("webdriver.ie.driver", this
                .getClass().getClassLoader().getResource(
                        "IEDriverServer.exe").getPath());
        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://techblog.polteq.com/testshop/");
        System.out.println(driver.getTitle());
        driver.quit();
    }

HTMLunit driver

  public void htmlUnitDriver() {
    WebDriver driver = new HtmlUnitDriver();
    driver.get("http://techblog.polteq.com/testshop/");
    System.out.println(driver.getTitle());
    driver.quit();
  }

Remote driver

We need to use <code>RemoteWebDriver if we like to make use of Selenium Grid (Browsers that might be located elsewhere). Using the RemoteWebDriver results in a couple disadvantages, like: external servlet needs to be running and it introduces extra latency to our tests.

  public void remoteDriver() throws MalformedURLException {
    DesiredCapabilities cap = DesiredCapabilities.firefox();
    WebDriver driver = new RemoteWebDriver(new URL(
        "http://localhost:4444/wd/hub"), cap);
    driver.get("http://techblog.polteq.com/testshop/");
    System.out.println(driver.getTitle());
    driver.quit();
  }

2 thoughts on “Using WebDriver”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.