[英]Android::android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching
[英]android espresso test is fails always in text matching
我在浓缩咖啡测试中遇到问题,我不知道为什么匹配文本总是失败,我什至尝试创建简单的应用程序有两个活动,第一个活动有 textview 和两个按钮一个按钮显示吐司另一个转到下一个活动,在录制过程中向文本添加断言和正确生成的代码我运行测试它总是失败,显示此错误:
android.support.test.espresso.NoMatchingViewException:在层次结构中没有找到匹配的视图:(id:com.example.admin.testtest2demoo:id/textView 和文本:是“Rooh”和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 层次结构来建立元素的子位置,而动作则依赖于 Espresso 层次结构。 在重放期间,操作和断言都是使用 Espresso 层次结构执行的,因此,由于 UI Automator 和 Espresso 层次结构之间的子位置不匹配,断言更容易失败。
此限制有几种解决方法:
1) 在 Android Studio 中,在文件中 | 设置,打开构建、执行、部署 | Espresso 测试记录器并将“断言深度”设置为 1。这样,添加的断言将不依赖于子位置来识别断言的元素(为断言生成的测试代码看起来与上面答案中的建议非常相似) . 缺点是断言可能变得过于笼统,并且可能匹配多个元素。
2) 依靠动作来断言屏幕上某些元素的存在,例如,与其断言屏幕上存在具有特定文本的按钮,不如单击它。
3) 仅记录操作并手动将断言添加到生成的 Espresso 测试中。
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()));
有关匹配文本的更多信息,您可以在此处找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.