Espresso - How to check if one of the view is displayed

后端 未结 10 2001
心在旅途
心在旅途 2021-02-03 17:22

In my test, after one action, there are two possible views which can appear and both of them are correct. How can I check if one of the view is displayed. For a single view I ca

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-03 17:39

    final AtomicBoolean view1Displayed = new AtomicBoolean(true);
    Espresso.onView(ViewMatchers.withId(viewId1)).inRoot(RootMatchers.withDecorView(Matchers.is(intentsTestRule.getActivity().getWindow().getDecorView()))).withFailureHandler(new FailureHandler() {
            @Override
            public void handle(Throwable error, Matcher viewMatcher) {
                view1Displayed.set(false);
            }
        }).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    
    if (view1Displayed.get()) {
            try {
                Espresso.onView(ViewMatchers.withId(viewId2)).inRoot(RootMatchers.withDecorView(Matchers.is(intentsTestRule.getActivity().getWindow().getDecorView()))).check(ViewAssertions.matches(Matchers.not(ViewMatchers.isDisplayed())));
            } catch (NoMatchingViewException ignore) {
            }
        } else {
            Espresso.onView(ViewMatchers.withId(viewId2)).inRoot(RootMatchers.withDecorView(Matchers.is(intentsTestRule.getActivity().getWindow().getDecorView()))).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
        }
    

提交回复
热议问题