Skip to content Skip to sidebar Skip to footer

How To Catch A View With Tag By Espresso In Android?

I have a PinCodeView that extends LinearLayout. I have following code in my init() method. DigitEditText extends EditText and just accepts one digit. This view will be used to rece

Solution 1:

Since withTagValue needs an instance of org.hamcrest.Matcher as an argument, we can create a simple one using the Matcher.is method to find views with a certain tag in your expresso test:

String tagValue = "lorem impsum";
ViewInteraction viewWithTagVI = onView(withTagValue(is((Object) tagValue)));

Solution 2:

I solved my problem with this trick. Hope it saves some times for you.

First I used Id rather than tag.

for (inti=0; i < 4; i++)
    {
        DigitEditTextdigitView= getDigitInput();
        digitView.setId(R.id.etPinCodeView + i); // uses for Espresso testing
        digitView.setKeyEventCallback(this);
        ...

And this is test for it:

onView(withId(R.id.etPinCodeView + 0)).perform(typeText("2"));
onView(withId(R.id.etPinCodeView + 1)).perform(typeText("0"));
onView(withId(R.id.etPinCodeView + 2)).perform(typeText("1"));
onView(withId(R.id.etPinCodeView + 3)).perform(typeText("6"));

Solution 3:

After setting the tag in your view somewhere in the app, for those confused about the syntax in Kotlin:

withTagValue(`is`(EXPECTED_TAG))

The complete syntax to assert a tag on a specific view:

onView(
   allOf(
       withId(R.id.some_id),
       withTagValue(`is`(EXPECTED_TAG))
   )
)

Simple :)

Post a Comment for "How To Catch A View With Tag By Espresso In Android?"