我在浓缩咖啡测试中有问题,我不知道为什么匹配文本总是会失败,我什至尝试创建简单的应用程序有两个活动,第一个活动具有textView和两个按钮一个按钮显示吐司另一个进行下一个活动,在录制期间,当对文本添加断言并且正确生成的代码我运行的测试时,它总是失败。
android.support.test.spresso.nomatchingViewException:在层次结构中没有视图找到匹配:(带ID:com.example.admin.admin.testtest2demoo:id/textview:id textView and text:with text:is``roh''rooh and Child在父母的位置0在父级的位置为0的孩子ID:Android:ID/content,并在屏幕上显示给用户)
在这里测试代码:
package com.example.admin.testtest2demoo;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void mainActivityTest() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
///when click on this button show toast
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.button2), withText("show SomeThing"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
2),
isDisplayed()));
appCompatButton.perform(click());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
///when click on this button show toast
ViewInteraction appCompatButton2 = onView(
allOf(withId(R.id.button2), withText("show SomeThing"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
2),
isDisplayed()));
appCompatButton2.perform(click());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
///check the text in first activity (is it Rooh)
ViewInteraction textView = onView(
allOf(withId(R.id.textView), withText("Rooh"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
0),
isDisplayed()));
textView.check(matches(withText("Rooh")));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
///when click on this button go to next activity
ViewInteraction appCompatButton3 = onView(
allOf(withId(R.id.button), withText("Go Next"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
0),
isDisplayed()));
appCompatButton3.perform(click());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
///check the text in second activity (is it Hello World!)
ViewInteraction textView2 = onView(
allOf(withId(R.id.tv1), withText("Hello World!"),
childAtPosition(
allOf(withId(R.id.background),
childAtPosition(
withId(R.id.swipe_refresh_layout),
0)),
0),
isDisplayed()));
textView2.check(matches(withText("Hello World!")));
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
这是第一个活动的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:onClick="goNext"
android:text="Go Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintVertical_bias="0.183" />
<TextView
android:id="@+id/textView"
android:layout_width="97dp"
android:layout_height="27dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="Rooh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="64dp"
android:onClick="showSomeThing"
android:text="show SomeThing"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>
第二个活动只有一个文本视图,如果我在上面添加断言,它也会失败。
如果我评论文本断言,测试将通过,但是如果文本断言在那里,它总是失败,我也包括每一步之后的睡眠(我知道它与之无关,但以防万一),我无法理解该代码是由录制生成的,并且文本已正确显示,但在层次结构中没有显示匹配的层次结构的视图!
我做错了什么?预先感谢
此问题归因于依赖UI Automator层次结构来建立元素的子位置,而动作依赖于浓缩咖啡等级。在重播期间,使用浓缩咖啡等级进行了动作和断言,因此,由于UI Automator和Espresso层次结构之间的儿童位置不匹配,主张更容易失败。
。此限制有几种解决方法:
1)在Android Studio中,在文件中|设置,打开构建,执行,部署|浓缩咖啡测试记录器并将"断言深度"设置为1。这样,增加的断言将不依赖儿童位置来识别主张的元素(该断言的生成的测试代码看起来与上面答案中建议的内容非常相似)。缺点是断言可能变得过于笼统,并且可能匹配多个元素。
2)依靠动作来断言屏幕上某些元素的存在,例如,请单击屏幕上存在一个特定文本的按钮,而是单击它。
3)仅记录动作,然后手动将主张添加到生成的浓缩咖啡测试中。
4)手动调整记录的断言以使用适当的子职位。
以下代码看起来完全错误:
ViewInteraction textView = onView(
allOf(withId(R.id.textView), withText("Rooh"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
0),
isDisplayed()));
textView.check(matches(withText("Rooh")));
基于您的布局,只有一个
TextView
组件没有任何孩子。因此,childAtPosition()
无法应用于它。textView.check(matches(withText("Rooh")));
是多余的,就像您声明textView
时已经这样做。
您可以将代码修改为应有效的代码:
ViewInteraction textView = onView(allOf(withId(R.id.textView), withText("Rooh")));
textView.check(matches(isDisplayed()));
更多有关在此处找到的文本匹配的更多信息。