我一直在使用Espresso通过Android应用程序进行自动UI测试.(我一直试图在下班回家时找到解决问题的方法,所以我没有确切的例子和错误,但我明天早上可以更新).我在单个用户界面中多次包含的布局中遇到了单元测试按钮的问题.以下是一个简单的例子:
<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" />
Run Code Online (Sandbox Code Playgroud)
以下是@ layout/box中的内容示例:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1" />
<Button
android:id="@+id/button2" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我似乎无法访问包含我想要"include_one"的按钮,而无需访问所有三个按钮.
我尝试使用以下内容访问按钮:
onView(allOf(withId(R.id.include_one), isDescendantOfA(withId(R.id.button1)))).perform(click());
Run Code Online (Sandbox Code Playgroud)
和
onView(allOf(withId(R.id.button1), hasParent(withId(R.id.include_one)))).perform(click());
Run Code Online (Sandbox Code Playgroud)
我从这个答案中找到了这两个:onChildView和hasSiblings with Espresso很遗憾我没有取得任何成功!
我知道这不是很好,但由于我不在我的工作电脑上,我无法告诉你我遇到的确切错误,但我遇到过:
com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException
Run Code Online (Sandbox Code Playgroud)
还有一个错误告诉我没有找到匹配项.
我使用的代码是有道理的,虽然我是新手使用Espresso任何人都可以提供一些建议,或者指出我可能会误解的是什么?
我收到以下错误:"比较方法违反了其总合同!" 使用以下比较器时,我无法使用jUnit复制异常.我想知道导致这个问题的原因以及如何复制它.有些其他例子有相同的问题,但没有如何复制它.
public class DtoComparator implements Comparator<Dto> {
@Override
public int compare(Dto r1, Dto r2) {
int value = 0;
value = r1.getOrder() - r2.getOrder();
if (value == 0 && !isValueNull(r1.getDate(), r2.getDate()))
value = r1.getDate().compareTo(r2.getDate());
return value;
}
private boolean isValueNull(Date date, Date date2) {
return date == null || date2 == null;
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下代码调用代码:
Collections.sort(dtos, new DtoComparator());
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
额外信息:错误似乎发生在Java utils内部的TimSort类中以及名为mergeLo的方法中.链接:http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/TimSort.java#TimSort.mergeLo%28int%2Cint%2Cint%2Cint% 29