Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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".

like image 373
Loïs Talagrand Avatar asked Oct 01 '15 05:10

Loïs Talagrand


People also ask

How do you check if a button is enabled or not in android?

ImageButton myButton = (ImageButton) findViewById(R. id. epic_button); if (myButton. isEnabled()){ //then the button is enabled. }

How do I check if a button is enabled or disabled in Appium?

How to check whether the text box/field is Editable or not ? When you identify the object using uiautomatorviewer or appium inspector, in properties it will show you whether button is enabled and editable properties for textbox. You can use element. getAttribute(“enabled”), it will return you true or false.

How do you turn off click on Android?

see this: developer.android.com/reference/android/view/View.html. You can also use Next. setEnabled(false) for the required functionality.


1 Answers

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.

like image 92
piotrek1543 Avatar answered Sep 27 '22 18:09

piotrek1543