public
final
class
ViewMatchers
extends Object
java.lang.Object | |
↳ | android.support.test.espresso.matcher.ViewMatchers |
A collection of hamcrest matchers that match View
s.
Nested classes | |
---|---|
enum |
ViewMatchers.Visibility
Enumerates the possible list of values for View.getVisibility(). |
Public methods | |
---|---|
static
<T>
void
|
assertThat(String message, T actual, Matcher<T> matcher)
A replacement for MatcherAssert.assertThat that renders View objects nicely. |
static
<T>
void
|
assertThat(T actual, Matcher<T> matcher)
A replacement for MatcherAssert.assertThat that renders View objects nicely. |
static
Matcher<View>
|
hasContentDescription()
Returns an
|
static
Matcher<View>
|
hasDescendant(Matcher<View> descendantMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
hasErrorText(String expectedError)
Returns a matcher that matches |
static
Matcher<View>
|
hasErrorText(Matcher<String> stringMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
hasFocus()
Returns a matcher that matches |
static
Matcher<View>
|
hasImeAction(int imeAction)
Returns a matcher that matches views that support input methods (e.g. |
static
Matcher<View>
|
hasImeAction(Matcher<Integer> imeActionMatcher)
Returns a matcher that matches views that support input methods (e.g. |
static
Matcher<View>
|
hasLinks()
Returns a matcher that matches |
static
Matcher<View>
|
hasSibling(Matcher<View> siblingMatcher)
Returns an
|
static
Matcher<View>
|
isAssignableFrom(Class<? extends View> clazz)
Returns a matcher that matches Views which are an instance of or subclass of the provided class. |
static
Matcher<View>
|
isChecked()
Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is in checked state. |
static
Matcher<View>
|
isClickable()
Returns a matcher that matches |
static
Matcher<View>
|
isCompletelyDisplayed()
Returns a matcher which only accepts a view whose height and width fit perfectly within the currently displayed region of this view. |
static
Matcher<View>
|
isDescendantOfA(Matcher<View> ancestorMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
isDisplayed()
Returns a matcher that matches |
static
Matcher<View>
|
isDisplayingAtLeast(int areaPercentage)
Returns a matcher which accepts a view so long as a given percentage of that view's area is not obscured by any other view and is thus visible to the user. |
static
Matcher<View>
|
isEnabled()
Returns a matcher that matches |
static
Matcher<View>
|
isFocusable()
Returns a matcher that matches |
static
Matcher<View>
|
isJavascriptEnabled()
Returns a matcher that matches |
static
Matcher<View>
|
isNotChecked()
Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is not in checked state. |
static
Matcher<View>
|
isRoot()
Returns a matcher that matches root |
static
Matcher<View>
|
isSelected()
Returns a matcher that matches |
static
Matcher<View>
|
supportsInputMethods()
Returns a matcher that matches views that support input methods. |
static
Matcher<View>
|
withChild(Matcher<View> childMatcher)
A matcher that returns true if and only if the view's child is accepted by the provided matcher. |
static
Matcher<View>
|
withClassName(Matcher<String> classNameMatcher)
Returns a matcher that matches Views with class name matching the given matcher. |
static
Matcher<View>
|
withContentDescription(int resourceId)
Returns a
|
static
Matcher<View>
|
withContentDescription(String text)
Returns an
|
static
Matcher<View>
|
withContentDescription(Matcher<? extends CharSequence> charSequenceMatcher)
Returns an
|
static
Matcher<View>
|
withEffectiveVisibility(ViewMatchers.Visibility visibility)
Returns a matcher that matches |
static
Matcher<View>
|
withHint(Matcher<String> stringMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
withHint(int resourceId)
Returns a matcher that matches a descendant of |
static
Matcher<View>
|
withHint(String hintText)
Returns a matcher that matches |
static
Matcher<View>
|
withId(Matcher<Integer> integerMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
withId(int id)
Same as withId(is(int)), but attempts to look up resource name of the given id and use an R.id.myView style description with describeTo. |
static
Matcher<View>
|
withInputType(int inputType)
Returns a matcher that matches |
static
Matcher<View>
|
withParent(Matcher<View> parentMatcher)
A matcher that accepts a view if and only if the view's parent is accepted by the provided matcher. |
static
Matcher<View>
|
withResourceName(String name)
Returns a matcher that matches |
static
Matcher<View>
|
withResourceName(Matcher<String> stringMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
withSpinnerText(int resourceId)
Returns a matcher that matches a descendant of |
static
Matcher<View>
|
withSpinnerText(String text)
Returns a matcher that matches |
static
Matcher<View>
|
withSpinnerText(Matcher<String> stringMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
withTagKey(int key)
Returns a matcher that matches |
static
Matcher<View>
|
withTagKey(int key, Matcher<Object> objectMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
withTagValue(Matcher<Object> tagValueMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
withText(Matcher<String> stringMatcher)
Returns a matcher that matches |
static
Matcher<View>
|
withText(String text)
Returns a matcher that matches |
static
Matcher<View>
|
withText(int resourceId)
Returns a matcher that matches a descendant of |
Inherited methods | |
---|---|
From
class
java.lang.Object
|
void assertThat (String message, T actual, Matcher<T> matcher)
A replacement for MatcherAssert.assertThat that renders View objects nicely.
Parameters | |
---|---|
message |
String :
the message to display. |
actual |
T :
the actual value. |
matcher |
Matcher :
a matcher that accepts or rejects actual.
|
void assertThat (T actual, Matcher<T> matcher)
A replacement for MatcherAssert.assertThat that renders View objects nicely.
Parameters | |
---|---|
actual |
T :
the actual value. |
matcher |
Matcher :
a matcher that accepts or rejects actual.
|
Matcher<View> hasContentDescription ()
Returns an
Matcher
that matches View
s with any content description.
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasDescendant (Matcher<View> descendantMatcher)
Returns a matcher that matches View
s based on the presence of a descendant in its view
hierarchy.
Parameters | |
---|---|
descendantMatcher |
Matcher :
the type of the descendant to match on
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasErrorText (String expectedError)
Returns a matcher that matches EditText
based on edit text error string value.
Note: Sugar for hasErrorText(is("string")).
Parameters | |
---|---|
expectedError |
String
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasErrorText (Matcher<String> stringMatcher)
Returns a matcher that matches EditText
based on edit text error string value.
Parameters | |
---|---|
stringMatcher |
Matcher
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasFocus ()
Returns a matcher that matches View
s currently have focus.
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasImeAction (int imeAction)
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
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasImeAction (Matcher<Integer> imeActionMatcher)
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 :
a matcher for the IME action
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasLinks ()
Returns a matcher that matches TextView
s that have links.
Returns | |
---|---|
Matcher<View> |
Matcher<View> hasSibling (Matcher<View> siblingMatcher)
Returns an
Matcher
that matches View
s 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 :
a
Matcher for the sibling of the view.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> isAssignableFrom (Class<? extends View> clazz)
Returns a matcher that matches Views which are an instance of 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.
Parameters | |
---|---|
clazz |
Class
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> isChecked ()
Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is in checked state.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isClickable ()
Returns a matcher that matches View
s that are clickable.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isCompletelyDisplayed ()
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.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isDescendantOfA (Matcher<View> ancestorMatcher)
Returns a matcher that matches View
s based on the given ancestor type.
Parameters | |
---|---|
ancestorMatcher |
Matcher :
the type of the ancestor to match on
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> isDisplayed ()
Returns a matcher that matches View
s 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 then 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.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isDisplayingAtLeast (int areaPercentage)
Returns a matcher which accepts a view so long as a given percentage of that view's area is not obscured by any other 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.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> isEnabled ()
Returns a matcher that matches View
s that are enabled.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isFocusable ()
Returns a matcher that matches View
s that are focusable.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isJavascriptEnabled ()
Returns a matcher that matches WebView
if they are evaluating Javascript.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isNotChecked ()
Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is not in checked state.
Returns | |
---|---|
Matcher<View> |
Matcher<View> isSelected ()
Returns a matcher that matches View
s that are selected.
Returns | |
---|---|
Matcher<View> |
Matcher<View> supportsInputMethods ()
Returns a matcher that matches views that support input methods.
Returns | |
---|---|
Matcher<View> |
Matcher<View> withChild (Matcher<View> childMatcher)
A matcher that returns true if and only if the view's child is accepted by the provided matcher.
Parameters | |
---|---|
childMatcher |
Matcher :
the matcher to apply on the child views.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withClassName (Matcher<String> classNameMatcher)
Returns a matcher that matches Views with class name matching the given matcher.
Parameters | |
---|---|
classNameMatcher |
Matcher
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withContentDescription (int resourceId)
Returns a
Matcher
that matches View
s based on content description property
value.
Parameters | |
---|---|
resourceId |
int :
the resource id of the content description to match on.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withContentDescription (String text)
Returns an
Matcher
that matches View
s based on content description property
value. Sugar for withContentDescription(is("string")).
Parameters | |
---|---|
text |
String :
the text to match on.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withContentDescription (Matcher<? extends CharSequence> charSequenceMatcher)
Returns an
Matcher
that matches View
s based on content description property
value.
Parameters | |
---|---|
charSequenceMatcher |
Matcher :
a CharSequence
Matcher for the content description
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withEffectiveVisibility (ViewMatchers.Visibility visibility)
Returns a matcher that matches View
s 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.
Parameters | |
---|---|
visibility |
ViewMatchers.Visibility
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withHint (Matcher<String> stringMatcher)
Returns a matcher that matches TextView
s based on hint property value. Note: View's
hint property can be null.
Parameters | |
---|---|
stringMatcher |
Matcher :
Matcher of String with text to match
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withHint (int resourceId)
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.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withHint (String hintText)
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
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withId (Matcher<Integer> integerMatcher)
Returns a matcher that matches View
s 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 :
a Matcher for resource ids
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withId (int id)
Same as withId(is(int)), but attempts to look up resource name of the given id and use an 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 resource lookup is available, but looking up the name for the given id, fails, "with id: %d (resource name not found)" will be returned as the description.
Parameters | |
---|---|
id |
int :
the resource id.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withInputType (int inputType)
Returns a matcher that matches InputType
.
Parameters | |
---|---|
inputType |
int
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withParent (Matcher<View> parentMatcher)
A matcher that accepts a view if and only if the view's parent is accepted by the provided matcher.
Parameters | |
---|---|
parentMatcher |
Matcher :
the matcher to apply on getParent.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withResourceName (String name)
Returns a matcher that matches View
s based on resource id names,
(for instance, channel_avatar).
Parameters | |
---|---|
name |
String :
the resource id name
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withResourceName (Matcher<String> stringMatcher)
Returns a matcher that matches View
s based on resource id names,
(for instance, channel_avatar).
Parameters | |
---|---|
stringMatcher |
Matcher :
a Matcher for resource id names
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withSpinnerText (int resourceId)
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 string resource the text view is expected to hold.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withSpinnerText (String text)
Returns a matcher that matches Spinner
based on it's selected item's toString value.
Note: Sugar for withSpinnerText(is("string")).
Parameters | |
---|---|
text |
String
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withSpinnerText (Matcher<String> stringMatcher)
Returns a matcher that matches Spinner
s based on toString value of the selected item.
Parameters | |
---|---|
stringMatcher |
Matcher :
Matcher of String with text to match.
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withTagKey (int key)
Returns a matcher that matches View
based on tag keys.
Parameters | |
---|---|
key |
int :
to match
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withTagKey (int key, Matcher<Object> objectMatcher)
Returns a matcher that matches View
s based on tag keys.
Parameters | |
---|---|
key |
int :
to match |
objectMatcher |
Matcher :
Object to match
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withTagValue (Matcher<Object> tagValueMatcher)
Returns a matcher that matches View
s based on tag property values.
Parameters | |
---|---|
tagValueMatcher |
Matcher :
a Matcher for the view's tag property value
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withText (Matcher<String> stringMatcher)
Returns a matcher that matches TextView
s based on text property value. Note: View's
text property is never null. If you setText(null) it will still be "". Do not use null matcher.
Parameters | |
---|---|
stringMatcher |
Matcher :
Matcher of String with text to match
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withText (String text)
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
|
Returns | |
---|---|
Matcher<View> |
Matcher<View> withText (int resourceId)
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.
|
Returns | |
---|---|
Matcher<View> |