Nxt3
Nxt3

Reputation: 2091

Determine if a button isn't clickable with Espresso

Doing

onView(withId(R.id.login_button)).check(matches(isClickable()));

is useful for verifying if a button is clickable. How would I verify that a button isn't clickable?

Edit: Like I said, it only tells me if it isClickable. I'm looking for a way to verify it IS NOT clickable.

Upvotes: 7

Views: 4499

Answers (1)

Matt C
Matt C

Reputation: 4555

2023 Update

isNotClickable()

onView(withId(R.id.button)).check(matches(isNotClickable()))

Source

public static Matcher<View> isNotClickable() {
    return new IsClickableMatcher(false);
}

Edit - Original Solution

Use not(), it returns a matcher that negates the matcher passed to it.

You're looking for: not( isClickable() )

onView(withId(R.id.login_button)).check(matches( not(isClickable()) ));

Breaking down the code

onView() returns the UI object we are interested in (login_button in this case), and we call .check on this object to see if our assertion holds.

The matches function returns an assertion that we build by passing in a Matcher. The isClickable() function returns a Matcher that we can give back to matches().

not() is a Matcher which returns a Matcher negating the logic of the one given to it.

So this code is checking if the assertion not( isClickable() ) is true on the login_button View.

Upvotes: 10

Related Questions