浓缩咖啡选择包含布局的子项 [英] Espresso select children of included layout

查看:92
本文介绍了浓缩咖啡选择包含布局的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Espresso与Android应用程序进行自动化的UI测试. (我一直在努力寻找下班时间解决问题的方法,所以我没有确切的示例和错误,但明天早上我可以进行更新).我在单个用户界面中多次包含的布局中的单元测试按钮遇到了问题.下面是一个简单的示例:

I have been using Espresso to carry out automated UI testing with an Android app. (I have been trying to find a resolution to the issue whilst at home from work, so I don’t have the exact examples and errors, but I can update tomorrow morning). I have run into an issue with unit testing buttons within a layout that is included multiple times within a single user interface. Below is a quick example:

<include 
   android:id="@+id/include_one"
   android:layout="@layout/boxes" />

<include 
   android:id="@+id/include_two"
   android:layout="@layout/boxes" />

<include 
    android:id="@+id/include_three"
    android:layout="@layout/boxes" />

以下是@ layout/boxes中的内容的一个示例:

Here is an example of what is within the @layout/boxes:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1" />
    <Button
        android:id="@+id/button2" />
</RelativeLayout>

我似乎无法访问我想要的"include_one"包含中的一个按钮,而无需访问所有三个按钮.

I am seemingly unable to access button one within the include I want "include_one", without accessing all three of the buttons.

我尝试通过以下方式访问按钮:

I have tried accessing the buttons with the following:

onView(allOf(withId(R.id.include_one), isDescendantOfA(withId(R.id.button1)))).perform(click());

onView(allOf(withId(R.id.button1), hasParent(withId(R.id.include_one)))).perform(click());

我都从这个答案中找到两个: onChildView和hasSiblings with Espresso 不幸的是我还没有成功!

Both of which I found from this answer: onChildView and hasSiblings with Espresso Unfortunately I haven’t had any success!

我知道这不好,但是由于我不在工作计算机上,所以我无法告诉您我遇到的确切错误,但是我遇到了:

I know this isn’t great, but as I am not with my work computer I can’t tell you the exact errors I have come across, but I have encountered:

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException

也是一个错误,告诉我没有找到匹配项.

also an error telling me there were no matches found.

尽管我是Espresso的新手,但我使用的代码还是有意义的.有人可以提供一些建议或指出我可能会误解的地方吗?

The code I am using makes sense, although I am new to using Espresso Can anyone offer some advice, or point out what I may be misunderstanding?

推荐答案

当多次尝试在同一布局中多次<include/>相同的自定义xml时,这是一个常见的陷阱.

This is a common pitfall when trying to <include/> the same custom xml several times in the same layout.

如果您现在尝试致电

Button button1 = (Button) findViewById(R.id.button1);

由于多次包含了 boxes.xml ,因此,您将始终获得第一个子布局中存在的按钮,而永远不会出现该按钮.

since the boxes.xml is included more than once, you will always get as a result the button present in the first sub layout, and never another one.

您非常接近,但是您需要使用 withParent()视图匹配器.

You were pretty close but you need to use the withParent() view matcher.

onView(allOf(withId(R.id.button1), withParent(withId(R.id.include_one))))
                .check(matches(isDisplayed()))
                .perform(click());

这篇关于浓缩咖啡选择包含布局的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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