Skip to content

Commit

Permalink
Refactor: Simplify error handling in GREYElementInteraction matchedEl…
Browse files Browse the repository at this point in the history
…ementsWithTimeout:error:
  • Loading branch information
axi0mX committed Aug 22, 2016
1 parent d9fa94d commit fb4da02
Showing 1 changed file with 39 additions and 49 deletions.
88 changes: 39 additions & 49 deletions EarlGrey/Core/GREYElementInteraction.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,66 +110,56 @@ - (NSArray *)matchedElementsWithTimeout:(CFTimeInterval)timeout error:(__strong
GREYElementFinder *elementFinder = [[GREYElementFinder alloc] initWithMatcher:elementMatcher];
NSError *searchActionError = nil;
CFTimeInterval timeoutTime = CACurrentMediaTime() + timeout;
BOOL timedOut = NO;

while (YES) {
if ([[UIApplication sharedApplication] _isSpringBoardShowingAnAlert] &&
![[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.apple.springboard"]) {
NSString *description = @"Interaction failed because a system alert view is displayed.";
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionSystemAlertViewIsDisplayedErrorCode
userInfo:@{ NSLocalizedDescriptionKey : description }];
break;
}
@autoreleasepool {
if ([[UIApplication sharedApplication] _isSpringBoardShowingAnAlert] &&
![[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.apple.springboard"]) {
NSString *description = @"Interaction failed because a system alert view is displayed.";
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionSystemAlertViewIsDisplayedErrorCode
userInfo:@{ NSLocalizedDescriptionKey : description }];
return nil;
}
// Find the element in the current UI hierarchy.
NSArray *elements = [elementFinder elementsMatchedInProvider:entireRootHierarchyProvider];
if (elements.count > 0) {
return elements;
} else if (!_searchAction) {
if (error) {
NSString *desc =
@"Interaction cannot continue because the desired element was not found.";
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSLocalizedDescriptionKey : desc }];
}
return nil;
} else if (searchActionError) {
break;
}

CFTimeInterval currentTime = CACurrentMediaTime();
if (currentTime >= timeoutTime) {
timedOut = YES;
break;
}
// Keep applying search action.
id<GREYInteraction> interaction =
[[GREYElementInteraction alloc] initWithElementMatcher:_searchActionElementMatcher];
// Don't fail if this interaction error's out. It might still have revealed the element
// we're looking for.
[interaction performAction:_searchAction error:&searchActionError];
}
if (!_searchAction) {
NSString *description = @"Interaction failed because no UI element was matched.";
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSLocalizedDescriptionKey : description }];
break;
} else if (searchActionError) {
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSUnderlyingErrorKey : searchActionError }];
break;
} else if (CACurrentMediaTime() >= timeoutTime) {
NSString *description =
[NSString stringWithFormat:@"Interaction timed out after %g seconds while searching for "
@"an element.", timeout];
NSError *timeoutError =
[NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionTimeoutErrorCode
userInfo:@{ NSLocalizedDescriptionKey : description }];
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSUnderlyingErrorKey : timeoutError }];
break;
}
@autoreleasepool {
// Keep applying search action. Don't fail if this interaction errors out. It might still have
// found the element we are looking for.
[[[GREYElementInteraction alloc] initWithElementMatcher:_searchActionElementMatcher]
performAction:_searchAction error:&searchActionError];
// Drain here so that search at the beginning of the loop looks at stable UI.
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
}
}

NSDictionary *userInfo = nil;
if (searchActionError) {
userInfo = @{ NSUnderlyingErrorKey : searchActionError };
} else if (timedOut) {
CFTimeInterval interactionTimeout =
GREY_CONFIG_DOUBLE(kGREYConfigKeyInteractionTimeoutDuration);
NSString *desc = [NSString stringWithFormat:@"Interaction timed out after %g seconds while "
@"searching for element.", interactionTimeout];
NSError *timeoutError = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionTimeoutErrorCode
userInfo:@{ NSLocalizedDescriptionKey : desc }];
userInfo = @{ NSUnderlyingErrorKey : timeoutError };
}
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:userInfo];
return nil;
}

Expand Down

0 comments on commit fb4da02

Please sign in to comment.