I wrote a test that is supposed to wait for 15 seconds before evaluating a condition. It currently waits much shorter, and goes to the evaluation block right away, producing an error. The seconds parameter after: waitWithTimeout seems to be getting ignored.
My Test Code:
- (void)testAsyncEvent {
[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Button")]
performAction:grey_tap()];
// Wait for the main view controller to become the root view controller.
BOOL success = [[GREYCondition conditionWithName:@"Wait for label to change text"
block:^{
NSError *error;
[[EarlGrey selectElementWithMatcher:grey_text(@"Delayed Appearance")]
performAction:grey_tap()
error:&error];
if ([error.domain isEqual:kGREYInteractionErrorDomain] &&
error.code == kGREYInteractionElementNotFoundErrorCode) {
return NO;
}
return YES;
}] waitWithTimeout:15];
GREYAssertTrue(success, @"Label text should be changed after 5 seconds. ");
}
And here is the Button tap action:
- (IBAction)buttonPressed:(id)sender
{
self.titleLabel.text = @"Button Pressed";
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 4 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^(void)
{
dispatch_async(dispatch_get_main_queue(), ^{
self.titleLabel.text = @"Delayed Appearance";
});
});
}
titleLabel's text is supposed to change to "Delayed Appearance" after 4 seconds through dispatch Async. But the block in the test fires very quickly, although it's set to 15 seconds. (And fails because no element with such text is found).
I don't think that's the intention of the API. The API is for waiting on a condition to be met for X number of seconds. The seconds parameter in the timeout is how long should it really wait for the condition to be met. If the condition is still not met by then, it will timeout and return NO. If you really just want to wait for 15 seconds use:
[[NSRunLoop currentRunLoop] runUntilDate:]
And, since you're using dispatch_after, you can increase the tracked time of dispatch_after calls:
[[GREYConfiguration sharedInstance] setValue:@(5.0)
forConfigKey:kGREYConfigKeyDispatchAfterMaxTrackableDelay];
Just remember to reset it to default after you no longer wish to track dispatch after calls that are for 5.0 seconds in future.