public class AndroidWebDriver extends java.lang.Object implements WebDriver, SearchContext, JavascriptExecutor, TakesScreenshot, Rotatable, BrowserConnection, HasTouchScreen, WebStorage, LocationContext, android.location.LocationListener, ApplicationCache
WebDriver.ImeHandler, WebDriver.Navigation, WebDriver.Options, WebDriver.TargetLocator, WebDriver.Timeouts, WebDriver.Window
Constructor and Description |
---|
AndroidWebDriver(android.app.Activity activity)
Use this contructor to use WebDriver with a WebView that has the same settings as
the Android browser.
|
AndroidWebDriver(android.app.Activity activity,
ViewFactory viewFactory,
ViewClientWrapper viewClient,
ChromeClientWrapper chromeClient)
Use this constructor to use WebDriver with a custom view.
|
AndroidWebDriver(android.app.Activity activity,
ViewFactory viewFactory,
ViewClientWrapper viewClient,
ChromeClientWrapper chromeClient,
android.view.View.OnFocusChangeListener focusListener)
Use this constructor to use WebDriver with a custom view and a custom
View.OnFocusChangeListener for that view..
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the current window, quitting the browser if it's the last window currently open.
|
java.lang.Object |
executeAsyncScript(java.lang.String script,
java.lang.Object... args)
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or
window.
|
java.lang.Object |
executeScript(java.lang.String script,
java.lang.Object... args)
Executes JavaScript in the context of the currently selected frame or window.
|
WebElement |
findElement(By by)
Find the first
WebElement using the given method. |
java.util.List<WebElement> |
findElements(By by)
Find all elements within the current page using the given mechanism.
|
void |
get(java.lang.String url)
Load a new web page in the current browser window.
|
boolean |
getAcceptSslCerts() |
android.app.Activity |
getActivity() |
java.lang.String |
getCurrentUrl()
Get a string representing the current URL that the browser is looking at.
|
LocalStorage |
getLocalStorage() |
ScreenOrientation |
getOrientation() |
java.lang.String |
getPageSource()
Get the source of the last loaded page.
|
<X> X |
getScreenshotAs(OutputType<X> target)
Capture the screenshot and store it in the specified location.
|
SessionStorage |
getSessionStorage() |
AppCacheStatus |
getStatus()
The current state of the application cache.
|
java.lang.String |
getTitle()
The title of the current page.
|
TouchScreen |
getTouch() |
java.lang.Object |
getView() |
android.webkit.WebView |
getWebView() |
java.lang.String |
getWindowHandle()
Return an opaque handle to this window that uniquely identifies it within this driver instance.
|
java.util.Set<java.lang.String> |
getWindowHandles()
Return a set of window handles which can be used to iterate over all open windows of this
webdriver instance by passing them to
#switchTo().window(String) |
boolean |
isJavascriptEnabled() |
boolean |
isOnline() |
Location |
location()
Gets the physical location of the browser.
|
WebDriver.Options |
manage()
Gets the Option interface
|
WebDriver.Navigation |
navigate()
An abstraction allowing the driver to access the browser's history and to navigate to a given
URL.
|
void |
onLocationChanged(android.location.Location location) |
void |
onProviderDisabled(java.lang.String s) |
void |
onProviderEnabled(java.lang.String s) |
void |
onStatusChanged(java.lang.String s,
int i,
android.os.Bundle bundle) |
protected java.lang.Object |
processJsonObject(java.lang.Object res) |
void |
quit()
Quits this driver, closing every associated window.
|
void |
rotate(ScreenOrientation orientation)
Changes the orientation of the browser window.
|
void |
setAcceptSslCerts(boolean accept) |
void |
setLocation(Location loc)
Sets the physical location.
|
void |
setOnline(boolean online)
Changes the application online state.
|
void |
setProxy(java.lang.String host,
int port) |
WebDriver.TargetLocator |
switchTo()
Send future commands to a different frame or window.
|
public AndroidWebDriver(android.app.Activity activity)
activity
- the activity context where the WebView will be created.public AndroidWebDriver(android.app.Activity activity, ViewFactory viewFactory, ViewClientWrapper viewClient, ChromeClientWrapper chromeClient)
activity
- the activity context where the view will be displayed.viewFactory
- a implementation of the ViewFactory interface. WebDriver will
use this creation mechanism to create views when needed (e.g. when clicking on a link
that opens a new window).viewClient
- the ViewClientWrapper used by the custom WebView.chromeClient
- the ChromeClientWrapper used by the custom WebView.public AndroidWebDriver(android.app.Activity activity, ViewFactory viewFactory, ViewClientWrapper viewClient, ChromeClientWrapper chromeClient, android.view.View.OnFocusChangeListener focusListener)
activity
- the activity context where the view will be displayed.viewFactory
- a implementation of the ViewFactory interface. WebDriver will
use this creation mechanism to create views when needed (e.g. when clicking on a link
that opens a new window).viewClient
- the ViewClientWrapper used by the custom WebView.chromeClient
- the ChromeClientWrapper used by the custom WebView.focusListener
- the listener used by the view that will be created by the viewFactory.public void setAcceptSslCerts(boolean accept)
public boolean getAcceptSslCerts()
public android.webkit.WebView getWebView()
public java.lang.Object getView()
public android.app.Activity getActivity()
public java.lang.String getCurrentUrl()
WebDriver
getCurrentUrl
in interface WebDriver
public java.lang.String getTitle()
WebDriver
public void get(java.lang.String url)
WebDriver
WebDriver.Navigation.to(String)
.public java.lang.String getPageSource()
WebDriver
getPageSource
in interface WebDriver
public void close()
WebDriver
public void quit()
WebDriver
public WebElement findElement(By by)
WebDriver
WebElement
using the given method.
This method is affected by the 'implicit wait' times in force at the time of execution.
The findElement(..) invocation will return a matching row, or try again repeatedly until
the configured timeout is reached.
findElement should not be used to look for non-present elements, use WebDriver.findElements(By)
and assert zero length response instead.findElement
in interface SearchContext
findElement
in interface WebDriver
by
- The locating mechanismBy
,
WebDriver.Timeouts
public java.util.List<WebElement> findElements(By by)
WebDriver
findElements
in interface SearchContext
findElements
in interface WebDriver
by
- The locating mechanism to useWebElement
s, or an empty list if nothing matchesBy
,
WebDriver.Timeouts
public AppCacheStatus getStatus()
ApplicationCache
getStatus
in interface ApplicationCache
AppCacheStatus
public java.util.Set<java.lang.String> getWindowHandles()
WebDriver
#switchTo().window(String)
getWindowHandles
in interface WebDriver
public java.lang.String getWindowHandle()
WebDriver
getWindowHandle
in interface WebDriver
public WebDriver.TargetLocator switchTo()
WebDriver
switchTo
in interface WebDriver
WebDriver.TargetLocator
public LocalStorage getLocalStorage()
getLocalStorage
in interface WebStorage
public SessionStorage getSessionStorage()
getSessionStorage
in interface WebStorage
public WebDriver.Navigation navigate()
WebDriver
navigate
in interface WebDriver
WebDriver.Navigation
that allows the selection of what to
do nextpublic boolean isJavascriptEnabled()
public java.lang.Object executeScript(java.lang.String script, java.lang.Object... args)
JavascriptExecutor
Within the script, use document
to refer to the current document. Note that local
variables will not be available once the script has finished executing, though global variables
will persist.
If the script has a return value (i.e. if the script contains a return
statement),
then the following steps will be taken:
Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"
executeScript
in interface JavascriptExecutor
script
- The JavaScript to executeargs
- The arguments to the script. May be emptypublic java.lang.Object executeAsyncScript(java.lang.String script, java.lang.Object... args)
JavascriptExecutor
synchronous JavaScript
,
scripts executed with this method must explicitly signal they are finished by invoking the
provided callback. This callback is always injected into the executed function as the last
argument.
The first argument passed to the callback function will be used as the script's result. This value will be handled as follows:
Example #1: Performing a sleep in the browser under test.
long start = System.currentTimeMillis();
((JavascriptExecutor) driver).executeAsyncScript(
"window.setTimeout(arguments[arguments.length - 1], 500);");
System.out.println(
"Elapsed time: " + System.currentTimeMillis() - start);
Example #2: Synchronizing a test with an AJAX application:
WebElement composeButton = driver.findElement(By.id("compose-button"));
composeButton.click();
((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"mailClient.getComposeWindowWidget().onload(callback);");
driver.switchTo().frame("composeWidget");
driver.findElement(By.id("to")).sendKeys("[email protected]");
Example #3: Injecting a XMLHttpRequest and waiting for the result:
Object response = ((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"var xhr = new XMLHttpRequest();" +
"xhr.open('GET', '/resource/data.json', true);" +
"xhr.onreadystatechange = function() {" +
" if (xhr.readyState == 4) {" +
" callback(xhr.responseText);" +
" }" +
"}" +
"xhr.send();");
JSONObject json = new JSONObject((String) response);
assertEquals("cheese", json.getString("food"));
Script arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" variable.
executeAsyncScript
in interface JavascriptExecutor
script
- The JavaScript to execute.args
- The arguments to the script. May be empty.protected java.lang.Object processJsonObject(java.lang.Object res) throws org.json.JSONException
org.json.JSONException
public void setProxy(java.lang.String host, int port)
public WebDriver.Options manage()
WebDriver
manage
in interface WebDriver
WebDriver.Options
public Location location()
LocationContext
location
in interface LocationContext
Location
containing the location information. Returns null if the location is
not availablepublic void setLocation(Location loc)
LocationContext
setLocation
in interface LocationContext
loc
- A Location
containing the new location informationpublic void onLocationChanged(android.location.Location location)
onLocationChanged
in interface android.location.LocationListener
public void onStatusChanged(java.lang.String s, int i, android.os.Bundle bundle)
onStatusChanged
in interface android.location.LocationListener
public void onProviderEnabled(java.lang.String s)
onProviderEnabled
in interface android.location.LocationListener
public void onProviderDisabled(java.lang.String s)
onProviderDisabled
in interface android.location.LocationListener
public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException
TakesScreenshot
For WebDriver extending TakesScreenshot, this makes a best effort depending on the browser to return the following in order of preference:
For WebElement extending TakesScreenshot, this makes a best effort depending on the browser to return the following in order of preference: - The entire content of the HTML element - The visisble portion of the HTML element
getScreenshotAs
in interface TakesScreenshot
X
- Return type for getScreenshotAs.target
- target type, @see OutputTypeWebDriverException
- on failure.public ScreenOrientation getOrientation()
getOrientation
in interface Rotatable
public void rotate(ScreenOrientation orientation)
Rotatable
public boolean isOnline()
isOnline
in interface BrowserConnection
public void setOnline(boolean online) throws WebDriverException
BrowserConnection
setOnline
in interface BrowserConnection
online
- A boolean representing the stateWebDriverException
public TouchScreen getTouch()
getTouch
in interface HasTouchScreen