我在浓缩咖啡测试中遇到问题,我不知道为什么匹配文本总是失败,我什至尝试创建简单的应用程序有两个活动,第一个活动有 textview 和两个按钮一个按钮显示吐司另一个去下一个活动,在录制过程中,当向文本添加断言并正确生成代码时,我运行测试它总是失败,显示此错误:
android.support.test.espresso.NoMatchingViewException:层次结构中没有找到匹配的视图:(id:com.example.admin.testtest2demoo:id/textView 和文本:是“Rooh”,子在父子位置的位置 0 0 in parent with 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>
第二个活动只有一个文本视图,如果我在其上添加断言,它也会失败。
如果我评论文本断言测试将通过,但如果文本断言存在它总是失败,我还在每一步之后包括睡眠(我知道它与它无关,但以防万一),我无法理解代码是由录制生成,文本显示正确,但显示层次结构中未找到匹配的视图!
我做错了什么?!提前致谢