Espresso webView webkeys failures on the Android 8.0 simulator

两盒软妹~` 提交于 2019-12-13 17:13:56

问题


I am running some test codes from Espresso Web

@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 and enter new text into the input element.
        .perform(clearElement())
        .perform(DriverAtoms.webKeys(MACCHIATO))

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

        // 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

element.
        .perform(clearElement())
        .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?


回答1:


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'
androidTestCompile('com.android.support.test.espresso:espresso-contrib: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'
androidTestCompile('com.android.support.test.espresso:espresso-contrib: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 {
        "android.support.test.runner.AndroidJUnitRunner"
}


来源:https://stackoverflow.com/questions/45822957/espresso-webview-webkeys-failures-on-the-android-8-0-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!