首页 > 解决方案 > android espresso 测试在文本匹配中总是失败

问题描述

我在浓缩咖啡测试中遇到问题,我不知道为什么匹配文本总是失败,我什至尝试创建简单的应用程序有两个活动,第一个活动有 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>

第二个活动只有一个文本视图,如果我在其上添加断言,它也会失败。

如果我评论文本断言测试将通过,但如果文本断言存在它总是失败,我还在每一步之后包括睡眠(我知道它与它无关,但以防万一),我无法理解代码是由录制生成,文本显示正确,但显示层次结构中未找到匹配的视图!

我做错了什么?!提前致谢

标签: javaandroidtestingandroid-espressotextmatching

解决方案


这个问题是由于断言依赖于 UI Automator 层次来建立元素的子位置,而操作依赖于 Espresso 层次。在回放期间,操作和断言都是使用 Espresso 层次结构执行的,因此,由于 UI Automator 和 Espresso 层次结构之间的子位置不匹配,断言更容易失败。

此限制有多种解决方法:

1) 在 Android Studio 中,在文件 | 设置,打开构建,执行,部署 | Espresso 测试记录器并将“断言深度”设置为 1。这样,添加的断言将不依赖子位置来识别断言元素(断言的生成测试代码看起来与上面答案中建议的非常相似) . 缺点是断言可能变得过于笼统并且可能匹配多个元素。

2) 依靠动作来断言屏幕上某些元素的存在,例如,与其断言带有特定文本的按钮存在于屏幕上,不如点击它。

3) 仅记录操作并将断言手动添加到生成的 Espresso 测试中。

4) 手动调整记录的断言以使用正确的子位置。


推荐阅读