public class ChromeDriver
extends org.openqa.selenium.remote.RemoteWebDriver
implements org.openqa.selenium.html5.LocationContext, org.openqa.selenium.html5.WebStorage, org.openqa.selenium.interactions.HasTouchScreen, org.openqa.selenium.mobile.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.org.openqa.selenium.remote.RemoteWebDriver.RemoteTargetLocator, org.openqa.selenium.remote.RemoteWebDriver.RemoteWebDriverOptions, org.openqa.selenium.remote.RemoteWebDriver.When
org.openqa.selenium.mobile.NetworkConnection.ConnectionType
org.openqa.selenium.WebDriver.ImeHandler, org.openqa.selenium.WebDriver.Navigation, org.openqa.selenium.WebDriver.Options, org.openqa.selenium.WebDriver.TargetLocator, org.openqa.selenium.WebDriver.Timeouts, org.openqa.selenium.WebDriver.Window
Constructor and Description |
---|
ChromeDriver()
Creates a new ChromeDriver using the
default
server configuration. |
ChromeDriver(org.openqa.selenium.Capabilities capabilities)
Deprecated.
Use
ChromeDriver(ChromeOptions) instead. |
ChromeDriver(ChromeDriverService service)
Creates a new ChromeDriver instance.
|
ChromeDriver(ChromeDriverService service,
org.openqa.selenium.Capabilities capabilities)
Deprecated.
Use
ChromeDriver(ChromeDriverService, ChromeOptions) instead. |
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 |
---|---|
org.openqa.selenium.html5.LocalStorage |
getLocalStorage() |
org.openqa.selenium.mobile.NetworkConnection.ConnectionType |
getNetworkConnection() |
org.openqa.selenium.html5.SessionStorage |
getSessionStorage() |
org.openqa.selenium.interactions.TouchScreen |
getTouch() |
void |
launchApp(java.lang.String id)
Launches Chrome app specified by id.
|
org.openqa.selenium.html5.Location |
location() |
void |
setFileDetector(org.openqa.selenium.remote.FileDetector detector) |
void |
setLocation(org.openqa.selenium.html5.Location location) |
org.openqa.selenium.mobile.NetworkConnection.ConnectionType |
setNetworkConnection(org.openqa.selenium.mobile.NetworkConnection.ConnectionType type) |
builder, 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, startSession, switchTo, toString
public 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)
@Deprecated public ChromeDriver(org.openqa.selenium.Capabilities capabilities)
ChromeDriver(ChromeOptions)
instead.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.@Deprecated public ChromeDriver(ChromeDriverService service, org.openqa.selenium.Capabilities capabilities)
ChromeDriver(ChromeDriverService, ChromeOptions)
instead.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(org.openqa.selenium.remote.FileDetector detector)
setFileDetector
in class org.openqa.selenium.remote.RemoteWebDriver
public org.openqa.selenium.html5.LocalStorage getLocalStorage()
getLocalStorage
in interface org.openqa.selenium.html5.WebStorage
public org.openqa.selenium.html5.SessionStorage getSessionStorage()
getSessionStorage
in interface org.openqa.selenium.html5.WebStorage
public org.openqa.selenium.html5.Location location()
location
in interface org.openqa.selenium.html5.LocationContext
public void setLocation(org.openqa.selenium.html5.Location location)
setLocation
in interface org.openqa.selenium.html5.LocationContext
public org.openqa.selenium.interactions.TouchScreen getTouch()
getTouch
in interface org.openqa.selenium.interactions.HasTouchScreen
public org.openqa.selenium.mobile.NetworkConnection.ConnectionType getNetworkConnection()
getNetworkConnection
in interface org.openqa.selenium.mobile.NetworkConnection
public org.openqa.selenium.mobile.NetworkConnection.ConnectionType setNetworkConnection(org.openqa.selenium.mobile.NetworkConnection.ConnectionType type)
setNetworkConnection
in interface org.openqa.selenium.mobile.NetworkConnection
public void launchApp(java.lang.String id)
id
- chrome app id