Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed acknowledgeSystemAlert on iOS 13.1 #1126

Merged
merged 2 commits into from
Oct 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Classes/UIAutomationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ - (void)tap;
- (void)tapWithOptions:(NSDictionary *)options;
- (NSNumber *)pid;
- (UIAXElement *)uiaxElement;
- (NSArray<UIAElement *> *)elements;
@end

@interface UIAElementArray : NSArray
Expand All @@ -34,6 +35,7 @@ - (BOOL)isVisible;

@interface UIAApplication : UIAElement
- (UIAAlert *)alert;
- (NSArray<UIAElement *> *)windows;
- (NSString *)name;
- (id)appItemScrollView;
@end
Expand Down Expand Up @@ -114,8 +116,26 @@ + (void)deactivateAppForDuration:(NSNumber *)duration {
[[self sharedHelper] deactivateAppForDuration:duration];
}

- (UIAAlert *)currentSystemAlert
{
UIAApplication *application = [[self target] frontMostApp];
UIAAlert *alert = application.alert;

#ifdef __IPHONE_13_1
if ([alert isKindOfClass:[self nilElementClass]]) {
// application.alert returns UIAElementNil on iOS 13.1
// Instead find the alert by looking for the alert's window and getting the UIAAlert off of it
alert = (UIAAlert *)[[[[[application windows] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIAElement *_Nullable evaluatedObject, NSDictionary<NSString *, id> *_Nullable bindings) {
return [[evaluatedObject valueForKey:@"type"] isEqualToString:@"SBAlertItemWindow"];
}]] firstObject] elements] firstObject];
}
#endif

return alert;
}

- (BOOL)acknowledgeSystemAlert {
UIAAlert* alert = [[self target] frontMostApp].alert;
UIAAlert *alert = [self currentSystemAlert];
// Even though `acknowledgeSystemAlertWithIndex:` checks the index, we have to have
// an additional check here to ensure that when `alert.buttons.count` is 0, subtracting one doesn't cause a wrap-around (2^63 - 1).
if (alert.buttons.count > 0) {
Expand All @@ -126,8 +146,8 @@ - (BOOL)acknowledgeSystemAlert {

// Inspired by: https://github.com/jamesjn/KIF/tree/acknowledge-location-alert
- (BOOL)acknowledgeSystemAlertWithIndex:(NSUInteger)index {
UIAApplication *application = [[self target] frontMostApp];
UIAAlert *alert = application.alert;
UIAAlert *alert = [self currentSystemAlert];

BOOL isIndexInRange = index < alert.buttons.count;
if (![alert isKindOfClass:[self nilElementClass]] && [self _alertIsValidAndVisible:alert] && isIndexInRange) {
[alert.buttons[index] tap];
Expand Down