首页 > 解决方案 > android.support.test.espresso.InjectEventSecurityException 当我尝试使用 espresso 填充几个编辑文本时抛出

问题描述

这是我的代码:

onView(withId(R.id.first_name)).perform(
                typeText("John"), closeSoftKeyboard());
SystemClock.sleep(5000);
onView(withId(R.id.last_name)).perform(typeText("Smith"));

我从这里尝试了答案 Android :java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS 权限 ,但没有帮助。

以下是我使用的 expresso 版本:

// Android Test Support Libraries
    espressoRunnerVersion = "0.5"
    espressoRulesVersion = "0.5"
    espressoVersion = "2.2.2"

这是完整的堆栈跟踪:

android.support.test.espresso.PerformException: Error performing 'type text(Smith)' on view 'with id: church.life.app:id/last_name'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:84)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:81)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
at church.life.app.ui.profile.ProfileSignupTest.InvalidSignupTest(ProfileSignupTest.java:44)
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 android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
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:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2337)
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.support.test.espresso.base.InputManagerEventInjectionStrategy.injectKeyEvent(InputManagerEventInjectionStrategy.java:118)
at android.support.test.espresso.base.EventInjector.injectKeyEvent(EventInjector.java:84)
at android.support.test.espresso.base.UiControllerImpl$2.call(UiControllerImpl.java:195)
at android.support.test.espresso.base.UiControllerImpl$2.call(UiControllerImpl.java:192)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.hardware.input.IInputManager$Stub$Proxy.injectInputEvent(IInputManager.java:575)
at android.hardware.input.InputManager.injectInputEvent(InputManager.java:880)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.test.espresso.base.InputManagerEventInjectionStrategy.injectKeyEvent(InputManagerEventInjectionStrategy.java:109)
... 9 more
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.input.InputManagerService.injectInputEventInternal(InputManagerService.java:627)
at com.android.server.input.InputManagerService.injectInputEvent(InputManagerService.java:601)
at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:143)
at android.os.Binder.execTransact(Binder.java:752)

我尝试过睡眠和不睡眠,也尝试将 closeSoftKeyboard 移到执行操作之外。

标签: androidunit-testingandroid-espresso

解决方案


在行上放置一个断点

onView(withId(R.id.first_name)).perform(typeText("John"), closeSoftKeyboard());

当您越过那条线时,您是否看到自动完成弹出框?如果是这样,问题在于弹出框正在更改 EditText 的焦点,并且您当时正尝试执行 typeText 方法。

最简单的解决方案:将 typeText() 替换为 replaceText()。键盘不会显示,但如果您的集成测试没有在键盘显示期间测试 UI 放置,这将起作用。它应该看起来像:

onView(withId(R.id.first_name)).perform(replaceText("John"));

推荐阅读