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

后端 未结 10 1966
心在旅途
心在旅途 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条回答
  • 2021-02-03 17:30

    For the ones looking to check the visibility status for a view; here are some utility functions I use.

    fun ViewInteraction.isGone() = getViewAssertion(ViewMatchers.Visibility.GONE)
    
    fun ViewInteraction.isVisible() = getViewAssertion(ViewMatchers.Visibility.VISIBLE)
    
    fun ViewInteraction.isInvisible() = getViewAssertion(ViewMatchers.Visibility.INVISIBLE)
    
    private fun getViewAssertion(visibility: ViewMatchers.Visibility): ViewAssertion? {
        return ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(visibility))
    }
    

    And can be used as follows

    onView(withId(R.id.progressBar)).isVisible()
    onView(withId(R.id.progressBar)).isGone()
    
    0 讨论(0)
  • 2021-02-03 17:34

    You can use Matchers.anyOf to check if any of the two views are displayed:

    onView(
       anyOf(withId(R.id.view_1), withId(R.id.view_2)) 
    ).check(matches(isDisplayed()));
    
    0 讨论(0)
  • 2021-02-03 17:34

    When I face this situation I generally split into multiple tests. One test sets the conditions for view #1 to be displayed and the other test sets the conditions for view #2 to be displayed.

    But let's say that you can't really control the conditions. For example, what if it depends on a random number or it depends on a third-party resource such as a calculation on a server? In that case, I usually solve the problem mocking. That way I can control the conditions so I know exactly which view to expect. I use Dependency Injection to set the mock I need for each test.

    0 讨论(0)
  • 2021-02-03 17:37

    Utility class which allows to check if view is visible, gone or invisible:

    public class ExtraAssertions {
        public static ViewAssertion isVisible() {
            return new ViewAssertion() {
                public void check(View view, NoMatchingViewException noView) {
                    assertThat(view, new VisibilityMatcher(View.VISIBLE));
                }
            };
        }
    
        public static ViewAssertion isGone() {
            return new ViewAssertion() {
                public void check(View view, NoMatchingViewException noView) {
                    assertThat(view, new VisibilityMatcher(View.GONE));
                }
            };
        }
    
        public static ViewAssertion isInvisible() {
            return new ViewAssertion() {
                public void check(View view, NoMatchingViewException noView) {
                    assertThat(view, new VisibilityMatcher(View.INVISIBLE));
                }
            };
        }
    
        private static class VisibilityMatcher extends BaseMatcher<View> {
    
            private int visibility;
    
            public VisibilityMatcher(int visibility) {
                this.visibility = visibility;
            }
    
            @Override public void describeTo(Description description) {
                String visibilityName;
                if (visibility == View.GONE) visibilityName = "GONE";
                else if (visibility == View.VISIBLE) visibilityName = "VISIBLE";
                else visibilityName = "INVISIBLE";
                description.appendText("View visibility must has equals " + visibilityName);
            }
    
            @Override public boolean matches(Object o) {
    
                if (o == null) {
                    if (visibility == View.GONE || visibility == View.INVISIBLE) return true;
                    else if (visibility == View.VISIBLE) return false;
                }
    
                if (!(o instanceof View))
                    throw new IllegalArgumentException("Object must be instance of View. Object is instance of " + o);
                return ((View) o).getVisibility() == visibility;
            }
        }
    }
    

    And usage could look like this:

    onView(withId(R.id.text_message)).check(isVisible());
    

    Another view assertion which could help to check extra visibility properties of a view and its parents: it checks visibility, isAttachedToWindow, alpha:

    class IsVisible : ViewAssertion {
        override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
            ViewMatchers.assertThat(
                    "View is not visible. " +
                            "visibility: ${view.visibility}, " +
                            "isAttachedToWindow: ${view.isAttachedToWindow}, " +
                            "alpha: ${view.alpha}",
                    true, `is`(isViewTreeVisible(view)))
        }
    
        private fun isViewTreeVisible(view: View?): Boolean {
            return if (view != null) {
                val viewVisible = view.isAttachedToWindow && view.visibility == View.VISIBLE && view.alpha == 1.0f
    
                if (view.parent !is View) viewVisible
                else viewVisible && isViewTreeVisible(view.parent as View)
            } else {
                true
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-03 17:38

    It's possible to catch the exceptions raised by Espresso like this:

    If you want to test if a view is in hierarchy:

    try {
        onView(withText("Button")).perform(click());
        // View is in hierarchy
    
    } catch (NoMatchingViewException e) {
        // View is not in hierarchy
    }
    

    This exception will be thrown if the view is not in the hierarchy.

    Sometimes the view can be in the hierarchy, but we need to test if it is displayed, so there is another exception for assertions, like this:

    try {
        onView(withText("Button")).check(matches(isDisplayed()));
        // View is displayed
    } catch (AssertionFailedError e) {
        // View not displayed
    }
    
    0 讨论(0)
  • 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<View> 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()));
        }
    
    0 讨论(0)
提交回复
热议问题