android.support.test.espresso.PerformException:在视图上执行 'load adapter data' 时出错

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view

我正在使用 Espresso 测试在我搜索项目时出现的列表视图(例如自动完成)。直到用户在 SearchView 中输入内容后,列表视图才会出现。即,仅当用户在 SearchView

中输入内容时,我才将 ListView 设置为 View.VISIBLE

当我尝试单击列表视图中的文本时出现此错误。 android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id:'。使用 onData 无效。

添加人为延迟是可行的,但我不确定这是否是一种不好的做法,因为它似乎违背了 onData 等方法的目的。

我试过的:

我的代码

此代码有效,但我不希望引入人为延迟。

public pickSuggestion(int index){

    /** artificial delay to allow list to appear. 
    This works but I shouldn't have to do this right? **/

    SystemClock.sleep(1000);

    onData(anything())
        .inAdapterView(withId(R.id.list))
        .atPosition(index)
        .onChildView(withId(R.id.mTextView))
        .perform(click());
}

Adding an artificial delay works, but I am unsure if this is bad practice since it seems to defeat the purpose of methods such as onData etc.

您的 errorEspresso 限制。这个框架需要在 UI 线程上 运行 并且它 'waits' 直到它空闲。它不等待加载适配器数据,而是等待获取空闲资源

检查:http://dev.jimdo.com/2014/05/09/wait-for-it-a-deep-dive-into-espresso-s-idling-resources/

空闲资源参考:https://developer.android.com/reference/android/support/test/espresso/IdlingResource.html

空闲资源文档:https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html

CountingIdlingResource:https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource.html

SystemClock.sleep(1000)Thread.sleep(1000) 这样的代码是一种不好的做法,因为更好的设备不需要这么多时间,而旧设备需要更多时间,所以您的代码可能不稳定,而不是快速灵活.

解决方案是创建您自己的 Espresso IdlingResource 以告知 Espresso 何时可以执行测试而不会丢失数据和时间。

希望这会有所帮助。

收到相同的错误消息在视图 上执行'load adapter data' 时出错,这些帖子的答案none 对我有用。


Espresso onData Error performing 'load adapter data' on view

我最终在 Android Studio 中使用了 Espresso UI test recorder。从顶部下拉菜单转到 运行,然后单击 Record Espresso Test。它会要求您选择要 运行 的设备,应用程序启动后,手动执行您喜欢执行的 ui 测试,并在需要时添加断言。完成后点击 确定。它将为您生成一个 UI 测试文件。

单击 RecyclerView 上的项目生成的代码如下所示。这里的繁重工作是 Matcher 方法 childAtPosition() 在测试开始时,它会休眠 10 秒以确保所有内容都已加载,通常不需要 10 秒,您可以将其减少到大约 2 秒。另一种方法是像 建议的那样使用 Espresso IdlingResource,但是要求 uires 在生产代码中添加干预(混合测试特定代码)以适应测试。

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void mainActivityTest() {
        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction recyclerView = onView(
                allOf(withId(R.id.recycler_view_list),
                        childAtPosition(
                                withClassName(is("android.support.constraint.ConstraintLayout")),
                                0)));
        recyclerView.perform(actionOnItemAtPosition(0, click()));
    }

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

注意:Espresso UI 测试记录器生成的代码也不是完美的,它大部分时间都有效,但不是 100%