【问题标题】:Espresso RecyclerViewActions click destroy activityEspresso RecyclerViewActions 点击销毁活动
【发布时间】:2021-10-10 16:21:56
【问题描述】:

我想使用 espresso 来测试我的应用,并从 documentation 进行非常简单的测试。但是 RecyclerViewActions 有问题。它只会破坏活动。

import android.os.SystemClock;

import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.rules.ActivityScenarioRule;

import com.motivationmanager.R;

import org.junit.Rule;
import org.junit.Test;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;

public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> mActivityRule = new ActivityScenarioRule<>(
            MainActivity.class);

    @Test
    public void recyclerViewTest() {
        SystemClock.sleep(2000);
        onView(ViewMatchers.withId(R.id.tasks_progress_recyclerview))
                .perform(RecyclerViewActions.scrollToPosition (0))
                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
    //copy of the code above that not working because error occurs
        SystemClock.sleep(2000);
        onView(ViewMatchers.withId(R.id.tasks_progress_recyclerview))
                .perform(RecyclerViewActions.scrollToPosition (0))
                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));}}

日志

Testing started at 18:47 ...

10/10 18:47:39: Launching 'MainActivityTest' on 4  WVGA (Nexus S) API 30.
Install successfully finished in 1 s 408 ms.
Running tests

$ adb shell am instrument -w -m    -e debug true -e class 'ui.MainActivityTest' com.example.motivationmanager.test/androidx.test.runner.AndroidJUnitRunner
Connecting to com.example.motivationmanager.test
Waiting for application to start debug server
Waiting for application to come online: com.example.motivationmanager.test | com.example.motivationmanager
Connecting to com.example.motivationmanager.test
Waiting for application to start debug server
Connecting to com.example.motivationmanager
Waiting for application to start debug server
Connected to process 9973 on device 'emulator-5556'.
Connecting to com.example.motivationmanager.test
Waiting for application to start debug server
Connecting to com.example.motivationmanager
Connected to the target VM, address: 'localhost:50308', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/tivationmanage: Not late-enabling -Xcheck:jni (already on)
I/tivationmanage: Unquickening 12 vdex files!
W/tivationmanage: Unexpected CPU variant for X86 using defaults: x86
I/tivationmanage: The ClassLoaderContext is a special shared library.
I/chatty: uid=10153(com.example.motivationmanager) identical 1 line
I/tivationmanage: The ClassLoaderContext is a special shared library.
D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar
D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar
I/tivationmanage: The ClassLoaderContext is a special shared library.
D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar
    Returning zygote-cached class loader: /system/framework/android.test.base.jar
I/tivationmanage: The ClassLoaderContext is a special shared library.
I/chatty: uid=10153(com.example.motivationmanager) identical 2 lines
I/tivationmanage: The ClassLoaderContext is a special shared library.
W/tivationmanage: Accessing hidden method Landroid/app/Instrumentation;->execStartActivity(Landroid/content/Context;Landroid/os/IBinder;Landroid/os/IBinder;Landroid/app/Activity;Landroid/content/Intent;ILandroid/os/Bundle;)Landroid/app/Instrumentation$ActivityResult; (greylist, linking, allowed)
    Accessing hidden method Landroid/app/Instrumentation;->execStartActivity(Landroid/content/Context;Landroid/os/IBinder;Landroid/os/IBinder;Ljava/lang/String;Landroid/content/Intent;ILandroid/os/Bundle;)Landroid/app/Instrumentation$ActivityResult; (greylist, linking, allowed)
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/MultiDex: VM with version 2.1.0 has multidex support
    Installing application
    VM has multidex support, MultiDex support library is disabled.
