Puff Puff - 2 months ago 23
Android Question

Espresso webView webkeys failures on the Android 8.0 simulator

I am running some test codes from Espresso Web

public void typeTextInInput_clickButton_SubmitsForm() {
// Lazily launch the Activity with a custom start Intent per test.

// 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)).
// Find the input element by ID.
.withElement(findElement(Locator.ID, "text_input"))

// Clear previous input and enter new text into the input element.

// Find the "Submit" button and simulate a click using JavaScript.
.withElement(findElement(Locator.ID, "submitBtn"))

// Find the response element by ID, and verify that it contains the
// entered text.
.withElement(findElement(Locator.ID, "response"))
.check(webMatches(getText(), containsString(MACCHIATO)));

It worked on the 7.1.1 simulator but not on the 8.0
I got the error message as

Caused by: java.lang.RuntimeException: Error in evaluationEvaluation: status: 13 value: {message=Cannot set the selection end} hasMessage: true message: Cannot set the selection end

if I change the code

.perform(DriverAtoms.webKeys(MACCHIATO)) => perform(webClick())

Then it works. So I guess it can find the element just not perform the action.
Is there anything I need to change in my code?

Wae Wae
Answer Source

I am facing the same issue. Please refer to this post in Cant add text to webview text field with Espresso

My code works for Android 7.1.1 simulator but it fails on the Android 8.0 simulator as well. I got it resolved by doing the following in the build.gradle.

1) Upgrade espresso library. I was on:

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'

Now it to:

androidTestCompile 'com.android.support.test:runner:1.0.0'
androidTestCompile 'com.android.support.test:rules:1.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'

2) After doing that, your app might not build. It might say it cannot find the test:runner:1.0.0 In that case, you need to add

repositories {
    maven { url "https://maven.google.com" }

3) The following issue you might need to resolve is that it might complain about "versions for app (2x.x.x) and test app (2x.x.x) differ" So I add the following in the gradle.

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:2x.x.x'

4) Additional, you might need to make sure you have the Runner added.

defaultConfig {