【问题标题】:android.support.test.espresso.PerformException: Error performing 'load adapter data' on viewandroid.support.test.espresso.PerformException:在视图上执行“加载适配器数据”时出错
【发布时间】:2016-09-11 17:42:13
【问题描述】:

我正在使用 Espresso 测试在我搜索项目时出现的列表视图(如自动完成)。在用户在 SearchView 中输入内容之前,列表视图不会出现。即我将 ListView 设置为 View.VISIBLE 仅当用户在 SearchView 中输入内容时

当我尝试单击列表视图中的文本时出现此错误。 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());
}

【问题讨论】:

    标签: android listview android-espresso listadapter android-espresso-recorder


    【解决方案1】:

    添加人为延迟有效,但我不确定这是否不好 练习,因为它似乎违背了方法的目的,例如 onData 等

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

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

    IdlingResource 参考:https://developer.android.com/reference/android/support/test/espresso/IdlingResource.html

    IdlingResource 文档: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 何时可以执行测试而不会丢失数据和时间。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

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

      Testing RecyclerView if it has data with Espresso
      Espresso onData Error performing 'load adapter data' on view
      Espresso. Error performing 'load adapter data'

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

      单击 RecyclerView 上的项目生成的代码如下所示。这里的重头戏是 Matcher 方法childAtPosition() 在测试开始时,它会休眠 10 秒以确保所有内容都已加载,通常不需要 10 秒,您可以将其减少到大约 2 秒。另一种方法是像@piotrek1543 建议的那样使用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));
                  }
              };
          }
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-11
        • 2020-11-24
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-11
        相关资源
        最近更新 更多