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

后端 未结 10 1967
心在旅途
心在旅途 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:43

    One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.

    Another way but more accurate (I have not tried) is to check for the rectangular bounds of the View. Not so simple.

    Is that clear enough? cannot give you specific examples since you did not post code.

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

    I researched Espresso a bit, and I found this @ Espresso Samples.

    1. Search text "Asserting that a view is not displayed". It says "The above approach works if the view is still part of the hierarchy." So I think your code should work but you need to use ViewAssertions also. Using your code, perhaps do this:

      if (ViewAssertions.doesNotExist()) == null) {
         return;
      }
      onMyPageOne.check(matches(isDisplayed()));
      
    2. Another technique is check for UI existence. Search for text "Asserting that a view is not present". Using your code, my best suggestion is:

      onMyPageOne.check(doesNotExist());

    Note: This calls doesNotExist method.

    Their sample code is: onView(withId(R.id.bottom_left)).check(doesNotExist());

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

    There are two cases here that you could be trying to cover. The first is if you are checking if the view "is displayed on the screen to the user" in which case you would use isDisplayed()

    onView(matcher).check(matches(isDisplayed()));
    

    or the negation

    onView(matcher).check(matches(not(isDisplayed())));
    

    The other case is if you are checking if the view is visible but not necessarily displayed on the screen (ie. an item in a scrollview). For this you can use withEffectiveVisibility(Visibility)

    onView(matcher).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
    
    0 讨论(0)
  • 2021-02-03 17:54

    The problem is that all assertoin() and check() methods return Assertion that stops test flow if failed.

    0 讨论(0)
提交回复
热议问题