ViewMatchers


class ViewMatchers


A collection of hamcrest matchers that match Views.

Summary

Nested types

Enumerates the possible list of values for getVisibility.

Public functions

java-static Unit
<T> assertThat(actual: T!, matcher: Matcher<T!>!)

A replacement for MatcherAssert.assertThat that renders View objects nicely.

java-static Unit
<T> assertThat(message: String!, actual: T!, matcher: Matcher<T!>!)

A replacement for MatcherAssert.assertThat that renders View objects nicely.

java-static Matcher<View!>!

Returns a matcher that matches Views currently do not have focus.

java-static Matcher<View!>!
hasBackground(drawableId: Int)

Returns a matcher that matches android.view.View based on background resource Bitmap.

java-static Matcher<View!>!
hasChildCount(childCount: Int)

Matches a ViewGroup if it has exactly the specified number of children.

java-static Matcher<View!>!

Returns an Matcher that matches Views with any content description.

java-static Matcher<View!>!
hasDescendant(descendantMatcher: Matcher<View!>!)

Returns a matcher that matches Views based on the presence of a descendant in its view hierarchy.

java-static Matcher<View!>!
hasErrorText(expectedError: String!)

Returns a matcher that matches EditText based on edit text error string value.

java-static Matcher<View!>!
hasErrorText(stringMatcher: Matcher<String!>!)

Returns a matcher that matches EditText based on edit text error string value.

java-static Matcher<View!>!

Returns a matcher that matches Views currently have focus.

java-static Matcher<View!>!
hasImeAction(imeAction: Int)