I/MonitoringInstr: Instrumentation started!
I/AndroidJUnitRunner: Waiting for debugger to connect...
I/System.out: Sending WAIT chunk
I/System.out: Debugger has connected
    waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/chatty: uid=10153(com.example.motivationmanager) identical 3 lines
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1300)
I/AndroidJUnitRunner: Debugger connected.
V/TestEventClient: No service name argument was given (testDiscoveryService, testRunEventService or orchestratorService)
I/MonitoringInstr: No JSBridge.
D/InfraTrack: Tracking disabled due to lack of internet permissions
I/UsageTrackerFacilitator: Usage tracking disabled
D/TestExecutor: Adding listener androidx.test.internal.runner.listener.LogRunListener
D/TestExecutor: Adding listener androidx.test.internal.runner.listener.InstrumentationResultPrinter
    Adding listener androidx.test.internal.runner.listener.ActivityFinisherRunListener
I/TestRunner: run started: 1 tests
I/TestRunner: started: recyclerViewTest(ui.MainActivityTest)

Started running tests

W/Settings: Setting always_finish_activities has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
D/LifecycleMonitor: Lifecycle status change: ui.MainActivity@197a684 in: PRE_ON_CREATE
V/ActivityScenario: Activity lifecycle changed event received but ignored because the reported transition was not ON_CREATE while the last known transition was PRE_ON_CREATE
W/tivationmanage: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/tivationmanage: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/LifecycleMonitor: Lifecycle status change: ui.MainActivity@197a684 in: CREATED
V/ActivityScenario: Update currentActivityStage to CREATED, currentActivity=ui.MainActivity@197a684
D/LifecycleMonitor: Lifecycle status change: ui.MainActivity@197a684 in: STARTED
V/ActivityScenario: Update currentActivityStage to STARTED, currentActivity=ui.MainActivity@197a684
D/LifecycleMonitor: Lifecycle status change: ui.MainActivity@197a684 in: RESUMED
V/ActivityScenario: Update currentActivityStage to RESUMED, currentActivity=ui.MainActivity@197a684
D/HostConnection: HostConnection::get() New Host Connection established 0xf6c223d0, tid 10012
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/EGL_emulation: eglCreateContext: 0xf6c22590: maj 3 min 0 rcv 3
D/EGL_emulation: eglMakeCurrent: 0xf6c22590: ver 3 0 (tinfo 0xc54bac70) (first time)
I/Gralloc4: mapper 4.x is not supported
D/HostConnection: createUnique: call
    HostConnection::get() New Host Connection established 0xf6c22050, tid 10012
D/goldfish-address-space: allocate: Ask for block of size 0x100
    allocate: ioctl allocate returned offset 0x3fbffe000 size 0x2000
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 
D/EventInjectionStrategy: Creating injection strategy with input manager.
W/tivationmanage: Accessing hidden method Landroid/hardware/input/InputManager;->getInstance()Landroid/hardware/input/InputManager; (greylist, reflection, allowed)
W/tivationmanage: Accessing hidden method Landroid/hardware/input/InputManager;->injectInputEvent(Landroid/view/InputEvent;I)Z (greylist, reflection, allowed)
    Accessing hidden field Landroid/hardware/input/InputManager;->INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH:I (greylist, reflection, allowed)
W/tivationmanage: Accessing hidden method Landroid/os/MessageQueue;->next()Landroid/os/Message; (greylist, reflection, allowed)
    Accessing hidden field Landroid/os/MessageQueue;->mMessages:Landroid/os/Message; (greylist, reflection, allowed)
    Accessing hidden method Landroid/os/Message;->recycleUnchecked()V (greylist, reflection, allowed)
W/tivationmanage: Accessing hidden method Landroid/view/WindowManagerGlobal;->getInstance()Landroid/view/WindowManagerGlobal; (greylist, reflection, allowed)
    Accessing hidden field Landroid/view/WindowManagerGlobal;->mViews:Ljava/util/ArrayList; (greylist, reflection, allowed)
    Accessing hidden field Landroid/view/WindowManagerGlobal;->mParams:Ljava/util/ArrayList; (greylist, reflection, allowed)
