Espresso: click() on SwitchCompat 不起作用



我有一个ViewPagerTabLayout,在第 3 页上有一个SwitchCompat我想对其执行click(),但它什么也没做。

@Test
public void checkSummaryPageUpdates_worksCorrect() {
onView(withId(R.id.view_pager)).perform(swipeLeft());
onView(withId(R.id.view_pager)).perform(swipeLeft());
onView(withId(R.id.gs_switch)).check(matches(isDisplayed()));
onView(withId(R.id.gs_switch)).perform(click());
onView(withId(R.id.gs_switch)).check(matches(isChecked())); //  AssertionFailed
}

这是错误

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError:"带有复选框状态:is"与所选视图不匹配。 预期:复选框状态:为 true

SwitchCompat似乎具有以下属性:

得到: "SwitchCompat{id=2131689643, res-name=gs_switch, visibility=VISIBLE, width=996, height=100, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-request=false, is-selected=false, root-is-layout-request=false, has-input-connection=false,

x=0.0, y=0.0, text=GLÜCKSSPIRALE +5,00 €, input-type=0, ime-target=false, has-links=false, is-checked=false}">

错误说未检查开关,但问题似乎之前发生过,因为开关只是保持未选中状态,perform(click())似乎什么也没做。

我发布了一个替代解决方案,该解决方案涉及创建自定义 ViewAction 来设置开关状态(或任何与此相关的可检查状态),而不是使用单击操作。这可确保操作与状态无关。

https://stackoverflow.com/a/39650813/1947601

因此,无论之前是否检查过它,您都可以保证它将被切换到您期望的状态。

最新更新