Returns a matcher that matches views that support input methods (e.g.

java-static Matcher<View!>!
hasImeAction(imeActionMatcher: Matcher<Int!>!)

Returns a matcher that matches views that support input methods (e.g.

java-static Matcher<View!>!

Returns a matcher that matches TextViews that have links.

java-static Matcher<View!>!
hasMinimumChildCount(minChildCount: Int)

Matches a ViewGroup if it has at least the specified number of children.

java-static Matcher<View!>!
hasSibling(siblingMatcher: Matcher<View!>!)

Returns an Matcher that matches Views based on their siblings.

java-static Matcher<View!>!
hasTextColor(colorResId: Int)

Returns a matcher that matches android.widget.TextView based on it's color.

java-static Matcher<View!>!

Matches Views based on instance or subclass of the provided class.

java-static Matcher<View!>!

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is in checked state.

java-static Matcher<View!>!

Returns a matcher that matches Views that are clickable.

java-static Matcher<View!>!

Returns a matcher which only accepts a view whose height and width fit perfectly within the currently displayed region of this view.

java-static Matcher<View!>!
isDescendantOfA(ancestorMatcher: Matcher<View!>!)

Returns a matcher that matches Views based on the given ancestor type.

java-static Matcher<View!>!

Returns a matcher that matches Views that are currently displayed on the screen to the user.

java-static Matcher<View!>!
isDisplayingAtLeast(areaPercentage: Int)

Returns a matcher which accepts a view so long as a given percentage of that view's area is not obscured by any parent view and is thus visible to the user.

java-static Matcher<View!>!

Returns a matcher that matches Views that are enabled.

java-static Matcher<View!>!

Returns a matcher that matches Views that are focusable.

java-static Matcher<View!>!

Returns a matcher that matches Views that are focused.

java-static Matcher<View!>!

Returns a matcher that matches WebView if they are evaluating JavaScript.

java-static Matcher<View!>!

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is not in checked state.

java-static Matcher<View!>!

Returns a matcher that matches Views that are not clickable.

java-static Matcher<View!>!

Returns a matcher that matches Views that are not enabled.

java-static Matcher<View!>!

Returns a matcher that matches Views that are not focusable.

java-static Matcher<View!>!

Returns a matcher that matches Views that are not focused.

java-static Matcher<View!>!

Returns a matcher that matches Views that are not selected.

java-static Matcher<View!>!

Returns a matcher that matches root View.

java-static Matcher<View!>!

Returns a matcher that matches Views that are selected.

java-static Matcher<View!>!

Returns a matcher that matches views that support input methods.

java-static Matcher<View!>!
withAlpha(alpha: Float)

Matches Views with the specified alpha value.

java-static Matcher<View!>!
withChild(childMatcher: Matcher<View!>!)

Returns true only if the view's child is accepted by the provided matcher.

java-static Matcher<View!>!
withClassName(classNameMatcher: Matcher<String!>!)

Returns a matcher that matches Views with class name matching the given matcher.

java-static Matcher<View!>!
withContentDescription(charSequenceMatcher: Matcher<CharSequence!>!)

Returns an Matcher that matches Views based on content description property value.

java-static Matcher<View!>!

Returns a Matcher that matches Views based on content description property value.

java-static Matcher<View!>!

Returns an Matcher that matches Views based on content description's text value.

java-static Matcher<View!>!

Returns a matcher that matches Views that have "effective" visibility set to the given value.

java-static Matcher<View!>!
withHint(hintText: String!)

Returns a matcher that matches TextView based on it's hint property value.

java-static Matcher<View!>!
withHint(resourceId: Int)

Returns a matcher that matches a descendant of TextView that is displaying the hint associated with the given resource id.

java-static Matcher<View!>!
withHint(stringMatcher: Matcher<String!>!)

Returns a matcher that matches TextViews based on hint property value.

java-static Matcher<View!>!
withId(id: Int)

A matcher that matches Views based on its resource id.

java-static Matcher<View!>!
withId(integerMatcher: Matcher<Int!>!)

Returns a matcher that matches Views based on resource ids.

java-static Matcher<View!>!
withInputType(inputType: Int)

Returns a matcher that matches android.text.InputType.

java-static Matcher<View!>!
withParent(parentMatcher: Matcher<View!>!)

A matcher that accepts a view if and only if the view's parent is accepted by the provided matcher.

java-static Matcher<View!>!

Returns a matcher that matches the child index inside the ViewParent.

java-static Matcher<View!>!

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

java-static Matcher<View!>!
withResourceName(stringMatcher: Matcher<String!>!)

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

java-static Matcher<View!>!
withSpinnerText(resourceId: Int)

Returns a matcher that matches a descendant of Spinner that is displaying the string of the selected item associated with the given resource id.

java-static Matcher<View!>!
withSpinnerText(stringMatcher: Matcher<String!>!)

Returns a matcher that matches Spinner's based on toString value of the selected item.

java-static Matcher<View!>!

Returns a matcher that matches Spinner based on it's selected item's toString value.

java-static Matcher<View!>!
withSubstring(substring: String!)

Returns a matcher that matches TextView that contains the specific substring.

java-static Matcher<View!>!

Returns a matcher that matches View based on tag keys.

java-static Matcher<View!>!
withTagKey(key: Int, objectMatcher: Matcher<Any!>!)

Returns a matcher that matches Views based on tag keys.

java-static Matcher<View!>!
withTagValue(tagValueMatcher: Matcher<Any!>!)

Returns a matcher that matches Views based on tag property values.

java-static Matcher<View!>!
withText(resourceId: Int)

Returns a matcher that matches a descendant of TextView that is displaying the string associated with the given resource id.

java-static Matcher<View!>!
withText(stringMatcher: Matcher<String!>!)

Returns a matcher that matches TextViews based on text property value.

java-static Matcher<View!>!
withText(text: String!)

Returns a matcher that matches TextView based on its text property value.

Public functions

assertThat

java-static fun <T> assertThat(actual: T!, matcher: Matcher<T!>!): Unit

A replacement for MatcherAssert.assertThat that renders View objects nicely.

Parameters
actual: T!

the actual value.

matcher: Matcher<T!>!

a matcher that accepts or rejects actual.

assertThat

java-static fun <T> assertThat(message: String!, actual: T!, matcher: Matcher<T!>!): Unit

A replacement for MatcherAssert.assertThat that renders View objects nicely.

Parameters
message: String!

the message to display.

actual: T!

the actual value.

matcher: Matcher<T!>!

a matcher that accepts or rejects actual.

doesNotHaveFocus

java-static fun doesNotHaveFocus(): Matcher<View!>!

Returns a matcher that matches Views currently do not have focus.

hasBackground

java-static fun hasBackground(drawableId: Int): Matcher<View!>!

Returns a matcher that matches android.view.View based on background resource Bitmap.

See also
sameAs

hasChildCount

java-static fun hasChildCount(childCount: Int): Matcher<View!>!

Matches a ViewGroup if it has exactly the specified number of children.

hasContentDescription

java-static fun hasContentDescription(): Matcher<View!>!

Returns an Matcher that matches Views with any content description.

hasDescendant

java-static fun hasDescendant(descendantMatcher: Matcher<View!>!): Matcher<View!>!

Returns a matcher that matches Views based on the presence of a descendant in its view hierarchy.

Parameters
descendantMatcher: Matcher<View!>!

the type of the descendant to match on

hasErrorText

java-static fun hasErrorText(expectedError: String!): Matcher<View!>!

Returns a matcher that matches EditText based on edit text error string value.

Note: Sugar for hasErrorText(is("string")).

hasErrorText

java-static fun hasErrorText(stringMatcher: Matcher<String!>!): Matcher<View!>!

Returns a matcher that matches EditText based on edit text error string value.

Note: View's error property can be null, to match against it use hasErrorText(nullValue(String.class)

hasFocus

java-static fun hasFocus(): Matcher<View!>!

Returns a matcher that matches Views currently have focus.

hasImeAction

java-static fun hasImeAction(imeAction: Int): Matcher<View!>!

Returns a matcher that matches views that support input methods (e.g. EditText) and have the specified IME action set in its EditorInfo.

Parameters
imeAction: Int

the IME action to match

hasImeAction

java-static fun hasImeAction(imeActionMatcher: Matcher<Int!>!): Matcher<View!>!

Returns a matcher that matches views that support input methods (e.g. EditText) and have the specified IME action set in its EditorInfo.

Parameters
imeActionMatcher: Matcher<Int!>!

a matcher for the IME action

hasLinks

java-static fun hasLinks(): Matcher<View!>!

Returns a matcher that matches TextViews that have links.

hasMinimumChildCount

java-static fun hasMinimumChildCount(minChildCount: Int): Matcher<View!>!

Matches a ViewGroup if it has at least the specified number of children.

hasSibling

java-static fun hasSibling(siblingMatcher: Matcher<View!>!): Matcher<View!>!

Returns an Matcher that matches Views based on their siblings.

This may be particularly useful when a view cannot be uniquely selected on properties such as text or R.id. For example: a call button is repeated several times in a contacts layout and the only way to differentiate the call button view is by what appears next to it (e.g. the unique name of the contact).

Parameters
siblingMatcher: Matcher<View!>!

a Matcher for the sibling of the view.

hasTextColor

java-static fun hasTextColor(colorResId: Int): Matcher<View!>!

Returns a matcher that matches android.widget.TextView based on it's color.

isAssignableFrom

java-static fun isAssignableFrom(clazz: Class<View!>!): Matcher<View!>!

Matches Views based on instance or subclass of the provided class.

Some versions of Hamcrest make the generic typing of this a nightmare, so we have a special case for our users.

isChecked

java-static fun isChecked(): Matcher<View!>!

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is in checked state.

isClickable

java-static fun isClickable(): Matcher<View!>!

Returns a matcher that matches Views that are clickable.

isCompletelyDisplayed

java-static fun isCompletelyDisplayed(): Matcher<View!>!

Returns a matcher which only accepts a view whose height and width fit perfectly within the currently displayed region of this view.

There exist views (such as ScrollViews) whose height and width are larger then the physical device screen by design. Such views will never be completely displayed.

isDescendantOfA

java-static fun isDescendantOfA(ancestorMatcher: Matcher<View!>!): Matcher<View!>!

Returns a matcher that matches Views based on the given ancestor type.

Parameters
ancestorMatcher: Matcher<View!>!

the type of the ancestor to match on

isDisplayed

java-static fun isDisplayed(): Matcher<View!>!

Returns a matcher that matches Views that are currently displayed on the screen to the user.

Note: isDisplayed will select views that are partially displayed (eg: the full height/width of the view is greater than the height/width of the visible rectangle). If you wish to ensure the entire rectangle this view draws is displayed to the user use isCompletelyDisplayed.

isDisplayingAtLeast

java-static fun isDisplayingAtLeast(areaPercentage: Int): Matcher<View!>!

Returns a matcher which accepts a view so long as a given percentage of that view's area is not obscured by any parent view and is thus visible to the user.

Parameters
areaPercentage: Int

an integer ranging from (0, 100] indicating how much percent of the surface area of the view must be shown to the user to be accepted.

isEnabled

java-static fun isEnabled(): Matcher<View!>!

Returns a matcher that matches Views that are enabled.

isFocusable

java-static fun isFocusable(): Matcher<View!>!

Returns a matcher that matches Views that are focusable.

isFocused

java-static fun isFocused(): Matcher<View!>!

Returns a matcher that matches Views that are focused.

isJavascriptEnabled

java-static fun isJavascriptEnabled(): Matcher<View!>!

Returns a matcher that matches WebView if they are evaluating JavaScript.

isNotChecked

java-static fun isNotChecked(): Matcher<View!>!

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is not in checked state.

isNotClickable

java-static fun isNotClickable(): Matcher<View!>!

Returns a matcher that matches Views that are not clickable.

isNotEnabled

java-static fun isNotEnabled(): Matcher<View!>!

Returns a matcher that matches Views that are not enabled.

isNotFocusable

java-static fun isNotFocusable(): Matcher<View!>!

Returns a matcher that matches Views that are not focusable.

isNotFocused

java-static fun isNotFocused(): Matcher<View!>!

Returns a matcher that matches Views that are not focused.

isNotSelected

java-static fun isNotSelected(): Matcher<View!>!

Returns a matcher that matches Views that are not selected.

isRoot

java-static fun isRoot(): Matcher<View!>!

Returns a matcher that matches root View.

isSelected

java-static fun isSelected(): Matcher<View!>!

Returns a matcher that matches Views that are selected.

supportsInputMethods

java-static fun supportsInputMethods(): Matcher<View!>!

Returns a matcher that matches views that support input methods.

withAlpha

java-static fun withAlpha(alpha: Float): Matcher<View!>!

Matches Views with the specified alpha value.

withChild

java-static fun withChild(childMatcher: Matcher<View!>!): Matcher<View!>!

Returns true only if the view's child is accepted by the provided matcher.

Parameters
childMatcher: Matcher<View!>!

the matcher to apply on the child views.

withClassName

java-static fun withClassName(classNameMatcher: Matcher<String!>!): Matcher<View!>!

Returns a matcher that matches Views with class name matching the given matcher.

withContentDescription

java-static fun withContentDescription(charSequenceMatcher: Matcher<CharSequence!>!): Matcher<View!>!

Returns an Matcher that matches Views based on content description property value.

Parameters
charSequenceMatcher: Matcher<CharSequence!>!

a CharSequenceMatcher for the content description

withContentDescription

java-static fun withContentDescription(resourceId: Int): Matcher<View!>!

Returns a Matcher that matches Views based on content description property value.

Parameters
resourceId: Int

the resource id of the content description to match on.

withContentDescription

java-static fun withContentDescription(text: String!): Matcher<View!>!

Returns an Matcher that matches Views based on content description's text value.

Parameters
text: String!

the text to match on.

withEffectiveVisibility

java-static fun withEffectiveVisibility(visibility: ViewMatchers.Visibility!): Matcher<View!>!

Returns a matcher that matches Views that have "effective" visibility set to the given value.

Effective visibility takes into account not only the view's visibility value, but also that of its ancestors. In case of View.VISIBLE, this means that the view and all of its ancestors have visibility=VISIBLE. In case of GONE and INVISIBLE, it's the opposite - any GONE or INVISIBLE parent will make all of its children have their effective visibility.

Note: Contrary to what the name may imply, view visibility does not directly translate into whether the view is displayed on screen (use isDisplayed for that). For example, the view and all of its ancestors can have visibility=VISIBLE, but the view may need to be scrolled to in order to be actually visible to the user. Unless you're specifically targeting the visibility value with your test, use isDisplayed.

withHint

java-static fun withHint(hintText: String!): Matcher<View!>!

Returns a matcher that matches TextView based on it's hint property value.

Note: View's sugar for withHint(is("string")).

Parameters
hintText: String!

String with the hint text to match

withHint

java-static fun withHint(resourceId: Int): Matcher<View!>!

Returns a matcher that matches a descendant of TextView that is displaying the hint associated with the given resource id.

Parameters
resourceId: Int

the string resource the text view is expected to have as a hint.

withHint

java-static fun withHint(stringMatcher: Matcher<String!>!): Matcher<View!>!

Returns a matcher that matches TextViews based on hint property value.

Note: View's hint property can be null, to match against it use withHint(nullValue(String.class)

Parameters
stringMatcher: Matcher<String!>!

Matcher of String with text to match

withId

java-static fun withId(id: Int): Matcher<View!>!

A matcher that matches Views based on its resource id.

Same as withId(is(int)) but attempts to look up resource name of the given id and use a R.id.myView style description with describeTo. If resource lookup is unavailable, at the time describeTo is invoked, this will print out a simple "with id: %d". If the lookup for a given id fails, "with id: %d (resource name not found)" will be returned as the description.

Note: Android resource ids are not guaranteed to be unique. You may have to pair this matcher with another one to guarantee a unique view selection.

Parameters
id: Int

the resource id.

withId

java-static fun withId(integerMatcher: Matcher<Int!>!): Matcher<View!>!

Returns a matcher that matches Views based on resource ids. Note: Android resource ids are not guaranteed to be unique. You may have to pair this matcher with another one to guarantee a unique view selection.

Parameters
integerMatcher: Matcher<Int!>!

a Matcher for resource ids

withInputType

java-static fun withInputType(inputType: Int): Matcher<View!>!

Returns a matcher that matches android.text.InputType.

withParent

java-static fun withParent(parentMatcher: Matcher<View!>!): Matcher<View!>!

A matcher that accepts a view if and only if the view's parent is accepted by the provided matcher.

Parameters
parentMatcher: Matcher<View!>!

the matcher to apply on getParent.

withParentIndex

java-static fun withParentIndex(index: Int): Matcher<View!>!

Returns a matcher that matches the child index inside the ViewParent.

withResourceName

java-static fun withResourceName(name: String!): Matcher<View!>!

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

Parameters
name: String!

the resource id name

withResourceName

java-static fun withResourceName(stringMatcher: Matcher<String!>!): Matcher<View!>!

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

Parameters
stringMatcher: Matcher<String!>!

a Matcher for resource id names

withSpinnerText

java-static fun withSpinnerText(resourceId: Int): Matcher<View!>!

Returns a matcher that matches a descendant of Spinner that is displaying the string of the selected item associated with the given resource id.

Parameters
resourceId: Int

the resource id of the string resource the text view is expected to hold.

withSpinnerText

java-static fun withSpinnerText(stringMatcher: Matcher<String!>!): Matcher<View!>!

Returns a matcher that matches Spinner's based on toString value of the selected item.

Parameters
stringMatcher: Matcher<String!>!

Matcher of String with text to match.

withSpinnerText

java-static fun withSpinnerText(text: String!): Matcher<View!>!

Returns a matcher that matches Spinner based on it's selected item's toString value.

Note: Sugar for withSpinnerText(is("string")).

withSubstring

java-static fun withSubstring(substring: String!): Matcher<View!>!

Returns a matcher that matches TextView that contains the specific substring.

Note: View's sugar for withText(containsString("string")).

Parameters
substring: String!

String that is expected to be contained

withTagKey

java-static fun withTagKey(key: Int): Matcher<View!>!

Returns a matcher that matches View based on tag keys.

Parameters
key: Int

to match

withTagKey

java-static fun withTagKey(key: Int, objectMatcher: Matcher<Any!>!): Matcher<View!>!

Returns a matcher that matches Views based on tag keys.

Parameters
key: Int

to match

objectMatcher: Matcher<Any!>!

Object to match

withTagValue

java-static fun withTagValue(tagValueMatcher: Matcher<Any!>!): Matcher<View!>!

Returns a matcher that matches Views based on tag property values.

Parameters
tagValueMatcher: Matcher<Any!>!

a Matcher for the view's tag property value

withText

java-static fun withText(resourceId: Int): Matcher<View!>!

Returns a matcher that matches a descendant of TextView that is displaying the string associated with the given resource id.

Parameters
resourceId: Int

the string resource the text view is expected to hold.

withText

java-static fun withText(stringMatcher: Matcher<String!>!): Matcher<View!>!

Returns a matcher that matches TextViews based on text property value.

Note: A View text property is never null. If you call setText with a null value it will still be "" (empty string). Do not use a null matcher.

Parameters
stringMatcher: Matcher<String!>!

Matcher of String with text to match

withText

java-static fun withText(text: String!): Matcher<View!>!

Returns a matcher that matches TextView based on its text property value.

Note: View's sugar for withText(is("string")).

Parameters
text: String!

String with the text to match