Chrome Driver

Developed in collaboration with the Chromium team, the ChromeDriver is a standalone server which implements WebDriver'swire 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

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

Getting Started

To get set up, firstdownloadthe appropriate prebuilt server. Make sure the downloaded server can be located on yourPATHor specify its location via thewebdriver.chrome.driversystem property. Finally, all you need to do is create a newChromeDriverinstance:

WebDriverdriver=newChromeDriver();driver.get("http://www.google.com");

Running the server in a child process

You may notice that theChromeDriverclass 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 theChromeDriverService:

importstaticorg.junit.Assert.assertEquals;

importorg.junit.After;

importorg.junit.AfterClass;

importorg.junit.Before;

importorg.junit.BeforeClass;

importorg.junit.runner.RunWith;

importorg.junit.runners.BlockJUnit4Cla***unner

importorg.openqa.selenium.chrome.ChromeDriverService;

importorg.openqa.selenium.remote.DesiredCapabilities;

importorg.openqa.selenium.remote.RemoteWebDriver;

@RunWith(BlockJUnit4Cla***unner.class)}

publicclassChromeTestextendsTestCase {

privatestaticChromeDriverService service;

privateWebDriver driver;

@BeforeClass

publicstaticvoidcreateAndStartService() {

service = newChromeDriverService.Builder()

.usingChromeDriverExecutable(newFile("path/to/my/chromedriver"))

.usingAnyFreePort()

.build();

service.start();

}

@AfterClass

publicstaticvoidcreateAndStopService() {

service.stop();

}

@Before

publicvoidcreateDriver() {

driver = newRemoteWebDriver(service.getUrl(),

DesiredCapabilities.chrome());

}

@After

publicvoidquitDriver() {

driver.quit();

}

@Test

publicvoidtestGoogleSearch() {

driver.get("http://www.google.com");

WebElement searchBox = driver.findElement(By.name("q"));

searchBox.sendKeys("webdriver");

searchBox.quit();

assertEquals("webdriver - Google Search", driver.getTitle());

}

}

Running the server as a standalone process

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

WebDriverdriver=newRemoteWebDriver("http://localhost:9515",DesiredCapabilities.chrome());driver.get("http://www.google.com");

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