1

Currently I wait for an element to appear like this:

let populated = GREYCondition(name: "Wait for UICollectionView to populate", block: { _ in
    var errorOrNil: NSError?

    EarlGrey().selectElementWithMatcher(collectionViewMatcher)
              .assertWithMatcher(grey_notNil(), error: &errorOrNil)

    let success = (errorOrNil == nil)
    return success
}).waitWithTimeout(20.0)

GREYAssertTrue(populated, reason: "Failed to populate UICollectionView in 20 seconds")

Which polls constantly for 20 seconds for collection view to populate. Is there a better, non-polling way of achieving this?

khandpur
  • 705
  • 3
  • 12

2 Answers2

1

EarlGrey recommends using its synchronization for waiting for elements rather than using sleeps or conditional checks like waits wherever possible.

EarlGrey has a variable kGREYConfigKeyInteractionTimeoutDuration value in GREYConfiguration that is set to 30 seconds and states -

 *  Configuration that holds timeout duration (in seconds) for action and assertions. Actions or
 *  assertions that are not scheduled within this time will fail due to timeout.

Since you're waiting for 20 seconds for your check, you can instead simply change it to -

EarlGrey().selectElementWithMatcher(collectionViewMatcher)
          .assertWithMatcher(grey_notNil(), error: &errorOrNil)

and it'll be populated without a timeout.

gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • 2
    so I needed to track the background queue on which we perform fetch request and earlgrey was able to synchronize with it. I see that dispatch queue idling resource uses idlingresource protocol. any reason why that hasn't been mentioned in docs? Seems like we could wrap our resources in an IdlingResource to achieve a more robustness in our tests. – khandpur Aug 01 '16 at 17:56
0

I like to connect Earl Grey with basic XCTest and I've come up with this simple solution to problem of waiting for elements:

app.webViews.buttons["logout()"].waitForExistence(timeout: 5)
app.webViews.buttons["logout()"].tap()

LSWarss
  • 53
  • 8