单击带有 Espresso 的不完全可见的 imageButton
Click on not fully visible imageButton with Espresso
我有一个不完全可见的自定义 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.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 复制了 class 并在这个方法中从 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;
}
}
您必须滚动到之前的按钮:
onView(withId(R.id.button_id)).perform(scrollTo(), click());
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());
希望能成功。
我认为对此没有任何简单、优雅的解决方案。 90% 约束在 GeneralClickAction
中硬编码,class 是最终约束,因此我们无法覆盖 getConstraints
.
我会根据 GeneralClickAction
's code 编写您自己的 ViewAction,跳过 isDisplayingAtLeast
检查。
以上的 None 对我有用。这是一个自定义匹配器,它完全删除了该约束并允许您单击视图
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();
}
}
);
这帮助我在 运行 我的测试
时解决了按钮可见性问题
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)));
}
对于文本视图上的相同错误,我最终做了如下操作,上面发布的答案和其他 SO 问题提供了很大帮助。我确实尝试了 scrollTo
方法,但由于某种原因错误仍然存在。希望这对某人有所帮助。
onView(allOf(withId(R.id.recycler_view), isDisplayed())).perform(RecyclerViewActions
.actionOnItem(hasDescendant(withText("my text")), click()));
创建您自己的 ViewAction class 没有视图可见性限制:
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()
}
}
我有一个不完全可见的自定义 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.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 复制了 class 并在这个方法中从 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;
}
}
您必须滚动到之前的按钮:
onView(withId(R.id.button_id)).perform(scrollTo(), click());
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());
希望能成功。
我认为对此没有任何简单、优雅的解决方案。 90% 约束在 GeneralClickAction
中硬编码,class 是最终约束,因此我们无法覆盖 getConstraints
.
我会根据 GeneralClickAction
's code 编写您自己的 ViewAction,跳过 isDisplayingAtLeast
检查。
None 对我有用。这是一个自定义匹配器,它完全删除了该约束并允许您单击视图
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();
}
}
);
这帮助我在 运行 我的测试
时解决了按钮可见性问题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)));
}
对于文本视图上的相同错误,我最终做了如下操作,上面发布的答案和其他 SO 问题提供了很大帮助。我确实尝试了 scrollTo
方法,但由于某种原因错误仍然存在。希望这对某人有所帮助。
onView(allOf(withId(R.id.recycler_view), isDisplayed())).perform(RecyclerViewActions
.actionOnItem(hasDescendant(withText("my text")), click()));
创建您自己的 ViewAction class 没有视图可见性限制:
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()
}
}