Reputation: 3654
I have a TabLayout
with a ViewPager
, on page 3 there is a SwitchCompat
I want to perform a click()
on, but it does nothing.
@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
}
This is the error
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with checkbox state: is ' doesn't match the selected view. Expected: with checkbox state: is true
The SwitchCompat seems to have the following properties:
Got: "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-requested=false, is-selected=false, root-is-layout-requested=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}"
The error says that the Switch is not checked, but the problem seems to occur before, cause the Switch just stays unchecked, the perform(click())
seems to do nothing.
Upvotes: 2
Views: 4364
Reputation: 3239
I posted an alternate solution that involves creating a custom ViewAction to set switch state (or any Checkable for that matter) instead of using a click action. This ensures that your action is state independent.
https://stackoverflow.com/a/39650813/1947601
So, no matter if it was previously checked or not, you're guaranteed that it will be toggled to the state you expect.
Upvotes: 2