【发布时间】:2016-07-07 16:34:37
【问题描述】:
在 Espresso 测试中单击按钮出现问题。假设我有两个活动“Activity1”和“Activity2”。单击 Activity1 中的对话框 OK 按钮启动 Activity2,其中 Activity2 中的按钮无法单击。
// The current activity in testing
// .....
onView(withText(R.string.dialog_btn_ok)).perform(click()); // go to the second activity
// The button on the second activity
onView(withId(R.id.btnMP)).check(matches(isDisplayed())); // this is ok
onView(withId(R.id.btnMP)).perform(click()); // got error here
android.support.test.espresso.PerformException:执行错误 '单击'视图'与 id: ..........
原因:java.lang.RuntimeException: Action will not be executed 因为目标视图与以下一项或多项不匹配 约束:至少 90% 的视图区域显示到 用户。目标视图:“按钮{id=2131296390, res-name=btnMP, 可见性=可见,宽度=652,高度=160,有焦点=假, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=20.0, y=-16.0, text=修改参数, input-type=0, ime-target=false, has-links=false}"
当我用 perform(scrollTo()) 更改它时,会显示不同的错误。
// The button on the second activity
onView(withId(R.id.btnMP)).check(matches(isDisplayed())); // this is ok
onView(withId(R.id.btnMP)).perform(scrollTo(), click()); // got error here
android.support.test.espresso.PerformException:执行错误 '滚动到'视图'与 id ....
Caused by: java.lang.RuntimeException: Action will not be executed 因为 目标视图与以下一项或多项约束不匹配: (视图具有有效的可见性=VISIBLE 并且是 a 的后代:(是 可从类分配:类 android.widget.ScrollView 或者是 可从类分配:类 android.widget.HorizontalScrollView)) 目标视图:“按钮{id=2131296390, res-name=btnMP, 可见性=可见,宽度=652,高度=160,有焦点=假, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=20.0, y=-16.0, text=修改参数, input-type=0, ime-target=false, has-links=false}" 在
【问题讨论】:
-
正如错误消息所说,它可能至少没有显示整个视图区域的 90%。尝试使用
isCompletelyDisplayed()而不是isDisplayed(),或者更好的是,尝试使用isDisplayingAtLeast()通过90 作为百分比。 -
是的,
isCompletelyDisplayed()失败,错误为DefaultFailureHandler$AssertionFailedWithCauseError: 'at least 100 percent of the view's area is displayed to the user.' doesn't match the selected view.我该怎么办? -
您是否禁用了测试设备上的动画?请务必禁用它们:进入设置 > 开发人员选项并禁用“窗口动画比例”、“过渡动画比例”和“动画器持续时间比例”
-
其实
scrollTo()应该能解决问题吧? -
你解决了禁用动画的问题吗?关于
scollTo()我不确定问题是否相同。
标签: java android unit-testing testing android-espresso