Chrome Driver

Developed in collaboration with the Chromium team, the ChromeDriver is a standalone server which implements WebDriver's wire protocol.

Requirements

The ChromeDriver controls the browser using Chrome's automation proxy framework. Consequently, the ChromeDriver is only compatible with Chrome version 12.0.712.0 or newer.

The server expects you to have Chrome installed in the default location for each system:

 

OS Expected Location of Chrome
Linux /usr/bin/google-chrome1
Mac /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe

1 For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual binary. See also the section on overriding the Chrome binary location .

Getting Started

To get set up, first download the appropriate prebuilt server. Make sure the downloaded server can be located on your PATH or specify its location via the webdriver.chrome.driver system property. Finally, all you need to do is create a new ChromeDriver instance:

WebDriver driver = new ChromeDriver();
driver
.get("http://www.google.com");

Running the server in a child process

You may notice that the ChromeDriver class is merely a convenience class that starts the server upon creation and shuts it down when you callquit. While the server is light weight, starting and stopping it multiple times will add a noticeable delay to a larger test suite. To compensate for this, you can directly control the life and death of the server using the ChromeDriverService:

 
  1. import static org.junit.Assert.assertEquals; 
  2.  
  3. import org.junit.After; 
  4. import org.junit.AfterClass; 
  5. import org.junit.Before; 
  6. import org.junit.BeforeClass; 
  7. import org.junit.runner.RunWith; 
  8. import org.junit.runners.BlockJUnit4ClassRunner 
  9. import org.openqa.selenium.chrome.ChromeDriverService; 
  10. import org.openqa.selenium.remote.DesiredCapabilities; 
  11. import org.openqa.selenium.remote.RemoteWebDriver; 
  12.  
  13. @RunWith(BlockJUnit4ClassRunner.class)} 
  14. public class ChromeTest extends TestCase { 
  15.  
  16.   private static ChromeDriverService service; 
  17.   private WebDriver driver; 
  18.  
  19.   @BeforeClass 
  20.   public static void createAndStartService() { 
  21.     service = new ChromeDriverService.Builder() 
  22.         .usingChromeDriverExecutable(new File("path/to/my/chromedriver")) 
  23.         .usingAnyFreePort() 
  24.         .build(); 
  25.     service.start(); 
  26.   } 
  27.  
  28.   @AfterClass 
  29.   public static void createAndStopService() { 
  30.     service.stop(); 
  31.   } 
  32.  
  33.   @Before 
  34.   public void createDriver() { 
  35.     driver = new RemoteWebDriver(service.getUrl(), 
  36.         DesiredCapabilities.chrome()); 
  37.   } 
  38.  
  39.   @After 
  40.   public void quitDriver() { 
  41.     driver.quit(); 
  42.   } 
  43.  
  44.   @Test 
  45.   public void testGoogleSearch() { 
  46.     driver.get("http://www.google.com"); 
  47.     WebElement searchBox = driver.findElement(By.name("q")); 
  48.     searchBox.sendKeys("webdriver"); 
  49.     searchBox.quit(); 
  50.     assertEquals("webdriver - Google Search", driver.getTitle()); 
  51.   } 

Running the server as a standalone process

Since the ChromeDriver implements the wire protocol, it is fully compatible with any RemoteWebDriver client. Simply start up your server, create a client, and away you go:

WebDriver driver = new RemoteWebDriver("http://localhost:9515", DesiredCapabilities.chrome());
driver
.get("http://www.google.com");