咖啡 – 点击对话框的button

我想testingAndroid 6的权限,但是我没有find使用Espresso来点击“允许”button的方法。 有没有办法做到这一点?

在这里输入图像说明

Espresso的版本是2.2.1。

考试:

@Test public void acceptFirstPermission() throws Exception { onView(withText("ALLOW")).perform(click()); } 

行为:

我仍然收到对话框的冻结屏幕(如截图)。 testing正在执行,直到他完成。

输出:

 Running tests Test running started android.support.test.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)? 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.doPerform(ViewInteraction.java:115) at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87) at com.walletsaver.app.test.espresso.MarshmallowPermissionsTest.acceptFirstPermission(MarshmallowPermissionsTest.java:31) 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.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257) 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 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879) Finish 

Solutions Collecting From Web of "咖啡 – 点击对话框的button"

我看到您正在尝试testing应用权限。 我想用Espresso来testing它可能是不可能的。 您可能需要采取另一个称为uiatomator UItesting工具。

uiatomator是Google制作的另一个很棒的工具,可以让你testing你的Android系统function,比如通知和屏幕locking。 你可以使用它与浓咖啡testing框架。

如需查找更多信息,请阅读以下文章:

http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

和uiautomator文档,你可以在这里find。

那么,它可以如此简单

 onView(withText("Allow")).perform(click()); 

当然,这不是一个完美的解决scheme,所以有两种方法可以使它更健壮:或者使用hierarchyviewer来分析应用程序,find如何识别button的提示(例如内容描述),或者深入Android源代码提示(例如button的资源ID)。

编辑

那么,并不是那么简单。 我写了一篇关于使用UiAutomator来做这个工作的文章 。

在运行testing之前,下面的代码片段将授予Permissions。 希望它能解决你的问题:)

  @Before public void grantPhonePermission() { // In M+, trying to call a number will trigger a runtime dialog. Make sure // the permission is granted before running this test. // goo.gl/C8T4BU if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { getInstrumentation().getUiAutomation().executeShellCommand( "pm grant " + getTargetContext().getPackageName() + " android.permission.READ_PHONE_STATE" + " android.permission.PHONE"); } }