简体   繁体   English

android espresso 测试总是在文本匹配中失败

[英]android espresso test is fails always in text matching

I have a problem in espresso test, I don't know why matching the text is always fail with me, I even tried to create simple app has two activities, the first activity has textview and two buttons one button show toast another go next activity, during the recording when add assertion to the text and the code generated correctly I run the test it always fails showing this error:我在浓缩咖啡测试中遇到问题,我不知道为什么匹配文本总是失败,我什至尝试创建简单的应用程序有两个活动,第一个活动有 textview 和两个按钮一个按钮显示吐司另一个转到下一个活动,在录制过程中向文本添加断言和正确生成的代码我运行测试它总是失败,显示此错误:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: com.example.admin.testtest2demoo:id/textView and with text: is "Rooh" and Child at position 0 in parent Child at position 0 in parent with id: android:id/content and is displayed on the screen to the user) android.support.test.espresso.NoMatchingViewException:在层次结构中没有找到匹配的视图:(id:com.example.admin.testtest2demoo:id/textView 和文本:是“Rooh”和Child在位置0的父子位置0 在父级中,id: android:id/content 并在屏幕上显示给用户)

here the test code :这里的测试代码:

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));
        }
    };
}
}

And this the layout of first activity :这是第一个活动的布局:

<?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>

And the second activity has only one textview, and also its fails if I add assertion on it.第二个活动只有一个文本视图,如果我在上面添加断言,它也会失败。

if I comment the text assertions the test will pass, but if text assertion is there it always fails, I also include sleep after each step (I know its nothing to do with it but just in case), I can not understand the code is generated by the recording and the text is displayed correctly but it says No views in hierarchy found matching!!如果我评论文本断言测试将通过,但如果文本断言在那里它总是失败,我还在每一步之后包括睡眠(我知道它与它无关但以防万一),我无法理解代码是由录音生成,文本显示正确,但它说在层次结构中没有找到匹配的视图!!

what I am doing wrong?!我做错了什么?! thanks in advance提前致谢

This problem is due to assertions relying on UI Automator hierarchy to establish an element's child position, while actions are relying on Espresso hierarchy.此问题是由于断言依赖于 UI Automator 层次结构来建立元素的子位置,而动作则依赖于 Espresso 层次结构。 During replay, both actions and assertions are performed using Espresso hierarchy, and as a result, assertions are more prone to failures due to child position mismatches between UI Automator and Espresso hierarchies.在重放期间,操作和断言都是使用 Espresso 层次结构执行的,因此,由于 UI Automator 和 Espresso 层次结构之间的子位置不匹配,断言更容易失败。

There are several workarounds for this limitation:此限制有几种解决方法:

1) In Android Studio, in File | 1) 在 Android Studio 中,在文件中 | Settings, open Build, Execution, Deployment |设置,打开构建、执行、部署 | Espresso Test Recorder and set "Assertion depth" to 1. This way, the added assertions will not rely on child positions to identify the asserted elements (the generated test code for the assertion will look very similar to what was suggested in the answer above). Espresso 测试记录器并将“断言深度”设置为 1。这样,添加的断言将不依赖于子位置来识别断言的元素(为断言生成的测试代码看起来与上面答案中的建议非常相似) . The downside is that an assertion might become too general and might match more than one element.缺点是断言可能变得过于笼统,并且可能匹配多个元素。

2) Rely on actions to assert existence of certain elements on the screen, eg, rather than asserting that a button with a particular text exists on the screen, click on it. 2) 依靠动作来断言屏幕上某些元素的存在,例如,与其断言屏幕上存在具有特定文本的按钮,不如单击它。

3) Record only actions and add assertions manually into the generated Espresso test. 3) 仅记录操作并手动将断言添加到生成的 Espresso 测试中。

4) Manually tweak the recorded assertions to use proper child positions. 4) 手动调整记录的断言以使用正确的子位置。

The below code looks totally wrong:下面的代码看起来完全错误:

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")));
  1. Based on your layout there is only one TextView component which doesn't have any children.根据您的布局,只有一个TextView组件没有任何子组件。 So, childAtPosition() can't be applied to it.因此, childAtPosition()不能应用于它。

  2. textView.check(matches(withText("Rooh"))); is redundant as you already do this when you declare textView .是多余的,因为您在声明textView时已经这样做了。

You can modify your code to the following one which should work:您可以将代码修改为以下应该有效的代码:

ViewInteraction textView = onView(allOf(withId(R.id.textView), withText("Rooh")));
textView.check(matches(isDisplayed()));

More about matching the text you can find here .有关匹配文本的更多信息,您可以在此处找到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android::android.support.test.espresso.NoMatchingViewException:在层次结构中没有找到匹配的视图 - Android::android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching 生成的Android espresso 测试运行失败,AndroidJUnitRunner 解析失败:Landroidx/test/platform/io/FileTestStorage; - Generated Android espresso test fails to run, AndroidJUnitRunner failed to resolve: Landroidx/test/platform/io/FileTestStorage; 浓咖啡测试android中的多个匹配项 - multi matchs in espresso test android 使用 Espresso 的 Android 仪器测试失败:javax.inject.Provider 中的 NoSuchMethodError get() - Android instrument test with Espresso fails: NoSuchMethodError get() in javax.inject.Provider 由于 Android 消息“查看全屏,要退出,从顶部向下滑动”,Espresso 测试失败 - Espresso test fails due to Android message `Viewing full screen, To exit, swipe down from the top` Espresso Android匹配父级内的文本视图 - Espresso Android matching textviews inside parent Android espresso 在层次结构中没有找到匹配的视图 - Android espresso No views in hierarchy found matching 如何在android espresso测试中向下滚动屏幕? 我需要验证屏幕上的文本 - How to scroll down the screen in the android espresso test? I need to validate the text present on the screen 如何在espresso UI测试中检查动态文本 - How to check dynamic text in espresso UI test 使用@Parameters的Android Espresso测试需要上下文 - Android Espresso test with @Parameters needs context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM