From 1cea72b9be8bb0ac06a5cc99f8bc0c3d6b74dd5a Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 27 Feb 2024 21:24:11 +0100 Subject: [PATCH 1/6] chore: Tune alert detection if system app is active --- WebDriverAgentLib/Categories/XCUIApplication+FBHelpers.m | 2 +- WebDriverAgentLib/FBAlert.m | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBHelpers.m b/WebDriverAgentLib/Categories/XCUIApplication+FBHelpers.m index 251fb7ad9..c3c99e6f5 100644 --- a/WebDriverAgentLib/Categories/XCUIApplication+FBHelpers.m +++ b/WebDriverAgentLib/Categories/XCUIApplication+FBHelpers.m @@ -533,7 +533,7 @@ - (BOOL)fb_isSameAppAs:(nullable XCUIApplication *)otherApp if (nil == otherApp) { return NO; } - return [self.bundleID isEqualToString:(NSString *)otherApp.bundleID]; + return self == otherApp || [self.bundleID isEqualToString:(NSString *)otherApp.bundleID]; } @end diff --git a/WebDriverAgentLib/FBAlert.m b/WebDriverAgentLib/FBAlert.m index cd71663dd..b06e26788 100644 --- a/WebDriverAgentLib/FBAlert.m +++ b/WebDriverAgentLib/FBAlert.m @@ -261,7 +261,12 @@ - (BOOL)clickAlertButton:(NSString *)label error:(NSError **)error - (XCUIElement *)alertElement { if (nil == self.element) { - self.element = XCUIApplication.fb_systemApplication.fb_alertElement ?: self.application.fb_alertElement; + XCUIApplication *systemApp = XCUIApplication.fb_systemApplication; + if ([systemApp fb_isSameAppAs:self.application]) { + self.element = systemApp.fb_alertElement; + } else { + self.element = systemApp.fb_alertElement ?: self.application.fb_alertElement; + } } return self.element; } From 681c0fc058bf02dd37eff7a3eae5c9cf94e2f6cc Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Wed, 28 Feb 2024 09:25:09 +0100 Subject: [PATCH 2/6] feat: Launch app under test with check for a blocking alert presence --- WebDriverAgent.xcodeproj/project.pbxproj | 12 +++ .../Categories/XCUIApplication+FBLaunch.h | 30 +++++++ .../Categories/XCUIApplication+FBLaunch.m | 81 +++++++++++++++++++ .../Commands/FBSessionCommands.m | 4 +- .../Routing/FBExceptionHandler.m | 3 + WebDriverAgentLib/Routing/FBExceptions.h | 3 + WebDriverAgentLib/Routing/FBExceptions.m | 1 + 7 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h create mode 100644 WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m diff --git a/WebDriverAgent.xcodeproj/project.pbxproj b/WebDriverAgent.xcodeproj/project.pbxproj index bc4f29813..1a98cc796 100644 --- a/WebDriverAgent.xcodeproj/project.pbxproj +++ b/WebDriverAgent.xcodeproj/project.pbxproj @@ -450,6 +450,10 @@ 71A7EAF91E224648001DA4F2 /* FBClassChainQueryParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 71A7EAF71E224648001DA4F2 /* FBClassChainQueryParser.h */; }; 71A7EAFA1E224648001DA4F2 /* FBClassChainQueryParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 71A7EAF81E224648001DA4F2 /* FBClassChainQueryParser.m */; }; 71A7EAFC1E229302001DA4F2 /* FBClassChainTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 71A7EAFB1E229302001DA4F2 /* FBClassChainTests.m */; }; + 71AB65B32B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */ = {isa = PBXBuildFile; fileRef = 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */; }; + 71AB65B42B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */ = {isa = PBXBuildFile; fileRef = 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */; }; + 71AB65B52B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */ = {isa = PBXBuildFile; fileRef = 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */; }; + 71AB65B62B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */ = {isa = PBXBuildFile; fileRef = 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */; }; 71ACF5B8242F2FDC00F0AAD4 /* FBSafariAlertTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 71ACF5B7242F2FDC00F0AAD4 /* FBSafariAlertTests.m */; }; 71B155DA23070ECF00646AFB /* FBHTTPStatusCodes.h in Headers */ = {isa = PBXBuildFile; fileRef = 71B155D923070ECF00646AFB /* FBHTTPStatusCodes.h */; settings = {ATTRIBUTES = (Public, ); }; }; 71B155DC230711E900646AFB /* FBCommandStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 71B155DB230711E900646AFB /* FBCommandStatus.m */; }; @@ -1028,6 +1032,8 @@ 71A7EAF71E224648001DA4F2 /* FBClassChainQueryParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FBClassChainQueryParser.h; sourceTree = ""; }; 71A7EAF81E224648001DA4F2 /* FBClassChainQueryParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBClassChainQueryParser.m; sourceTree = ""; }; 71A7EAFB1E229302001DA4F2 /* FBClassChainTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBClassChainTests.m; sourceTree = ""; }; + 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "XCUIApplication+FBLaunch.h"; sourceTree = ""; }; + 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "XCUIApplication+FBLaunch.m"; sourceTree = ""; }; 71ACF5B7242F2FDC00F0AAD4 /* FBSafariAlertTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FBSafariAlertTests.m; sourceTree = ""; }; 71B155D923070ECF00646AFB /* FBHTTPStatusCodes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FBHTTPStatusCodes.h; sourceTree = ""; }; 71B155DB230711E900646AFB /* FBCommandStatus.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FBCommandStatus.m; sourceTree = ""; }; @@ -1709,6 +1715,8 @@ 71A5C67229A4F39600421C37 /* XCTIssue+FBPatcher.m */, AD6C269A1CF2494200F8B5FF /* XCUIApplication+FBHelpers.h */, AD6C269B1CF2494200F8B5FF /* XCUIApplication+FBHelpers.m */, + 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */, + 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */, 71C8E54F25399A6B008572C1 /* XCUIApplication+FBQuiescence.h */, 71C8E55025399A6B008572C1 /* XCUIApplication+FBQuiescence.m */, 716C9DFE27315EFF005AD475 /* XCUIApplication+FBUIInterruptions.h */, @@ -2331,6 +2339,7 @@ 641EE6882240C5CA00173FCB /* FBElement.h in Headers */, 641EE6892240C5CA00173FCB /* XCTAXClient-Protocol.h in Headers */, 641EE68B2240C5CA00173FCB /* FBExceptionHandler.h in Headers */, + 71AB65B42B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */, 71822726258744AE00661B83 /* RoutingConnection.h in Headers */, 641EE68C2240C5CA00173FCB /* FBRoute.h in Headers */, 641EE68D2240C5CA00173FCB /* XCTestDriver.h in Headers */, @@ -2662,6 +2671,7 @@ 71930C4220662E1F00D3AFEC /* FBPasteboard.h in Headers */, EE7E271C1D06C69F001BEC7B /* FBDebugLogDelegateDecorator.h in Headers */, EEDFE1211D9C06F800E6FFE5 /* XCUIDevice+FBHealthCheck.h in Headers */, + 71AB65B32B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */, 7155D703211DCEF400166C20 /* FBMjpegServer.h in Headers */, EE35AD761E3B77D600A02D78 /* XCUIRecorderNodeFinderMatch.h in Headers */, EE35AD6C1E3B77D600A02D78 /* XCUIApplicationProcess.h in Headers */, @@ -3071,6 +3081,7 @@ 641EE5EB2240C5CA00173FCB /* XCUIElement+FBFind.m in Sources */, 641EE5EC2240C5CA00173FCB /* FBResponsePayload.m in Sources */, C845206322D5E79700EA68CB /* FBUnattachedAppLauncher.m in Sources */, + 71AB65B62B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */, 641EE5ED2240C5CA00173FCB /* FBRoute.m in Sources */, 641EE5EE2240C5CA00173FCB /* NSString+FBVisualLength.m in Sources */, 641EE5EF2240C5CA00173FCB /* FBRunLoopSpinner.m in Sources */, @@ -3216,6 +3227,7 @@ EE158AC51CBD456F00A3E3F0 /* FBOrientationCommands.m in Sources */, 716F0DA32A16CA1000CDD977 /* NSDictionary+FBUtf8SafeDictionary.m in Sources */, 71D475C42538F5A8008D9401 /* XCUIApplicationProcess+FBQuiescence.m in Sources */, + 71AB65B52B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */, 641EE7082240CDEB00173FCB /* XCUIElement+FBTVFocuse.m in Sources */, 71E75E6F254824230099FC87 /* XCUIElementQuery+FBHelpers.m in Sources */, 71D04DCA25356C43008A052C /* XCUIElement+FBCaching.m in Sources */, diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h new file mode 100644 index 000000000..19ea0fa76 --- /dev/null +++ b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface XCUIApplication (FBLaunch) + +/**This property must only be used internally */ +@property (nonatomic, nullable) NSNumber *fb_didStartWithoutBlockingAlert; + +/** + * Launches the app with background blocking alert validation. + * This allows to avoid deadlocks or long timeouts on app startup. + * @param interval The amount of float ssconds between blocking alert presence checks + * @param exceptionName The name of the exception to be thrown in case an alert is detected + */ +- (void)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval + exceptionName:(NSString *)exceptionName; + +@end + +NS_ASSUME_NONNULL_END diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m new file mode 100644 index 000000000..9c6cd807c --- /dev/null +++ b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import "XCUIApplication+FBLaunch.h" + +#import + +#import "FBAlert.h" +#import "FBLogger.h" +#import "XCUIapplication.h" +#import "XCUIApplication+FBAlert.h" +#import "XCUIApplication+FBHelpers.h" + +@implementation XCUIApplication (FBLaunch) + +static char XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT; + +@dynamic fb_didStartWithoutBlockingAlert; + +- (void)setFb_didStartWithoutBlockingAlert:(NSNumber *)didStartWithoutBlockingAlert +{ + objc_setAssociatedObject(self, &XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT, + didStartWithoutBlockingAlert, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (NSNumber *)fb_didStartWithoutBlockingAlert +{ + return objc_getAssociatedObject(self, &XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT); +} + +- (void)fb_scheduleNextDispatchWithInterval:(NSTimeInterval)interval + timeStarted:(uint64_t)timeStarted + timeout:(NSTimeInterval)timeout + exceptionName:(NSString *)exceptionName +{ + dispatch_time_t dispatchTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) (interval * NSEC_PER_SEC)); + dispatch_after(dispatchTime, dispatch_get_main_queue(), ^{ + NSTimeInterval duration = (clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - timeStarted) / NSEC_PER_SEC; + if ([self.fb_didStartWithoutBlockingAlert boolValue] || duration > timeout) { + return; + } + + FBAlert *alert = nil; + @try { + XCUIElement *alertElement = [self.class.fb_systemApplication fb_alertElement]; + if (nil != alertElement) { + alert = [FBAlert alertWithElement:alertElement]; + } + } @catch (NSException *e) { + [FBLogger logFmt:@"Got an unexpected exception while checking for system alerts: %@\n%@", e.reason, e.callStackSymbols]; + } + if (nil != alert) { + NSString *reason = [NSString stringWithFormat:@"The application '%@' cannot be launched because it is blocked by an unexpected system alert: %@", self.bundleID, alert.text]; + @throw [NSException exceptionWithName:exceptionName reason:reason userInfo:nil]; + } + [self fb_scheduleNextDispatchWithInterval:interval + timeStarted:timeStarted + timeout:timeout + exceptionName:exceptionName]; + }); +} + +- (void)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval + exceptionName:(NSString *)exceptionName +{ + self.fb_didStartWithoutBlockingAlert = @NO; + [self fb_scheduleNextDispatchWithInterval:interval + timeStarted:clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) + timeout:65. + exceptionName:exceptionName]; + [self launch]; + self.fb_didStartWithoutBlockingAlert = @YES; +} + +@end diff --git a/WebDriverAgentLib/Commands/FBSessionCommands.m b/WebDriverAgentLib/Commands/FBSessionCommands.m index 9594058a9..73725dfbf 100644 --- a/WebDriverAgentLib/Commands/FBSessionCommands.m +++ b/WebDriverAgentLib/Commands/FBSessionCommands.m @@ -11,6 +11,7 @@ #import "FBCapabilities.h" #import "FBConfiguration.h" +#import "FBExceptions.h" #import "FBLogger.h" #import "FBProtocolHelpers.h" #import "FBRouteRequest.h" @@ -20,6 +21,7 @@ #import "FBActiveAppDetectionPoint.h" #import "FBXCodeCompatibility.h" #import "XCUIApplication+FBHelpers.h" +#import "XCUIApplication+FBLaunch.h" #import "XCUIApplication+FBQuiescence.h" #import "XCUIDevice.h" #import "XCUIDevice+FBHealthCheck.h" @@ -164,7 +166,7 @@ + (NSArray *)routes return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } else { - [app launch]; + [app fb_launchWithInterruptingAlertCheckInterval:1. exceptionName:FBSessionCreationException]; } if (!app.running) { NSString *errorMsg = [NSString stringWithFormat:@"Cannot launch %@ application. Make sure the correct bundle identifier has been provided in capabilities and check the device log for possible crash report occurrences", bundleID]; diff --git a/WebDriverAgentLib/Routing/FBExceptionHandler.m b/WebDriverAgentLib/Routing/FBExceptionHandler.m index 7b7a8263f..811a0efb5 100644 --- a/WebDriverAgentLib/Routing/FBExceptionHandler.m +++ b/WebDriverAgentLib/Routing/FBExceptionHandler.m @@ -45,6 +45,9 @@ - (void)handleException:(NSException *)exception forResponse:(RouteResponse *)re } else if ([exception.name isEqualToString:FBTimeoutException]) { commandStatus = [FBCommandStatus timeoutErrorWithMessage:exception.reason traceback:traceback]; + } else if ([exception.name isEqualToString:FBSessionCreationException]) { + commandStatus = [FBCommandStatus sessionNotCreatedError:exception.reason + traceback:traceback]; } else { commandStatus = [FBCommandStatus unknownErrorWithMessage:exception.reason traceback:traceback]; diff --git a/WebDriverAgentLib/Routing/FBExceptions.h b/WebDriverAgentLib/Routing/FBExceptions.h index 1c9507a19..a7263bae4 100644 --- a/WebDriverAgentLib/Routing/FBExceptions.h +++ b/WebDriverAgentLib/Routing/FBExceptions.h @@ -14,6 +14,9 @@ NS_ASSUME_NONNULL_BEGIN /*! Exception used to notify about missing session */ extern NSString *const FBSessionDoesNotExistException; +/*! Exception used to notify about session creation issues */ +extern NSString *const FBSessionCreationException; + /*! Exception used to notify about application deadlock */ extern NSString *const FBApplicationDeadlockDetectedException; diff --git a/WebDriverAgentLib/Routing/FBExceptions.m b/WebDriverAgentLib/Routing/FBExceptions.m index 571cea4fb..9e1f2141f 100644 --- a/WebDriverAgentLib/Routing/FBExceptions.m +++ b/WebDriverAgentLib/Routing/FBExceptions.m @@ -10,6 +10,7 @@ #import "FBExceptions.h" NSString *const FBInvalidArgumentException = @"FBInvalidArgumentException"; +NSString *const FBSessionCreationException = @"FBSessionCreationException"; NSString *const FBSessionDoesNotExistException = @"FBSessionDoesNotExistException"; NSString *const FBApplicationDeadlockDetectedException = @"FBApplicationDeadlockDetectedException"; NSString *const FBElementAttributeUnknownException = @"FBElementAttributeUnknownException"; From 338c845fcd00b4293c3f11fdcae8de2a4b2957a8 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 29 Feb 2024 10:21:40 +0100 Subject: [PATCH 3/6] tune exceptions handling --- .../Categories/XCUIApplication+FBLaunch.h | 5 +++- .../Categories/XCUIApplication+FBLaunch.m | 25 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h index 19ea0fa76..47eaacabe 100644 --- a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h +++ b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h @@ -13,9 +13,12 @@ NS_ASSUME_NONNULL_BEGIN @interface XCUIApplication (FBLaunch) -/**This property must only be used internally */ +/** This property must only be used internally */ @property (nonatomic, nullable) NSNumber *fb_didStartWithoutBlockingAlert; +/** This property must only be used internally */ +@property (nonatomic, nullable) NSNumber *fb_hasBlockingAlert; + /** * Launches the app with background blocking alert validation. * This allows to avoid deadlocks or long timeouts on app startup. diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m index 9c6cd807c..aead8324c 100644 --- a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m +++ b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m @@ -20,8 +20,10 @@ @implementation XCUIApplication (FBLaunch) static char XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT; +static char XCUIAPPLICATION_HAS_BLOCKING_ALERT; @dynamic fb_didStartWithoutBlockingAlert; +@dynamic fb_hasBlockingAlert; - (void)setFb_didStartWithoutBlockingAlert:(NSNumber *)didStartWithoutBlockingAlert { @@ -34,6 +36,17 @@ - (NSNumber *)fb_didStartWithoutBlockingAlert return objc_getAssociatedObject(self, &XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT); } +- (void)setFb_hasBlockingAlert:(NSNumber *)hasBlockingAlert +{ + objc_setAssociatedObject(self, &XCUIAPPLICATION_HAS_BLOCKING_ALERT, + hasBlockingAlert, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (NSNumber *)fb_hasBlockingAlert +{ + return objc_getAssociatedObject(self, &XCUIAPPLICATION_HAS_BLOCKING_ALERT); +} + - (void)fb_scheduleNextDispatchWithInterval:(NSTimeInterval)interval timeStarted:(uint64_t)timeStarted timeout:(NSTimeInterval)timeout @@ -57,6 +70,7 @@ - (void)fb_scheduleNextDispatchWithInterval:(NSTimeInterval)interval } if (nil != alert) { NSString *reason = [NSString stringWithFormat:@"The application '%@' cannot be launched because it is blocked by an unexpected system alert: %@", self.bundleID, alert.text]; + self.fb_hasBlockingAlert = @YES; @throw [NSException exceptionWithName:exceptionName reason:reason userInfo:nil]; } [self fb_scheduleNextDispatchWithInterval:interval @@ -70,12 +84,19 @@ - (void)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval exceptionName:(NSString *)exceptionName { self.fb_didStartWithoutBlockingAlert = @NO; + self.fb_hasBlockingAlert = @NO; [self fb_scheduleNextDispatchWithInterval:interval timeStarted:clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) timeout:65. exceptionName:exceptionName]; - [self launch]; - self.fb_didStartWithoutBlockingAlert = @YES; + @try { + [self launch]; + self.fb_didStartWithoutBlockingAlert = @YES; + } @catch (NSException *e) { + if (![self.fb_hasBlockingAlert boolValue]) { + @throw e; + } + } } @end From ef6947ab26d27f3af351f645ad69a4c7d80683b9 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 29 Feb 2024 11:14:20 +0100 Subject: [PATCH 4/6] Another approach --- .../Categories/XCUIApplication+FBLaunch.h | 9 ++-- .../Categories/XCUIApplication+FBLaunch.m | 45 ++++++++++--------- .../Commands/FBSessionCommands.m | 6 ++- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h index 47eaacabe..a9cc1398d 100644 --- a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h +++ b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h @@ -17,16 +17,17 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, nullable) NSNumber *fb_didStartWithoutBlockingAlert; /** This property must only be used internally */ -@property (nonatomic, nullable) NSNumber *fb_hasBlockingAlert; +@property (nonatomic, nullable) NSString *fb_blockingAlertText; /** * Launches the app with background blocking alert validation. * This allows to avoid deadlocks or long timeouts on app startup. * @param interval The amount of float ssconds between blocking alert presence checks - * @param exceptionName The name of the exception to be thrown in case an alert is detected + * @param error The actual error if present + * @return YES in case of success */ -- (void)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval - exceptionName:(NSString *)exceptionName; +- (BOOL)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval + error:(NSError **)error; @end diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m index aead8324c..1d31efbe3 100644 --- a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m +++ b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m @@ -13,6 +13,7 @@ #import "FBAlert.h" #import "FBLogger.h" +#import "FBErrorBuilder.h" #import "XCUIapplication.h" #import "XCUIApplication+FBAlert.h" #import "XCUIApplication+FBHelpers.h" @@ -20,10 +21,10 @@ @implementation XCUIApplication (FBLaunch) static char XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT; -static char XCUIAPPLICATION_HAS_BLOCKING_ALERT; +static char XCUIAPPLICATION_BLOCKING_ALERT_TEXT; @dynamic fb_didStartWithoutBlockingAlert; -@dynamic fb_hasBlockingAlert; +@dynamic fb_blockingAlertText; - (void)setFb_didStartWithoutBlockingAlert:(NSNumber *)didStartWithoutBlockingAlert { @@ -36,21 +37,20 @@ - (NSNumber *)fb_didStartWithoutBlockingAlert return objc_getAssociatedObject(self, &XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT); } -- (void)setFb_hasBlockingAlert:(NSNumber *)hasBlockingAlert +- (void)setFb_blockingAlertText:(NSString *)blockingAlertText { - objc_setAssociatedObject(self, &XCUIAPPLICATION_HAS_BLOCKING_ALERT, - hasBlockingAlert, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + objc_setAssociatedObject(self, &XCUIAPPLICATION_BLOCKING_ALERT_TEXT, + blockingAlertText, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -- (NSNumber *)fb_hasBlockingAlert +- (NSString *)fb_blockingAlertText { - return objc_getAssociatedObject(self, &XCUIAPPLICATION_HAS_BLOCKING_ALERT); + return objc_getAssociatedObject(self, &XCUIAPPLICATION_BLOCKING_ALERT_TEXT); } - (void)fb_scheduleNextDispatchWithInterval:(NSTimeInterval)interval timeStarted:(uint64_t)timeStarted timeout:(NSTimeInterval)timeout - exceptionName:(NSString *)exceptionName { dispatch_time_t dispatchTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) (interval * NSEC_PER_SEC)); dispatch_after(dispatchTime, dispatch_get_main_queue(), ^{ @@ -69,34 +69,39 @@ - (void)fb_scheduleNextDispatchWithInterval:(NSTimeInterval)interval [FBLogger logFmt:@"Got an unexpected exception while checking for system alerts: %@\n%@", e.reason, e.callStackSymbols]; } if (nil != alert) { - NSString *reason = [NSString stringWithFormat:@"The application '%@' cannot be launched because it is blocked by an unexpected system alert: %@", self.bundleID, alert.text]; - self.fb_hasBlockingAlert = @YES; - @throw [NSException exceptionWithName:exceptionName reason:reason userInfo:nil]; + self.fb_blockingAlertText = alert.text; + [self terminate]; + return; } [self fb_scheduleNextDispatchWithInterval:interval timeStarted:timeStarted - timeout:timeout - exceptionName:exceptionName]; + timeout:timeout]; }); } -- (void)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval - exceptionName:(NSString *)exceptionName +- (BOOL)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval + error:(NSError **)error { self.fb_didStartWithoutBlockingAlert = @NO; - self.fb_hasBlockingAlert = @NO; + self.fb_blockingAlertText = nil; [self fb_scheduleNextDispatchWithInterval:interval timeStarted:clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - timeout:65. - exceptionName:exceptionName]; + timeout:65.]; @try { [self launch]; self.fb_didStartWithoutBlockingAlert = @YES; + return YES; } @catch (NSException *e) { - if (![self.fb_hasBlockingAlert boolValue]) { - @throw e; + if (nil == self.fb_blockingAlertText) { + self.fb_didStartWithoutBlockingAlert = @YES; + return [[[FBErrorBuilder builder] + withDescriptionFormat:@"The application '%@' cannot be launched because of an unexpected error: %@", self.bundleID, e.reason] + buildError:error];; } } + return [[[FBErrorBuilder builder] + withDescriptionFormat:@"The application '%@' cannot be launched because it is blocked by an unexpected system alert: %@", self.bundleID, self.fb_blockingAlertText] + buildError:error];; } @end diff --git a/WebDriverAgentLib/Commands/FBSessionCommands.m b/WebDriverAgentLib/Commands/FBSessionCommands.m index 73725dfbf..eb620e5d2 100644 --- a/WebDriverAgentLib/Commands/FBSessionCommands.m +++ b/WebDriverAgentLib/Commands/FBSessionCommands.m @@ -166,7 +166,11 @@ + (NSArray *)routes return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } else { - [app fb_launchWithInterruptingAlertCheckInterval:1. exceptionName:FBSessionCreationException]; + NSError *launchError; + if (![app fb_launchWithInterruptingAlertCheckInterval:1. error:&launchError]) { + return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:launchError.description + traceback:nil]); + } } if (!app.running) { NSString *errorMsg = [NSString stringWithFormat:@"Cannot launch %@ application. Make sure the correct bundle identifier has been provided in capabilities and check the device log for possible crash report occurrences", bundleID]; From 2c2c9927eb7e028a49960a9514e5d34ea0b2459b Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 29 Feb 2024 19:20:18 +0100 Subject: [PATCH 5/6] Use localized description --- WebDriverAgentLib/Commands/FBSessionCommands.m | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/WebDriverAgentLib/Commands/FBSessionCommands.m b/WebDriverAgentLib/Commands/FBSessionCommands.m index eb620e5d2..2f5d07239 100644 --- a/WebDriverAgentLib/Commands/FBSessionCommands.m +++ b/WebDriverAgentLib/Commands/FBSessionCommands.m @@ -93,7 +93,7 @@ + (NSArray *)routes traceback:nil]); } if (nil == (capabilities = FBParseCapabilities((NSDictionary *)request.arguments[@"capabilities"], &error))) { - return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:error.description traceback:nil]); + return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:error.localizedDescription traceback:nil]); } [FBConfiguration resetSessionSettings]; @@ -161,14 +161,14 @@ + (NSArray *)routes if (![XCUIDevice.sharedDevice fb_openUrl:initialUrl withApplication:bundleID error:&openError]) { - NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ wuth the %@ application. Original error: %@", - initialUrl, bundleID, openError.description]; + NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ with the %@ application. Original error: %@", + initialUrl, bundleID, openError.localizedDescription]; return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } else { NSError *launchError; if (![app fb_launchWithInterruptingAlertCheckInterval:1. error:&launchError]) { - return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:launchError.description + return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:launchError.localizedDescription traceback:nil]); } } @@ -184,7 +184,7 @@ + (NSArray *)routes withApplication:bundleID error:&openError]) { NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ with the %@ application. Original error: %@", - initialUrl, bundleID, openError.description]; + initialUrl, bundleID, openError.localizedDescription]; return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } else { @@ -197,7 +197,7 @@ + (NSArray *)routes NSError *openError; if (![XCUIDevice.sharedDevice fb_openUrl:initialUrl error:&openError]) { NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@. Original error: %@", - initialUrl, openError.description]; + initialUrl, openError.localizedDescription]; return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } @@ -402,7 +402,8 @@ + (NSArray *)routes NSError *error; if (![FBActiveAppDetectionPoint.sharedInstance setCoordinatesWithString:(NSString *)[settings objectForKey:FB_SETTING_ACTIVE_APP_DETECTION_POINT] error:&error]) { - return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.description traceback:nil]); + return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.localizedDescription + traceback:nil]); } } if (nil != [settings objectForKey:FB_SETTING_INCLUDE_NON_MODAL_ELEMENTS]) { @@ -433,7 +434,7 @@ + (NSArray *)routes NSError *error; if (![FBConfiguration setScreenshotOrientation:(NSString *)[settings objectForKey:FB_SETTING_SCREENSHOT_ORIENTATION] error:&error]) { - return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.description + return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.localizedDescription traceback:nil]); } } From 3b92cee3bd60458dc7c3410ba1a3b8d71cd6ac60 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sun, 3 Mar 2024 16:16:03 +0100 Subject: [PATCH 6/6] handle error on app launch --- WebDriverAgent.xcodeproj/project.pbxproj | 12 -- .../Categories/XCUIApplication+FBLaunch.h | 34 ------ .../Categories/XCUIApplication+FBLaunch.m | 107 ------------------ .../Commands/FBSessionCommands.m | 9 +- 4 files changed, 4 insertions(+), 158 deletions(-) delete mode 100644 WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h delete mode 100644 WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m diff --git a/WebDriverAgent.xcodeproj/project.pbxproj b/WebDriverAgent.xcodeproj/project.pbxproj index 1a98cc796..bc4f29813 100644 --- a/WebDriverAgent.xcodeproj/project.pbxproj +++ b/WebDriverAgent.xcodeproj/project.pbxproj @@ -450,10 +450,6 @@ 71A7EAF91E224648001DA4F2 /* FBClassChainQueryParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 71A7EAF71E224648001DA4F2 /* FBClassChainQueryParser.h */; }; 71A7EAFA1E224648001DA4F2 /* FBClassChainQueryParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 71A7EAF81E224648001DA4F2 /* FBClassChainQueryParser.m */; }; 71A7EAFC1E229302001DA4F2 /* FBClassChainTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 71A7EAFB1E229302001DA4F2 /* FBClassChainTests.m */; }; - 71AB65B32B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */ = {isa = PBXBuildFile; fileRef = 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */; }; - 71AB65B42B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */ = {isa = PBXBuildFile; fileRef = 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */; }; - 71AB65B52B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */ = {isa = PBXBuildFile; fileRef = 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */; }; - 71AB65B62B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */ = {isa = PBXBuildFile; fileRef = 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */; }; 71ACF5B8242F2FDC00F0AAD4 /* FBSafariAlertTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 71ACF5B7242F2FDC00F0AAD4 /* FBSafariAlertTests.m */; }; 71B155DA23070ECF00646AFB /* FBHTTPStatusCodes.h in Headers */ = {isa = PBXBuildFile; fileRef = 71B155D923070ECF00646AFB /* FBHTTPStatusCodes.h */; settings = {ATTRIBUTES = (Public, ); }; }; 71B155DC230711E900646AFB /* FBCommandStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 71B155DB230711E900646AFB /* FBCommandStatus.m */; }; @@ -1032,8 +1028,6 @@ 71A7EAF71E224648001DA4F2 /* FBClassChainQueryParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FBClassChainQueryParser.h; sourceTree = ""; }; 71A7EAF81E224648001DA4F2 /* FBClassChainQueryParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBClassChainQueryParser.m; sourceTree = ""; }; 71A7EAFB1E229302001DA4F2 /* FBClassChainTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBClassChainTests.m; sourceTree = ""; }; - 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "XCUIApplication+FBLaunch.h"; sourceTree = ""; }; - 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "XCUIApplication+FBLaunch.m"; sourceTree = ""; }; 71ACF5B7242F2FDC00F0AAD4 /* FBSafariAlertTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FBSafariAlertTests.m; sourceTree = ""; }; 71B155D923070ECF00646AFB /* FBHTTPStatusCodes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FBHTTPStatusCodes.h; sourceTree = ""; }; 71B155DB230711E900646AFB /* FBCommandStatus.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FBCommandStatus.m; sourceTree = ""; }; @@ -1715,8 +1709,6 @@ 71A5C67229A4F39600421C37 /* XCTIssue+FBPatcher.m */, AD6C269A1CF2494200F8B5FF /* XCUIApplication+FBHelpers.h */, AD6C269B1CF2494200F8B5FF /* XCUIApplication+FBHelpers.m */, - 71AB65B12B8F156200187057 /* XCUIApplication+FBLaunch.h */, - 71AB65B22B8F156200187057 /* XCUIApplication+FBLaunch.m */, 71C8E54F25399A6B008572C1 /* XCUIApplication+FBQuiescence.h */, 71C8E55025399A6B008572C1 /* XCUIApplication+FBQuiescence.m */, 716C9DFE27315EFF005AD475 /* XCUIApplication+FBUIInterruptions.h */, @@ -2339,7 +2331,6 @@ 641EE6882240C5CA00173FCB /* FBElement.h in Headers */, 641EE6892240C5CA00173FCB /* XCTAXClient-Protocol.h in Headers */, 641EE68B2240C5CA00173FCB /* FBExceptionHandler.h in Headers */, - 71AB65B42B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */, 71822726258744AE00661B83 /* RoutingConnection.h in Headers */, 641EE68C2240C5CA00173FCB /* FBRoute.h in Headers */, 641EE68D2240C5CA00173FCB /* XCTestDriver.h in Headers */, @@ -2671,7 +2662,6 @@ 71930C4220662E1F00D3AFEC /* FBPasteboard.h in Headers */, EE7E271C1D06C69F001BEC7B /* FBDebugLogDelegateDecorator.h in Headers */, EEDFE1211D9C06F800E6FFE5 /* XCUIDevice+FBHealthCheck.h in Headers */, - 71AB65B32B8F156200187057 /* XCUIApplication+FBLaunch.h in Headers */, 7155D703211DCEF400166C20 /* FBMjpegServer.h in Headers */, EE35AD761E3B77D600A02D78 /* XCUIRecorderNodeFinderMatch.h in Headers */, EE35AD6C1E3B77D600A02D78 /* XCUIApplicationProcess.h in Headers */, @@ -3081,7 +3071,6 @@ 641EE5EB2240C5CA00173FCB /* XCUIElement+FBFind.m in Sources */, 641EE5EC2240C5CA00173FCB /* FBResponsePayload.m in Sources */, C845206322D5E79700EA68CB /* FBUnattachedAppLauncher.m in Sources */, - 71AB65B62B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */, 641EE5ED2240C5CA00173FCB /* FBRoute.m in Sources */, 641EE5EE2240C5CA00173FCB /* NSString+FBVisualLength.m in Sources */, 641EE5EF2240C5CA00173FCB /* FBRunLoopSpinner.m in Sources */, @@ -3227,7 +3216,6 @@ EE158AC51CBD456F00A3E3F0 /* FBOrientationCommands.m in Sources */, 716F0DA32A16CA1000CDD977 /* NSDictionary+FBUtf8SafeDictionary.m in Sources */, 71D475C42538F5A8008D9401 /* XCUIApplicationProcess+FBQuiescence.m in Sources */, - 71AB65B52B8F156200187057 /* XCUIApplication+FBLaunch.m in Sources */, 641EE7082240CDEB00173FCB /* XCUIElement+FBTVFocuse.m in Sources */, 71E75E6F254824230099FC87 /* XCUIElementQuery+FBHelpers.m in Sources */, 71D04DCA25356C43008A052C /* XCUIElement+FBCaching.m in Sources */, diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h deleted file mode 100644 index a9cc1398d..000000000 --- a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.h +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface XCUIApplication (FBLaunch) - -/** This property must only be used internally */ -@property (nonatomic, nullable) NSNumber *fb_didStartWithoutBlockingAlert; - -/** This property must only be used internally */ -@property (nonatomic, nullable) NSString *fb_blockingAlertText; - -/** - * Launches the app with background blocking alert validation. - * This allows to avoid deadlocks or long timeouts on app startup. - * @param interval The amount of float ssconds between blocking alert presence checks - * @param error The actual error if present - * @return YES in case of success - */ -- (BOOL)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval - error:(NSError **)error; - -@end - -NS_ASSUME_NONNULL_END diff --git a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m b/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m deleted file mode 100644 index 1d31efbe3..000000000 --- a/WebDriverAgentLib/Categories/XCUIApplication+FBLaunch.m +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ - -#import "XCUIApplication+FBLaunch.h" - -#import - -#import "FBAlert.h" -#import "FBLogger.h" -#import "FBErrorBuilder.h" -#import "XCUIapplication.h" -#import "XCUIApplication+FBAlert.h" -#import "XCUIApplication+FBHelpers.h" - -@implementation XCUIApplication (FBLaunch) - -static char XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT; -static char XCUIAPPLICATION_BLOCKING_ALERT_TEXT; - -@dynamic fb_didStartWithoutBlockingAlert; -@dynamic fb_blockingAlertText; - -- (void)setFb_didStartWithoutBlockingAlert:(NSNumber *)didStartWithoutBlockingAlert -{ - objc_setAssociatedObject(self, &XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT, - didStartWithoutBlockingAlert, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -} - -- (NSNumber *)fb_didStartWithoutBlockingAlert -{ - return objc_getAssociatedObject(self, &XCUIAPPLICATION_DID_START_WO_BLOCKING_ALERT); -} - -- (void)setFb_blockingAlertText:(NSString *)blockingAlertText -{ - objc_setAssociatedObject(self, &XCUIAPPLICATION_BLOCKING_ALERT_TEXT, - blockingAlertText, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -} - -- (NSString *)fb_blockingAlertText -{ - return objc_getAssociatedObject(self, &XCUIAPPLICATION_BLOCKING_ALERT_TEXT); -} - -- (void)fb_scheduleNextDispatchWithInterval:(NSTimeInterval)interval - timeStarted:(uint64_t)timeStarted - timeout:(NSTimeInterval)timeout -{ - dispatch_time_t dispatchTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) (interval * NSEC_PER_SEC)); - dispatch_after(dispatchTime, dispatch_get_main_queue(), ^{ - NSTimeInterval duration = (clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - timeStarted) / NSEC_PER_SEC; - if ([self.fb_didStartWithoutBlockingAlert boolValue] || duration > timeout) { - return; - } - - FBAlert *alert = nil; - @try { - XCUIElement *alertElement = [self.class.fb_systemApplication fb_alertElement]; - if (nil != alertElement) { - alert = [FBAlert alertWithElement:alertElement]; - } - } @catch (NSException *e) { - [FBLogger logFmt:@"Got an unexpected exception while checking for system alerts: %@\n%@", e.reason, e.callStackSymbols]; - } - if (nil != alert) { - self.fb_blockingAlertText = alert.text; - [self terminate]; - return; - } - [self fb_scheduleNextDispatchWithInterval:interval - timeStarted:timeStarted - timeout:timeout]; - }); -} - -- (BOOL)fb_launchWithInterruptingAlertCheckInterval:(NSTimeInterval)interval - error:(NSError **)error -{ - self.fb_didStartWithoutBlockingAlert = @NO; - self.fb_blockingAlertText = nil; - [self fb_scheduleNextDispatchWithInterval:interval - timeStarted:clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - timeout:65.]; - @try { - [self launch]; - self.fb_didStartWithoutBlockingAlert = @YES; - return YES; - } @catch (NSException *e) { - if (nil == self.fb_blockingAlertText) { - self.fb_didStartWithoutBlockingAlert = @YES; - return [[[FBErrorBuilder builder] - withDescriptionFormat:@"The application '%@' cannot be launched because of an unexpected error: %@", self.bundleID, e.reason] - buildError:error];; - } - } - return [[[FBErrorBuilder builder] - withDescriptionFormat:@"The application '%@' cannot be launched because it is blocked by an unexpected system alert: %@", self.bundleID, self.fb_blockingAlertText] - buildError:error];; -} - -@end diff --git a/WebDriverAgentLib/Commands/FBSessionCommands.m b/WebDriverAgentLib/Commands/FBSessionCommands.m index 2f5d07239..38def9553 100644 --- a/WebDriverAgentLib/Commands/FBSessionCommands.m +++ b/WebDriverAgentLib/Commands/FBSessionCommands.m @@ -21,7 +21,6 @@ #import "FBActiveAppDetectionPoint.h" #import "FBXCodeCompatibility.h" #import "XCUIApplication+FBHelpers.h" -#import "XCUIApplication+FBLaunch.h" #import "XCUIApplication+FBQuiescence.h" #import "XCUIDevice.h" #import "XCUIDevice+FBHealthCheck.h" @@ -166,10 +165,10 @@ + (NSArray *)routes return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } else { - NSError *launchError; - if (![app fb_launchWithInterruptingAlertCheckInterval:1. error:&launchError]) { - return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:launchError.localizedDescription - traceback:nil]); + @try { + [app launch]; + } @catch (NSException *e) { + return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:e.reason traceback:nil]); } } if (!app.running) {