In this document
Espresso-web is an entry point to work with WebView objects on Android. It
uses Atoms from the popular WebDriver API to introspect into and control the
behavior of a WebView object.
Similar to onData(), WebView interactions are actually composed of several
View Atoms. An Atom can be seen as a ViewAction, a self-contained unit which
performs an action in your UI. However, they need to be properly orchestrated
and are quite verbose. Web and WebInteraction wrap this boilerplate and give
an Espresso-like feel to interacting with WebView objects.
WebView interactions use a combination of the Java programming language and
JavaScript to do their work. Because there is no chance of introducing race
conditions by exposing data from the JavaScript environment—everything
Espresso sees on the Java-based side is an isolated copy—returning data
from WebInteraction objects is fully supported.
Packages
To include Espresso-Web in your project, complete the following steps:
- Open your app’s
build.gradlefile. This is usually not the top-levelbuild.gradlefile butapp/build.gradle. Add the following line inside dependencies:
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
Espresso-Web is only compatible with Espresso 2.2 or higher and the Android Testing Support Library 0.3 or higher, so make sure you update those lines as well:
androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
Common web interactions
Common interactions with WebInteraction objects include the following:
-
withElement(ElementReference)will supply theElementReferenceto the Atom. Example:onWebView().withElement(findElement(Locator.ID, "teacher"))
-
withContextualElement(Atom<ElementReference>)will supply theElementReferenceto the Atom. Example:onWebView() .withElement(findElement(Locator.ID, "teacher")) .withContextualElement(findElement(Locator.ID, "person_name")) -
check(WebAssertion)will evaluate an assertion. Example:onWebView() .withElement(findElement(Locator.ID, "teacher")) .withContextualElement(findElement(Locator.ID, "person_name")) .check(webMatches(getText(), containsString("Socrates"))); -
perform(Atom)will execute the provided Atom within the current context. Example:onWebView() .withElement(findElement(Locator.ID, "teacher")) .perform(webClick()); -
reset()is necessary when a prior action, such as a click, introduces a navigation change that invalidates theElementReferenceandWindowReferencepointers.
WebView example
JavaScript has to be enabled for Espresso web to control the WebView. You can
force it by overriding afterActivityLaunched() in the ActivityTestRule:
@Rule
public ActivityTestRule<WebViewActivity> mActivityRule =
new ActivityTestRule<WebViewActivity>(WebViewActivity.class,
false, false) {
@Override
protected void afterActivityLaunched() {
onWebView().forceJavascriptEnabled();
}
}
@Test
public void typeTextInInput_clickButton_SubmitsForm() {
// Lazily launch the Activity with a custom start Intent per test.
mActivityRule.launchActivity(withWebFormIntent());
// Selects the WebView in your layout. If you have multiple WebView objects,
// you can also use a matcher to select a given WebView,
// onWebView(withId(R.id.web_view)).
onWebView()
// Find the input element by ID.
.withElement(findElement(Locator.ID, "text_input"))
// Clear previous input.
.perform(clearElement())
// Enter text into the input element.
.perform(DriverAtoms.webKeys(MACCHIATO))
// Find the submit button.
.withElement(findElement(Locator.ID, "submitBtn"))
// Simulate a click using JavaScript.
.perform(webClick())
// Find the response element by ID.
.withElement(findElement(Locator.ID, "response"))
// Verify that the response page contains the entered text.
.check(webMatches(getText(), containsString(MACCHIATO)));
}
See the Espresso Web sample on GitHub.