如何在Android中通过Espresso捕获带有标签的视图? [英] How to catch a View with Tag by Espresso in Android?

查看:61
本文介绍了如何在Android中通过Espresso捕获带有标签的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展了LinearLayoutPinCodeView.我的init()方法中有以下代码. DigitEditText扩展EditText并且仅接受一位数字.此视图将用于接收长4位数字的确认代码.

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);
        ...
}

我已经创建了res/values/ids.xml,这是它的内容:

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

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

现在,在Espresso中,我想捕捉每个DigitEditText并在其中放入一个数字.我该怎么做?我看到有两种方法,withTagKey()withTagValue(),但是我不知道如何使它们起作用.

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.

我认为类似的方法可能有用,但是似乎我无法为withTagValue()分配0.

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"));

推荐答案

我用此技巧解决了我的问题.希望它为您节省一些时间.

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

首先,我使用Id而不是tag.

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

这是对它的测试:

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"));

这篇关于如何在Android中通过Espresso捕获带有标签的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