jor*_*rry 18 android android-espresso
我一直在使用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任何人都可以提供一些建议,或者指出我可能会误解的是什么?
app*_*oll 22
<include/>在同一布局中多次尝试使用相同的自定义xml 时,这是一个常见的陷阱.
如果你现在试着打电话
Button button1 = (Button) findViewById(R.id.button1);
Run Code Online (Sandbox Code Playgroud)
由于boxes.xml包含多次,因此您将始终得到第一个子布局中存在的按钮,而不是另一个.
你非常接近,但你需要使用withParent()视图匹配器.
onView(allOf(withId(R.id.button1), withParent(withId(R.id.include_one))))
.check(matches(isDisplayed()))
.perform(click());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12976 次 |
| 最近记录: |