Question

I am trying out the new Android testing library Espresso. When I try:

onView(withId(R.id.gettingStarted))

The test runs fine. But when I try:

onView(withId(R.id.gettingStarted)).check(matches(isDisplayed()));

I get a .NoMatchingViewException: No views in hierarchy found matching: with id: is <2131296645>

Has anyone seen anything like this before? My initial reflex is that this is just a bug because Espresso is only on version 1.0. I am using Android Studio and followed the set up directions exactly.

Was it helpful?

Solution

This is expected behavior.

onView(withId(R.id.gettingStarted)) by itself doesn't do anything. When you invoke the perform method, Espresso runs the matcher provided inside the onView method against the current view hierarchy - if no matching view is found, the exception is thrown.

See the start guide for more details: https://developer.android.com/training/testing/espresso/index.html

OTHER TIPS

There are many legitimate cases when you cannot determine R.id at test development time. For example, the specific view may not have an R.id or the R.id is not unique. This can make normal instrumentation tests brittle and complicated to write because the normal way to access the view (with findViewById()) does not work. Thus, you may need to access private members of the Activity or Fragment holding the view or find a container with a known R.id and navigate to its content for the particular view.

For more info : https://code.google.com/p/android-test-kit/wiki/EspressoStartGuide

This means that no view for this Id is found in the complete hierarchy of the UI Tree, irrespective of whether the view is visible or invisible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top