Espresso: return boolean if view exists

前端 未结 8 1731
陌清茗
陌清茗 2020-12-09 01:23

I am trying to check to see if a view is displayed with Espresso. Here is some pseudo code to show what I am trying:

if (!Espresso.onView(withId(R.id.someID)         


        
8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 01:38

    It's been some time since this issue was stated, but as it is one of the top hit on Google when searching for ways to make sure a view is present, before doing any actions on it in Espresso, I would like to share my very basic way of handling this.

    1: Start out by writing an extension to ViewInteraction:

    fun ViewInteraction.exists(): Boolean {
    val viewExists = AtomicReference()
    
    this.perform(object : ViewAction {
        override fun perform(uiController: UiController?, view: View?) {
            viewExists.set(view != null)
        }
    
        override fun getConstraints(): Matcher? {
            return Matchers.allOf(ViewMatchers.withEffectiveVisibility(
                    ViewMatchers.Visibility.VISIBLE),
                    ViewMatchers.isAssignableFrom(View::class.java))
        }
    
        override fun getDescription(): String {
            return "check if view exists"
        }
    })
    return viewExists.get()
    }
    

    2: Create a simple help method in your base class (to be used in all test classes):

    fun viewExists(id: Int): Boolean {
        return try {
            onView(withId(id)).exists()
        } catch (e: RuntimeException) {
            false
        }
    }
    

    With this you either get true or false from onView(withId(id)).exists(), or safely catch the RuntimeException and return false.

    Normally a simple check to .exists() would be sufficient, but in some cases, like when you are deleting ListView items until non is left -> when the last item is deleted, the ListView might no longer be present, then an Exception is thrown when trying to check if it exists.

    3: With the above implementation, it is safe to check if any view exists, since the RuntimeException is handled nicely behind the scene:

    if(viewExists(R.id.something)) {
        //do something
    }
    //do something else
    

提交回复
热议问题