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

后端 未结 10 2022
心在旅途
心在旅途 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()
    

提交回复
热议问题