Skip to content

Most visited

Recently visited

navigation

Espresso Web

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:

  1. Open your app’s build.gradle file. This is usually not the top-level build.gradle file but app/build.gradle.
  2. Add the following line inside dependencies:

    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
    
  3. 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:

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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.