android - 旋转按钮 Espresso 测试点击不工作

标签 android android-espresso

我在玩意式 Espresso 时发现我的其中一个按钮无法点击。我试图在屏幕上四处移动它以及所有使其被自动点击的东西,但失败了。然后我注意到它有一个 rotation=180。并通过删除它问题似乎得到了解决,但这是需要的。

这是我的测试代码:

@Test
public void orderUp() throws Exception {
    onView(ViewMatchers.withId(R.id.move_up)).perform(ViewActions.click());
    onView(withText("Up")).inRoot(withDecorView(not(is(menuActivityTestRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));
}

那么,如何使测试用例适用于旋转按钮?

编辑:“错误日志表明我认为没有找到 Toast”

android.support.test.espresso.NoMatchingRootException: Matcher 'with decor view not is <DecorView@1ec572[MainActivity]>' did not match any of the following roots: [Root{application-window-token=android.view.ViewRootImpl$W@449fd58, window-token=android.view.ViewRootImpl$W@449fd58, has-window-focus=true, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#81810100 pfl=0x20000 wanim=0x103046c needsMenuKey=2 naviIconColor=0}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#81810100 pfl=0x20000 wanim=0x103046c needsMenuKey=2 naviIconColor=0}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}}]
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1567)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90)
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.check(ViewInteraction.java:291)
at com.tabbara.mohammad.trailedsheetexample.ExampleInstrumentedTest.orderUp(ExampleInstrumentedTest.java:57)
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:1967)

最佳答案

ViewActions.click在屏幕上找到 View 的坐标,然后在坐标上执行/模拟点击。我不太确定,但似乎 setRotation 可能影响了坐标计算。另一种解决方案是创建自定义点击操作:

public static ViewAction forceClick() {
    return new ViewAction() {
        @Override public Matcher<View> getConstraints() {
            return allOf(isClickable(), isEnabled());
        }

        @Override public String getDescription() {
            return "force click";
        }

        @Override public void perform(UiController uiController, View view) {
            view.performClick();
            uiController.loopMainThreadUntilIdle();
        }
    };
}

此操作通过调用 performClick() 在 View 上执行点击操作,但未找到其坐标。但要确保 View 在应用程序中附加了点击监听器

onView(withId(R.id.move_up)).perform(forceClick());

关于android - 旋转按钮 Espresso 测试点击不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53232947/

相关文章:

android - Espresso 模糊 View 匹配异常 : How can I select a first horizontal Recyclerview from multiple horizontal Recyclerview

Android 无法在 fragment 中按 ID 找到 View

android - Android 中的谷歌地图聚类

安卓。如何从 Espresso 测试库中滑动 NavigationDrawer?

android - 如何一次性授予android所有权限?

Android Studio 2.2 预览版 Record Espresso Test 选项不可用

android - 使用 angularjs 和 cordova 上传相机图像

android - JAudioTagger 和 Android - 更改 mp3 中的值?

java - 如何只获取 JSON 中大数组的第一个元素?

java - 我应该何时(而不是如何)在 Android 上测试 UI 组件?