0

I have an AndroidJUnit4 testcase, within this test case I'm using the following code to verify that the snackbar is displayed and contains the right text:

onView(withId(android.support.design.R.id.snackbar_text)).check(matches(isDisplayed()));
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.msg_error_unknown_user)))
                    .check(matches(isDisplayed()));

When I run this test on an emulator/device with API level 22, the tests run correct. When I switch to API level 17, the test fails with this exception:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.mydomain.myappname:id/snackbar_text

Note that the application behaves in the same way on both API level; the snackbar is being displayed correctly.

Why is in my testcase the snackbar not retrieved on API level 17 and how can I fix this?

Peter
  • 10,910
  • 3
  • 35
  • 68

1 Answers1

0

Well, Espresso is looking into your your xml file and searching for snackbar_text, but it won't find it as it has android system id, where Espressodon't have access. If you still want to check it, please learn about another Google instrumentation testing framework called uiautomator.

Here you would find how it work along with Espresso: http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

Also please take your time to see how I've already tested Snackbar in a code below:

@Test
public void checkIfSnackBarIsProperlyDisplayed() throws InterruptedException {
        onView(withId(R.id.fab)).perform(click());
        onView(withText(R.string.function_not_available)).check(matches(isDisplayed()));

    }

    @Test
    public void clickOnFloatingActionButtonToShowSnackBar() {
        onView(withId(R.id.fab)).perform(click());
        onView(withId(R.id.snackbar_text))
                .check(matches(withText(R.string.function_not_available)));

    }

    @Test
    public void swipeRightToDismissDisplayedCommunique() {
        onView(withId(R.id.fab)).perform(click());
        onView(withId(R.id.snackbar_text))
                .perform(swipeRight())
                .check(matches(not(isCompletelyDisplayed())));

    }
}

Hope you find it useful.

piotrek1543
  • 19,130
  • 7
  • 81
  • 94