Search code examples
earlgrey

Test runs fine on iPhone 6s Plus but fails on iPhone 5s


I have a test that selects a particular feed in my app. It works fine on iPhone 6s but fails on iPhone 5s with element not found error. Upon further investigation it seems like the the feed is missing from the view hierarchy. I came up with a workaround which is to something like:

if (running on iPhone 5s) {
  // Scroll down by 50 units.
  // Then locate the feed and check that it's visible.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"feed10")] 
      assertWithMatcher:grey_sufficientlyVisible()];
}

Though this seems fine, I'd like to know if there's a better way to conditionally scroll if element isn't found on the screen.


Solution

  • EarlGrey provides the usingSearchAction:onElementWithMatcher: api to find elements that need to be scrolled to in order to find them. Since you're using grey_sufficientlyVisible(), it is required the the element is visible on the screen for the assertWithMatcher check to pass. From their faq, you can change your assertion to be the following:

    if (running on iPhone 5s) {
      [[EarlGrey selectElementWithMatcher:matcher]
                      usingSearchAction:grey_scrollInDirection(kGREYDirectionDown, 50)
                   onElementWithMatcher:grey_accessibilityID(@"feed10")
                      assertWithMatcher:grey_sufficientlyVisible()];
    }
    

    FYI - you use kGREYDirectionDown since it denotes the direction of the change of the viewport and not the direction of the swipe done for the scroll.