Skip to content
Advertisement

Android: checking a button is enabled

I am having problems testing my app. I created an espresso test which is supposed to fail, since whenever I launch my app in the emulator, I get the expected behavior. There is my test:

 onView(withText("wrong answer")).perform(click());
 onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));

When launching the test, nothing is reported, whereas nextQuestionButton should not be enabled upon clicking the radioButton whose text is “wrong answer”.

Advertisement

Answer

According to what I understand, you want it to work like this:

if nextQuestionButton IS enabled, then take following actions:

  • click on ‘wrong answer’,
  • check if nextQuestionButton changed stated to NOT enabled.

If this is so, the code should be like this:

onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));
onView(withText("wrong answer")).perform(click());
onView(withId(R.id.nextQuestionButton)).check(matches(not(isEnabled())));

Espresso allows you to use Hamcrest matchers in tests.

Hamcrest 1.3 Quick Reference.

Please check also this (if you haven’t done that already):

Espresso 2.1. Espresso Cheat Sheet Master [updated]

According to this fragment of your post:

When launching the test, nothing is reported, whereas nextQuestionButton should not be enabled upon clicking the radioButton whose text is “wrong answer”.

It means that you hadn’t set disabled your next question button, so Espresso passes this test.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement