我浪费了两天时间尝试对全屏 Activity 执行点击操作。
重现步骤
- 使用全屏 Activity 代码模板在 android studio 中创建一个新项目
为执行点击的 Activity 编写 espresso 测试
public class FullscreenActivityTest extends ActivityInstrumentationTestCase2<FullscreenActivity> { public FullscreenActivityTest() { super(FullscreenActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); getActivity(); } public void testClickingOnLayout() { onView(withId(R.id.fullscreen_content)).perform(click()); }
- 在模拟器(从 Gingerbread 面包到 Lollipop 的任何 Android 版本)上运行此测试。
失败
android.support.test.espresso.PerformException:
Error performing 'click' on view 'with id: com.example.espressodefect:id/fullscreen_content'
...
Caused by: android.support.test.espresso.PerformException:
Error performing 'Send down montion event' on view 'unknown'.
...
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException:
Injecting to another application requires INJECT_EVENTS permission
完整堆栈是 here .
尝试过
我试过以下但没有成功:
- 模拟器上没有锁屏。 (还使用
WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
更新了 Activity - 关闭所有动画
- 没有屏幕键盘,也没有任何叠加在应用程序之上的东西。
- 还遵循说明 here在 developer.android.com
- JUnit3 和 JUnit4 样式 (AndroidJUnitRunner)
-
<uses-permission>
对于 INJECT_EVENTS
长话短说
在 espresso 测试中,如果我使用非全屏 Activity (例如用空白 Activity 创建的 Activity ),则执行操作效果很好。全屏上的相同操作失败。
我正在使用 Espresso 2.0 和支持库。
我在这里做错了什么?
更新
Espresso 开发人员确认这是缺陷。请看issue 140 .
最佳答案
此错误已在 Espresso 的最新版本中解决:
Espresso 2.2.2,Runner/Rules 0.5(2016 年 2 月 22 日,静默发布)- https://google.github.io/android-testing-support-library/downloads/release-notes/index.html
在我的项目中,我更改了/app/build.grandle 中的依赖项:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
到
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
在 AndroidStudio 中:Grandle Scripts => build.gradle 文件
关于android - Espresso - 对全屏 Activity 执行操作失败 - InjectEventSecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29329658/