Selenium 2.0 WebDriver – ChromeDriver

by seetaram on August 11, 2011

Selenium 2.0 provides different webdriver bindings for each browsers.  For example, firefox has FirefoxDriver and internet explorer has InternetExplorerDriver.  Google Chrome has ChromeDriver bindings.  Implementation is little different but there is no major change.  Only we need to understand the way it works.

For the FirefoxDriver, we need not start the server for execution of tests in standalone machine but for the ChromeDriver we need to start the server first and then start executing the tests from Eclipse.  Client will interact with the server with JsonWireProtocol and thus we can invoke Chrome browser and execute the tests on it.

Following is an example of just a Google search which uses ChromeDriver implementation:

package com.selftechy.wdriverbasics;

/*
 * Author - Seetaram Hegde
 * Copyrights - All rights reserved
 * 
 */
import java.io.File;
import java.io.IOException;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

@RunWith(BlockJUnit4ClassRunner.class)
public class ChromeDriverEx {

  private static ChromeDriverService srv;
  private WebDriver driver;

  @BeforeClass
  public static void StartServer() throws IOException {
	  //Below file path to Chrome browser should be changed accordingly
    srv = new ChromeDriverService.Builder()
        .usingChromeDriverExecutable(new File("F:\\Helios-Workspace\\WebDriver\\chromedriverServer\\chromedriver.exe"))
        .usingAnyFreePort()
        .build();
    srv.start();
  }

  @AfterClass
  public static void StopServer() {
    srv.stop();
  }

  @Before
  public void StartDriver() {
    driver = new RemoteWebDriver(srv.getUrl(),
        DesiredCapabilities.chrome());
  }

  @After
  public void StopDriver() {
    driver.quit();
  }

  @Test
  public void testChromeDriver() {
    driver.get("http://www.google.com");
    WebElement searchEdit = driver.findElement(By.name("q"));
    searchEdit.sendKeys("Selftechy on google");
    searchEdit.submit();
  }
}

Before executing the above code, please make sure that the Chrome browser is installed on the machine and appropriate JAR files are configured to the build path.

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email