I/ViewInteraction: Performing 'scroll RecyclerView to position: 0' action on view view.getId() is <2131296529/com.example.motivationmanager:id/tasks_progress_recyclerview>
W/tivationmanage: Accessing hidden method Landroid/view/ViewConfiguration;->getDoubleTapMinTime()I (greylist, reflection, allowed)
I/ViewInteraction: Performing 'actionOnItemAtPosition performing ViewAction: single click on item at position: 0' action on view view.getId() is <2131296529/com.example.motivationmanager:id/tasks_progress_recyclerview>
D/LifecycleMonitor: Lifecycle status change: ui.MainActivity@197a684 in: PAUSED
V/ActivityScenario: Update currentActivityStage to PAUSED, currentActivity=ui.MainActivity@197a684
D/LifecycleMonitor: Lifecycle status change: ui.MainActivity@197a684 in: STOPPED
V/ActivityScenario: Update currentActivityStage to STOPPED, currentActivity=ui.MainActivity@197a684
D/LifecycleMonitor: Lifecycle status change: ui.MainActivity@197a684 in: DESTROYED
V/ActivityScenario: Update currentActivityStage to DESTROYED, currentActivity=null
W/RootViewPicker: No activities found - waiting: 10ms for one to appear.
W/RootViewPicker: No activities found - waiting: 50ms for one to appear.
W/RootViewPicker: No activities found - waiting: 150ms for one to appear.
W/RootViewPicker: No activities found - waiting: 250ms for one to appear.
E/TestRunner: failed: recyclerViewTest(ui.MainActivityTest)
    ----- begin exception -----
E/TestRunner: androidx.test.espresso.NoActivityResumedException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
        at dalvik.system.VMStack.getThreadStackTrace(Native Method)
        at java.lang.Thread.getStackTrace(Thread.java:1736)
        at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:12)
        at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:7)
        at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:8)
        at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:11)
        at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:4)
        at ui.MainActivityTest.recyclerViewTest(MainActivityTest.java:58)
        at java.lang.reflect.Method.invoke(Native Method)
        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 org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
        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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
        at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:444)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2205)
    ----- end exception -----
I/TestRunner: finished: recyclerViewTest(ui.MainActivityTest)

androidx.test.espresso.NoActivityResumedException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
    at dalvik.system.VMStack.getThreadStackTrace(Native Method)
    at java.lang.Thread.getStackTrace(Thread.java:1736)
    at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:12)
    at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:7)
    at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:8)
    at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:11)
    at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:4)
    at ui.MainActivityTest.recyclerViewTest(MainActivityTest.java:58)

I/TestRunner: run finished: 1 tests, 1 failed, 0 ignored
Tests ran to completion.

I/MonitoringInstr: waitForActivitiesToComplete() took: 0ms
Disconnected from the target VM, address: 'localhost:50308', transport: 'socket'

在“RecyclerViewActions.actionOnItemAtPosition(0, click()”活动状态为 DESTROYED 并且代码副本出现错误“androidx.test.espresso.NoActivityResumedException: NoActivityResumedException: No activity found.”之后如何点击可以破坏活动?我该如何修复它? 我在 recyclerview 中有 3 个或更多项目。我从 recommendation 关闭了动画。我使用 3.4.0 版本的浓缩咖啡。不知道该怎么做。

【问题讨论】:

  • MainActivity 是否可能正在显示一个对话框或打开另一个活动,例如用于登录或身份验证的活动?甚至可能是权限对话框?在这些条件下,Espresso 绝对会抛出同样的错误,因为正在测试的 Activity 在启动时会被推入暂停状态,直到有问题的 Dialog/Activity 关闭
  • @Cruceo 没有。这是一个简单的列表。单击元素打开另一个包含该元素详细信息的活动。没有显示对话框或权限。我用手做,它工作。有趣的是,我用 doubleClick() 尝试了这个示例,其中一行日志看起来像“引起:java.lang.SecurityException:注入另一个应用程序需要 INJECT_EVENTS 权限”。但我找不到对我有帮助的东西。

标签: java android android-recyclerview click android-espresso


【解决方案1】:

解决了。问题出在我的点击监听器代码中,它依赖于 getCallingActivity()

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多