旋转按钮Espresso Test单击不起作用

时间:2018-11-09 20:37:27

标签: android android-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)

1 个答案:

答案 0 :(得分:1)

ViewActions.click在屏幕上查找视图的坐标,然后在坐标上执行/模拟点击。我不确定,但似乎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()来执行对视图的单击,而没有找到其坐标。但是请确保该视图在应用程序中附加了点击侦听器

onView(withId(R.id.move_up)).perform(forceClick());
相关问题