【问题标题】:Testing endless scrolling RecyclerView with Espresso and RxJava使用 Espresso 和 RxJava 测试无限滚动 RecyclerView
【发布时间】:2016-12-08 10:50:47
【问题描述】:

我有RecyclerView 无限滚动。因此,当用户到达列表中的last - 2 位置时,我会调用服务器以获取更多数据,并且在调用过程中,我会再添加一项 - 进度一项。

现在,我正在尝试使用 Espresso 编写体面的 UI 测试,以检查当前是否无休止滚动:

    @Test
    public void checkIfProgressShown() {
        InstaFeed feed = TestDataFactory.makeInstaFeed(20);
        InstaFeed oldFeed = TestDataFactory.makeInstaFeed(20);
               when(mockDataManager.getFeedItemsFromServer()).thenReturn(Observable.just(feed.getInstaItems()));
        when(mockDataManager.getOldFeedItemsFromServer()).thenReturn(Observable.just(oldFeed.getInstaItems())
                .delay(2, TimeUnit.SECONDS));

        instaActivityActivityTestRule.launchActivity(null);

        int position = 0;
        for (InstaItem item : feed.getInstaItems()) {
            onView(withId(R.id.recycler_view))
               .perform(RecyclerViewActions.scrollToPosition(position));
            onView(withText(item.getLocation().getName()))
                .check(matches(isDisplayed())); // Line of crash
            position++;
        }

        onView(withId(R.id.progress))
                .check(matches(isDisplayed()));
    }

所以基本上说,我正在尝试延迟来自 Observable 的响应,并显示新批次的项目,以便 Espresso 可以向下滚动到最后一个项目并使我的 progress 项目可见。问题就在这里,测试只是堆栈,从 UI 的角度来看,它看起来像:

不要因为这里没有进度条而感到困惑——设备上禁用了动画,所以没关系。最后,小项目是有进度的项目

60 秒后它崩溃了 -

android.support.test.espresso.AppNotIdleException: Looped for 3019 iterations over 60 SECONDS. The following Idle Conditions failed .
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at org.kidinov.mvp_test.InstaActivityTest.checkIfProgressShown(InstaActivityTest.java:69)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.kidinov.mvp_test.test.common.TestComponentRule$1.evaluate(TestComponentRule.java:49)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at org.kidinov.mvp_test.runner.UnlockDeviceAndroidJUnitRunner.onStart(UnlockDeviceAndroidJUnitRunner.java:36)
at org.kidinov.mvp_test.runner.RxAndroidJUnitRunner.onStart(RxAndroidJUnitRunner.java:16)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)

我使用RxAndroidJUnitRunner 作为跑步者。

可能是我做错了,没必要做模拟延迟?


UPD:如果您想处理这种情况,请按顺序使用prepared projectthat class 内部的问题。

【问题讨论】:

  • 有很多可能导致您的主线程冻结。计算调度程序是否有任何方式与即时调度程序挂钩?或者可以检查您是否在延迟的 observable 上添加 subscribeOn 和 observeOn 问题仍然存在?

标签: android automated-tests rx-java android-espresso ui-testing


【解决方案1】:

我花了一些时间修复了你的测试。希望对你有帮助。

1.您以错误的方式生成测试数据。在makeInstaItem 中,您作为i 索引(以及0 值)传递,因此列表中的最后一项与InstaAdapter 创建的进度项具有相同的创建时间(0L)。:

private static InstaItem makeInstaItem(String prefix, int i, int dateСoefficient) {
    InstaItem item = new InstaItem();
    item.setLocation(new Location(prefix + "_location_" + i));
    item.setCreatedTime(i * 1000000L * dateСoefficient);
    item.setImages(makeInstaImages());
    item.setId(randomUuid());
    return item;
}

这应该改为例如:

private static InstaItem makeInstaItem(String prefix, int i, int dateСoefficient) {
    InstaItem item = new InstaItem();
    item.setLocation(new Location(prefix + "_location_" + i));
    item.setCreatedTime((i + 1) * 1000000L * dateСoefficient);
    item.setImages(makeInstaImages());
    item.setId(randomUuid());
    return item;
}

创建的测试列表作为对适配器的引用传递。添加进度项后,它在最后一项之前。在测试中,您试图匹配此进度条项目,就像它是常规位置项目一样,因此它会崩溃。在我的所有更改之后,它被添加为最后一项,并在测试循环到达它之前被删除

2。 在 InstaPresenter 中,当调用 loadMore 订阅时,您不会添加项目:

subscribe(x -> {
    Timber.d("load more - items fetched");
}

改成例如:

subscribe(items -> {
    if (!items.isEmpty()) {
        getMvpView().showFeed(items);
    }
    Timber.d("load more - items fetched");
}

3.您根本不需要延迟。保持实现简单,例如:

when(mockDataManager.getOldFeedItemsFromServer()).thenReturn(Observable.just(oldFeed.getInstaItems())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext(x -> {
                Timber.d("On next after delay");
                when(mockDataManager.subscribeOnFeedItemsChanges())
                        .thenReturn(Observable.just(oldFeed.getInstaItems()));
            }));

instaActivityActivityTestRule.launchActivity(null);

int position = 0;
for (InstaItem item : feed.getInstaItems()) {
    onView(withId(R.id.recycler_view))
                .perform(RecyclerViewActions.scrollToPosition(position));
    onView(withText(item.getLocation().getName()))
                .check(matches(isDisplayed()));
    onView(withText(InstaAdapter.dateFormat.format(new Date(item.getCreatedTime() * 1000))))
                .check(matches(isDisplayed()));
    position++;
}

for (InstaItem item : oldFeed.getInstaItems()) {
    onView(withId(R.id.recycler_view))
                .perform(RecyclerViewActions.scrollToPosition(position));
    onView(withText(item.getLocation().getName()))
                .check(matches(isDisplayed()));
    onView(withText(InstaAdapter.dateFormat.format(new Date(item.getCreatedTime() * 1000))))
                .check(matches(isDisplayed()));
    position++;
}
  1. 只是我个人的小建议。我宁愿用单元测试来测试演示者逻辑,也不愿与 UI 测试作斗争。

我记录了video 以显示在上述更改后它是如何工作的

【讨论】:

  • 我很抱歉这么说,但你的问题和代码有误。此外,您的第四个建议是错误的 - 如果您想完整地测试应用程序,您需要检查双方,而不仅仅是演示者。
  • @Divers 你能解释一下吗?如果您想查看进度,只需在代码中保留延迟,并在循环之间添加,1. 滚动到进度onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition(position));,然后,2. 等待加载集合中的第一项 onView(isRoot()).perform(waitText(oldFeed.getInstaItems().get(0).getLocation().getName(), 3000));。其中waitText 与您的waitId 相同,除了它通过文本匹配Matcher
【解决方案2】:

引用的项目不再使用最新版本的 Android Studio 和 JDK 11 构建。

在将 Espresso 与 RxJava 结合使用时,您可以使用 https://github.com/square/RxIdler 注册所需的 IdlingResources 并包装任何自定义 Rx 调度程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多