当前位置:  开发笔记 > 编程语言 > 正文

浓缩咖啡 - 单击对话框的按钮

如何解决《浓缩咖啡-单击对话框的按钮》经验,为你挑选了2个好方法。

我想测试Android 6的权限,但我找不到使用Espresso点击"允许"按钮的方法.有没有办法做到这一点?

在此输入图像描述

Espresso的版本是2.2.1.

考试:

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

行为:

我仍然使用Dialog接收冻结的屏幕(如屏幕截图所示).一直测试执行直到他完成.

输出:

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

Egor.. 7

嗯,它可以很简单

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

当然,这不是一个完美的解决方案,因此有两种方法可以使其更加健壮:使用hierarchyviewer分析应用程序以找到有关如何识别按钮的提示(例如内容描述),或者深入了解Android源代码提示(例如按钮的资源ID).

编辑

嗯,这不是那么简单.我写了一篇关于使用UiAutomator来完成这项工作的文章.



1> Egor..:

嗯,它可以很简单

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

当然,这不是一个完美的解决方案,因此有两种方法可以使其更加健壮:使用hierarchyviewer分析应用程序以找到有关如何识别按钮的提示(例如内容描述),或者深入了解Android源代码提示(例如按钮的资源ID).

编辑

嗯,这不是那么简单.我写了一篇关于使用UiAutomator来完成这项工作的文章.



2> piotrek1543..:

我看到你正试图测试应用程序权限.我认为用Espresso测试它可能是不可能的.您可能需要将此操作作为另一个名为的UI测试工具uiatomator.

uiatomator,Google制作的另一款出色工具可让您测试Android系统功能,如通知和屏幕锁定.您可以将它与Espresso测试框架一起使用.

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

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

和uiautomator文档,你可以在这里找到.

推荐阅读
围脖上的博博_771
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有