Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 receive confirmation code which has 4 digits long.

private void init()
{
    ...

    for (int i = 0; i < 4; i++)
    {
        DigitEditText digitView = getDigitInput();
        digitView.setTag(R.id.etPinCodeView, i); // uses for Espresso testing
        digitView.setKeyEventCallback(this);
        ...
}

I have created res/values/ids.xml and this is its content:

<resources>
    <item name="etPinCodeView" type="id"/>
</resources>

Now, in Espresso I want to catch each DigitEditText and put a digit in it. How I'm able to do that? I see there are two methodes, withTagKey() and withTagValue() but I have no idea how to get them into work.

I thought something like this might work but seems I'm not able to assign 0 into withTagValue().

onView(allOf(withTagKey(R.id.etPinCodeView), withTagValue(matches(0)))).perform(typeText("2"));
like image 980
Hesam Avatar asked Feb 27 '16 20:02

Hesam


People also ask

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.

How do you assert espresso?

check is a method which accepts an argument of type ViewAssertion and do assertion using passed in ViewAssertion object. matches(withText(“Hello”)) returns a view assertion, which will do the real job of asserting that both actual view (found using withId) and expected view (found using withText) are one and the same.


1 Answers

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)));
like image 131
petey Avatar answered Sep 30 '22 17:09

petey