Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android.support.test.espresso.PerformException:执行时出错';加载适配器数据';在望_Android_Listview_Android Espresso_Listadapter_Android Espresso Recorder - Fatal编程技术网

android.support.test.espresso.PerformException:执行时出错';加载适配器数据';在望

android.support.test.espresso.PerformException:执行时出错';加载适配器数据';在望,android,listview,android-espresso,listadapter,android-espresso-recorder,Android,Listview,Android Espresso,Listadapter,Android Espresso Recorder,我正在使用Espresso测试一个列表视图,当我搜索一个项目(比如自动完成)时,它会出现。在用户在SearchView中键入内容之前,列表视图不会出现。i、 e.我将ListView设置为视图。仅当用户在SearchView中键入内容时才可见 当我尝试单击列表视图中的文本时,出现此错误android.support.test.espresso.PerformException:在id为“”的视图上执行“加载适配器数据”时出错。。使用onData不起作用 添加人为延迟是有效的,但我不确定这是否是一

我正在使用Espresso测试一个列表视图,当我搜索一个项目(比如自动完成)时,它会出现。在用户在SearchView中键入内容之前,列表视图不会出现。i、 e.我将ListView设置为
视图。仅当用户在SearchView中键入内容时才可见

当我尝试单击列表视图中的文本时,出现此错误<代码>android.support.test.espresso.PerformException:在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());
}
添加一个人工延迟是有效的,但我不确定这是否是坏的 练习,因为它似乎违背了方法的目的,例如 昂达塔等

您的
错误
浓缩咖啡
限制。这个框架需要在UI线程上运行,它会“等待”直到空闲。它不等待加载适配器数据,而是等待获取空闲资源

检查:

IdlingResource参考:

IdlingResource文档:

CountingIdlingResource:

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

解决方案是创建您自己的
Espresso IdlingResource
,告诉Espresso何时可以在不丢失数据和时间的情况下执行测试


希望这会有所帮助。

在视图上执行“加载适配器数据”时收到相同的错误消息错误,这些帖子中的答案对我都不起作用



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

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

@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));
            }
        };
    }
}
@LargeTest
@RunWith(AndroidJUnit4.class)
公共类维护活动测试{
@统治
public ActivityTestRule mActivityTestRule=新的ActivityTestRule(MainActivity.class);
@试验
公共无效维护活动测试(){
//添加了睡眠语句以匹配应用程序的执行延迟。
//处理此类情况的推荐方法是使用Espresso闲置资源:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
试一试{
睡眠(10000);
}捕捉(中断异常e){
e、 printStackTrace();
}
ViewInteraction recyclerView=onView(
allOf(带id(R.id.回收商\查看\列表),
儿童体位(
withClassName(is(“android.support.constraint.ConstraintLayout”),
0)));
执行(ActionItemPosition(0,click());
}
专用静态匹配器子定位(
最终匹配器父匹配器,最终整型位置){
返回新的TypeSafeMatcher(){
@凌驾
公共无效说明(说明){
description.appendText(“父项中的子项位置“+位置+”);
parentMatcher.descripbeto(描述);
}
@凌驾
公共布尔匹配安全(视图){
ViewParent=view.getParent();
返回视图组的父实例(&parentMatcher.matches)(父)
&&view.equals(((视图组)parent.getChildAt(位置));
}
};
}
}
注意:Espresso UI测试记录器生成的代码也不是完美的,它在大多数情况下都有效,但不是100%