public class ChromeDriver extends RemoteWebDriver implements LocationContext, WebStorage, HasTouchScreen, NetworkConnection
WebDriver implementation that controls a Chrome browser running on the local machine.
This class is provided as a convenience for easily testing the Chrome browser. The control server
which each instance communicates with will live and die with the instance.
To avoid unnecessarily restarting the ChromeDriver server with each instance, use a
RemoteWebDriver coupled with the desired ChromeDriverService, which is managed
separately. For example:
import static org.junit.Assert.assertEquals;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
{@literal @RunWith(JUnit4.class)}
public class ChromeTest extends TestCase {
private static ChromeDriverService service;
private WebDriver driver;
{@literal @BeforeClass}
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
.usingAnyFreePort()
.build();
service.start();
}
{@literal @AfterClass}
public static void createAndStopService() {
service.stop();
}
{@literal @Before}
public void createDriver() {
driver = new RemoteWebDriver(service.getUrl(),
DesiredCapabilities.chrome());
}
{@literal @After}
public void quitDriver() {
driver.quit();
}
{@literal @Test}
public void testGoogleSearch() {
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("webdriver");
searchBox.quit();
assertEquals("webdriver - Google Search", driver.getTitle());
}
}
Note that unlike ChromeDriver, RemoteWebDriver doesn't directly implement
role interfaces such as LocationContext and WebStorage.
Therefore, to access that functionality, it needs to be
augmented and then cast
to the appropriate interface.RemoteWebDriver.RemoteTargetLocator, RemoteWebDriver.RemoteWebDriverOptions, RemoteWebDriver.WhenNetworkConnection.ConnectionTypeWebDriver.ImeHandler, WebDriver.Navigation, WebDriver.Options, WebDriver.TargetLocator, WebDriver.Timeouts, WebDriver.Window| Constructor and Description |
|---|
ChromeDriver()
Creates a new ChromeDriver using the
default
server configuration. |
ChromeDriver(Capabilities capabilities)
Creates a new ChromeDriver instance.
|
ChromeDriver(ChromeDriverService service)
Creates a new ChromeDriver instance.
|
ChromeDriver(ChromeDriverService service,
Capabilities capabilities)
Creates a new ChromeDriver instance.
|
ChromeDriver(ChromeDriverService service,
ChromeOptions options)
Creates a new ChromeDriver instance with the specified options.
|
ChromeDriver(ChromeOptions options)
Creates a new ChromeDriver instance with the specified options.
|
| Modifier and Type | Method and Description |
|---|---|
LocalStorage |
getLocalStorage() |
NetworkConnection.ConnectionType |
getNetworkConnection()
Query the driver for the Airplane Mode setting state
|
SessionStorage |
getSessionStorage() |
TouchScreen |
getTouch() |
void |
launchApp(java.lang.String id)
Launches Chrome app specified by id.
|
Location |
location()
Gets the physical location of the browser.
|
void |
setFileDetector(FileDetector detector)
Set the file detector to be used when sending keyboard input.
|
void |
setLocation(Location location)
Sets the physical location.
|
NetworkConnection.ConnectionType |
setNetworkConnection(NetworkConnection.ConnectionType type)
Set the Connection type Not all connection type combinations are valid for an individual type
of device and the remote endpoint will make a best effort to set the type as requested
|
close, execute, execute, executeAsyncScript, executeScript, findElement, findElement, findElementByClassName, findElementByCssSelector, findElementById, findElementByLinkText, findElementByName, findElementByPartialLinkText, findElementByTagName, findElementByXPath, findElements, findElements, findElementsByClassName, findElementsByCssSelector, findElementsById, findElementsByLinkText, findElementsByName, findElementsByPartialLinkText, findElementsByTagName, findElementsByXPath, get, getCapabilities, getCommandExecutor, getCurrentUrl, getElementConverter, getErrorHandler, getExecuteMethod, getFileDetector, getKeyboard, getMouse, getPageSource, getScreenshotAs, getSessionId, getTitle, getWindowHandle, getWindowHandles, log, manage, navigate, perform, quit, resetInputState, setCommandExecutor, setElementConverter, setErrorHandler, setFoundBy, setLogLevel, setSessionId, startClient, startClient, startClient, startSession, startSession, stopClient, stopClient, stopClient, switchTo, toStringpublic ChromeDriver()
default
server configuration.public ChromeDriver(ChromeDriverService service)
service will be started along with the driver,
and shutdown upon calling RemoteWebDriver.quit().service - The service to use.RemoteWebDriver.RemoteWebDriver(org.openqa.selenium.remote.CommandExecutor, Capabilities)public ChromeDriver(Capabilities capabilities)
capabilities will be passed to the
chromedriver service.capabilities - The capabilities required from the ChromeDriver.ChromeDriver(ChromeDriverService, Capabilities)public ChromeDriver(ChromeOptions options)
options - The options to use.ChromeDriver(ChromeDriverService, ChromeOptions)public ChromeDriver(ChromeDriverService service, ChromeOptions options)
service will be
started along with the driver, and shutdown upon calling RemoteWebDriver.quit().service - The service to use.options - The options to use.public ChromeDriver(ChromeDriverService service, Capabilities capabilities)
service will be started along with the
driver, and shutdown upon calling RemoteWebDriver.quit().service - The service to use.capabilities - The capabilities required from the ChromeDriver.public void setFileDetector(FileDetector detector)
RemoteWebDriversetFileDetector in class RemoteWebDriverdetector - The detector to use. Must not be null.FileDetector,
LocalFileDetector,
UselessFileDetectorpublic LocalStorage getLocalStorage()
getLocalStorage in interface WebStoragepublic SessionStorage getSessionStorage()
getSessionStorage in interface WebStoragepublic Location location()
LocationContextlocation in interface LocationContextLocation containing the location information. Returns null if the location is
not availablepublic void setLocation(Location location)
LocationContextsetLocation in interface LocationContextlocation - A Location containing the new location informationpublic TouchScreen getTouch()
getTouch in interface HasTouchScreenpublic NetworkConnection.ConnectionType getNetworkConnection()
NetworkConnectiongetNetworkConnection in interface NetworkConnectionNetworkConnection.ConnectionType indicating if the
device is in Airplane Modepublic NetworkConnection.ConnectionType setNetworkConnection(NetworkConnection.ConnectionType type)
NetworkConnectionsetNetworkConnection in interface NetworkConnectiontype - ConnectionType of what the network connection should beNetworkConnection.ConnectionType of what the
device's network connection ispublic void launchApp(java.lang.String id)
id - chrome app id