简体   繁体   中英

Espresso test on Android getting wrong view hierarchy on logs while working properly on device

I wrote simple example test for Activity witch tests that when i click button - new fragment opens (LoginFragment). There is test class code:

@RunWith(AndroidJUnit4::class)
@LargeTest
class MainActivityTest {

    @get:Rule
    var activityScenarioRule = activityScenarioRule<MainActivity>()


    @Test
    fun test_clickLogInButton_openLoginFragment() {
        onView(withId(R.id.btnEnter)).perform(click())

        onView(withId(R.id.loginFragmentLayout)).check(matches(isDisplayed()))
    }
}

This test worked correctly on one computer, but now I transferred it to another and the hierarchy of views in the log began to display incorrectly. Log:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: view.getId() is <2131362001/com.example.android:id/btnEnter>

Meanwhile, I see how the given "button press" command is actually used on the emulator and new fragment getting launched. Thus, I assume that the test still works, but the display of the logs, and as a result, the test results, is broken.

My dependencies. I tried a lot of variants and right now i have following:

    // Testing
    testImplementation 'junit:junit:4.13.2'
    testImplementation "org.json:json:20180813"
    testImplementation 'org.mockito:mockito-core:3.12.4'
    testImplementation 'org.powermock:powermock-api-mockito2:2.0.9'
    testImplementation 'org.powermock:powermock-core:2.0.9'
    testImplementation 'org.powermock:powermock-module-junit4-rule-agent:2.0.2'
    testImplementation 'org.powermock:powermock-module-junit4-rule:2.0.2'
    testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
    testImplementation "androidx.room:room-testing:$room_version"
    //UI tests
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
    androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
    debugImplementation "androidx.fragment:fragment-testing:1.4.0"
    androidTestImplementation "androidx.fragment:fragment-ktx:1.4.0"
    androidTestImplementation "androidx.test:core:1.4.0"
    androidTestImplementation "androidx.test:rules:1.4.0"
    androidTestImplementation "androidx.test:runner:1.4.0"
    androidTestImplementation "androidx.navigation:navigation-testing:2.3.5"

What i was trying to do:

  • Clear cache and restart
  • Delete apk from output folder
  • Test on multiple devices (real and emulators)
  • Test on debug mode (code working properly, but logs and results of the tests are the same)

On my androidTest manifest i have following:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.presentation.activities"
    android:versionCode="1"
    android:versionName="1.0">

    <application>
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

Any suggestions how to fix it?

As i can see, the problem was on UI-testing itselfe. I managed to fix it via adding try-catch logic to first onView action in tests. This is small fun i wrote for it:

    private fun performActionCount(
        action: () -> Unit,
        maxRepeatTimes: Int = -1
    ) {
        var success = false
        var counter = if (maxRepeatTimes == -1) Int.MIN_VALUE else 0
        while (!success && counter < maxRepeatTimes) {
            success = try {
                counter++
                action()
                true
            } catch (e: NoMatchingViewException) {
                false
            }
        }
    }

It takes the lambda of action that you need to do and trying to execute it several times. Example of how i use it:

        performActionCount(
            action = {
                onView(withId(R.id.btnEnter)).perform(click())
            },
            maxRepeatTimes = 20
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM