代码之家  ›  专栏  ›  技术社区  ›  HowieH

单击不完全可见的图像按钮加浓咖啡

  •  34
  • HowieH  · 技术社区  · 11 年前

    我有个习惯 ImageButton 按设计,它是不完全可见的,因此当我执行单击操作时,会出现以下错误:

    android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: test.com.myproject.app:id/navigationButtonProfile'.
    Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
    at least 90 percent of the view's area is displayed to the user.
    at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5356)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)
    

    按钮的一小部分在屏幕外(即,它在顶部裁剪),可能有12%的按钮在屏幕外。这是设计的,不可能滚动或执行任何视图操作以使其可见。 有人知道如何克服这个90%的限制吗?

    解决方案: 我按照建议创建了自己的点击操作,而且效果很好。 我从 Google Espresso 并且在该方法中从90变为75:

        @Override
        @SuppressWarnings("unchecked")
        public Matcher<View> getConstraints() {
            Matcher<View> standardConstraint = isDisplayingAtLeast(75);
            if (rollbackAction.isPresent()) {
                return allOf(standardConstraint, rollbackAction.get().getConstraints());
            } else {
                return standardConstraint;
            }
        }
    
    7 回复  |  直到 10 年前
        1
  •  67
  •   Lavekush Agrawal rekire    8 年前

    以上这些都不适用于我。这里有一个自定义匹配器,它可以完全删除该约束,并允许您单击视图

     onView(withId(yourID)).check(matches(allOf( isEnabled(), isClickable()))).perform(
                new ViewAction() {
                    @Override
                    public Matcher<View> getConstraints() {
                        return ViewMatchers.isEnabled(); // no constraints, they are checked above
                    }
    
                    @Override
                    public String getDescription() {
                        return "click plus button";
                    }
    
                    @Override
                    public void perform(UiController uiController, View view) {
                        view.performClick();
                    }
                }
        );
    
        2
  •  14
  •   Alexander Pacha    10 年前

    我不认为有任何简单、优雅的解决方案。90%的约束是硬编码的 GeneralClickAction ,并且该类是最终的,因此我们不能重写 getConstraints .

    我会根据 GeneralClickAction 's code ,跳过 isDisplayingAtLeast 检查

        3
  •  12
  •   denys    11 年前

    您必须滚动到以下按钮:

    onView(withId(R.id.button_id)).perform(scrollTo(), click());
    
        4
  •  9
  •   pergy Javier    9 年前

    这有助于我在运行测试时解决按钮可见性问题

    public static ViewAction handleConstraints(final ViewAction action, final Matcher<View> constraints)
    {
        return new ViewAction()
        {
            @Override
            public Matcher<View> getConstraints()
            {
                return constraints;
            }
    
            @Override
            public String getDescription()
            {
                return action.getDescription();
            }
    
            @Override
            public void perform(UiController uiController, View view)
            {
                action.perform(uiController, view);
            }
        };
    }
    
    public void clickbutton()
    {
        onView(withId(r.id.button)).perform(handleConstraints(click(), isDisplayingAtLeast(65)));
    }
    
        5
  •  7
  •   Anna    11 年前

    默认点击Espresso需要视图可见性 > 90% .你觉得创建自己的 click ViewAction? 这样地。。。

    public final class MyViewActions {
       public static ViewAction click() {
          return new GeneralClickAction(SafeTap.SINGLE, GeneralLocation.CENTER, Press.FINGER);
        }
    }
    

    单击将单击视图的中心。

    然后可以执行如下单击: onView(withId(....)).perform(MyViewActions.click());

    我希望它能奏效。

        6
  •  3
  •   Philipp E.    4 年前

    创建自己的ViewAction类,不受视图可见性约束:

     public static ViewAction myClick() {
    
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return ViewMatchers.isEnabled(); // no constraints, they are checked above
            }
    
            @Override
            public String getDescription() {
                return null;
            }
    
            @Override
            public void perform(UiController uiController, View view) {
                view.performClick();
            }
        };
    
    }
    

    然后使用它:

    onView(withId(R.id.your_view_id)).perform(myClick());
    

    Kotlin版本:

    fun myClick(): ViewAction = object : ViewAction {
      override fun getConstraints(): Matcher<View> = ViewMatchers.isEnabled() // no constraints, they are checked above
    
      override fun getDescription(): String = ""
    
      override fun perform(uiController: UiController, view: View) {
        view.performClick()
      }
    }
    
        7
  •  0
  •   user2984447    7 年前

    对于我最后在文本视图上犯的同样错误,上面发布的答案和其他SO问题非常有用。我试过了 scrollTo 方法,但由于某种原因,错误仍然存在。希望这对某人有所帮助。

            onView(allOf(withId(R.id.recycler_view), isDisplayed())).perform(RecyclerViewActions
                    .actionOnItem(hasDescendant(withText("my text")), click()));