Skip to content

Commit

Permalink
feat: Open the default URL in Safari upon session startup
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Aug 5, 2024
1 parent 3c8497b commit b17e52c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
12 changes: 12 additions & 0 deletions WebDriverAgent.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@
7182276E258744C900661B83 /* HTTPErrorResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = E444DC59249131880060D7EB /* HTTPErrorResponse.h */; };
71822777258744CE00661B83 /* DDNumber.h in Headers */ = {isa = PBXBuildFile; fileRef = E444DC7D249131B00060D7EB /* DDNumber.h */; };
71822780258744D000661B83 /* DDRange.h in Headers */ = {isa = PBXBuildFile; fileRef = E444DC7B249131B00060D7EB /* DDRange.h */; };
718D2C122C60BAE3000788F2 /* FBWebServerParams.m in Sources */ = {isa = PBXBuildFile; fileRef = 718D2C112C60BAE3000788F2 /* FBWebServerParams.m */; };
718D2C132C60BAE3000788F2 /* FBWebServerParams.h in Headers */ = {isa = PBXBuildFile; fileRef = 718D2C102C60BAE3000788F2 /* FBWebServerParams.h */; };
718D2C142C60BAE3000788F2 /* FBWebServerParams.h in Headers */ = {isa = PBXBuildFile; fileRef = 718D2C102C60BAE3000788F2 /* FBWebServerParams.h */; };
718D2C152C60BAE3000788F2 /* FBWebServerParams.m in Sources */ = {isa = PBXBuildFile; fileRef = 718D2C112C60BAE3000788F2 /* FBWebServerParams.m */; };
718F49C8230844330045FE8B /* FBProtocolHelpersTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 718F49C7230844330045FE8B /* FBProtocolHelpersTests.m */; };
718F49C923087ACF0045FE8B /* FBProtocolHelpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 71B155DD23080CA600646AFB /* FBProtocolHelpers.h */; };
718F49CA23087AD30045FE8B /* FBProtocolHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 71B155DE23080CA600646AFB /* FBProtocolHelpers.m */; };
Expand Down Expand Up @@ -1027,6 +1031,8 @@
718226C72587443600661B83 /* GCDAsyncSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GCDAsyncSocket.h; path = WebDriverAgentLib/Vendor/CocoaAsyncSocket/GCDAsyncSocket.h; sourceTree = SOURCE_ROOT; };
718226C82587443600661B83 /* GCDAsyncSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GCDAsyncSocket.m; path = WebDriverAgentLib/Vendor/CocoaAsyncSocket/GCDAsyncSocket.m; sourceTree = SOURCE_ROOT; };
718226C92587443600661B83 /* GCDAsyncUdpSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GCDAsyncUdpSocket.m; path = WebDriverAgentLib/Vendor/CocoaAsyncSocket/GCDAsyncUdpSocket.m; sourceTree = SOURCE_ROOT; };
718D2C102C60BAE3000788F2 /* FBWebServerParams.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FBWebServerParams.h; sourceTree = "<group>"; };
718D2C112C60BAE3000788F2 /* FBWebServerParams.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FBWebServerParams.m; sourceTree = "<group>"; };
718F49C7230844330045FE8B /* FBProtocolHelpersTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FBProtocolHelpersTests.m; sourceTree = "<group>"; };
71930C4020662E1F00D3AFEC /* FBPasteboard.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FBPasteboard.h; sourceTree = "<group>"; };
71930C4120662E1F00D3AFEC /* FBPasteboard.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FBPasteboard.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1948,6 +1954,8 @@
7140974A1FAE1B51008FB2C5 /* FBW3CActionsSynthesizer.m */,
713AE573243A53BE0000D657 /* FBW3CActionsHelpers.h */,
713AE574243A53BE0000D657 /* FBW3CActionsHelpers.m */,
718D2C102C60BAE3000788F2 /* FBWebServerParams.h */,
718D2C112C60BAE3000788F2 /* FBWebServerParams.m */,
7157B28F221DADD2001C348C /* FBXCAXClientProxy.h */,
7157B290221DADD2001C348C /* FBXCAXClientProxy.m */,
EE5A24401F136C8D0078B1D9 /* FBXCodeCompatibility.h */,
Expand Down Expand Up @@ -2463,6 +2471,7 @@
641EE6D62240C5CA00173FCB /* FBLogger.h in Headers */,
71BB58F72B96531900CB9BFE /* FBScreenRecordingContainer.h in Headers */,
641EE6D72240C5CA00173FCB /* XCTestObserver.h in Headers */,
718D2C142C60BAE3000788F2 /* FBWebServerParams.h in Headers */,
641EE6D82240C5CA00173FCB /* XCUIElement.h in Headers */,
641EE6D92240C5CA00173FCB /* XCKeyboardInputSolver.h in Headers */,
718226CB2587443700661B83 /* GCDAsyncUdpSocket.h in Headers */,
Expand Down Expand Up @@ -2718,6 +2727,7 @@
EE35AD311E3B77D600A02D78 /* XCKeyboardLayout.h in Headers */,
716C9DFA27315D21005AD475 /* FBReflectionUtils.h in Headers */,
E444DCB624913C220060D7EB /* RouteRequest.h in Headers */,
718D2C132C60BAE3000788F2 /* FBWebServerParams.h in Headers */,
71F5BE23252E576C00EE9EBA /* XCUIElement+FBSwiping.h in Headers */,
718226CC2587443700661B83 /* GCDAsyncSocket.h in Headers */,
EE35AD3B1E3B77D600A02D78 /* XCTAsyncActivity-Protocol.h in Headers */,
Expand Down Expand Up @@ -3093,6 +3103,7 @@
641EE5D72240C5CA00173FCB /* FBScreenshotCommands.m in Sources */,
71F3E7D725417FF400E0C22B /* FBSettings.m in Sources */,
641EE5D92240C5CA00173FCB /* XCUIElement+FBPickerWheel.m in Sources */,
718D2C152C60BAE3000788F2 /* FBWebServerParams.m in Sources */,
641EE5DA2240C5CA00173FCB /* XCUIApplicationProcessDelay.m in Sources */,
641EE5DB2240C5CA00173FCB /* FBXPath.m in Sources */,
71C8E55425399A6B008572C1 /* XCUIApplication+FBQuiescence.m in Sources */,
Expand Down Expand Up @@ -3282,6 +3293,7 @@
AD76723E1D6B7CC000610457 /* XCUIElement+FBTyping.m in Sources */,
EE158AAF1CBD456F00A3E3F0 /* XCUIElement+FBAccessibility.m in Sources */,
714E14BA29805CAE00375DD7 /* XCAXClient_iOS+FBSnapshotReqParams.m in Sources */,
718D2C122C60BAE3000788F2 /* FBWebServerParams.m in Sources */,
7150348821A6DAD600A0F4BA /* FBImageUtils.m in Sources */,
E444DCAB24913C220060D7EB /* HTTPResponseProxy.m in Sources */,
E444DC6D249131890060D7EB /* HTTPErrorResponse.m in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions WebDriverAgentLib/Commands/FBSessionCommands.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "FBRuntimeUtils.h"
#import "FBActiveAppDetectionPoint.h"
#import "FBXCodeCompatibility.h"
#import "FBWebServerParams.h"
#import "XCUIApplication+FBHelpers.h"
#import "XCUIApplication+FBQuiescence.h"
#import "XCUIDevice.h"
Expand Down Expand Up @@ -176,6 +177,18 @@ + (NSArray *)routes
_XCTSetApplicationStateTimeout(defaultTimeout);
}
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"17.0") && [bundleID isEqualToString:FB_SAFARI_BUNDLE_ID]) {
// Opening the default URL in Safari instead of an empty page helps
// the remote debugger to avoid issues while looking for active web views
FBWebServerParams *wsParams = FBWebServerParams.sharedInstance;
NSString *healthEndpoint = [NSString stringWithFormat:@"http://127.0.0.1:%@/health", wsParams.port];
id<FBResponsePayload> errorResponse = [self openDeepLink:healthEndpoint
withApplication:bundleID
timeout:capabilities[FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC]];
if (nil != errorResponse) {
NSLog(@"Was not able to open the default URL %@ in Safari", healthEndpoint);
}
}
}
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];
Expand Down
3 changes: 3 additions & 0 deletions WebDriverAgentLib/Routing/FBSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

NS_ASSUME_NONNULL_BEGIN

/** Bundle identifier of Mobile Safari browser */
extern NSString* const FB_SAFARI_BUNDLE_ID;

/**
Class that represents testing session
*/
Expand Down
2 changes: 2 additions & 0 deletions WebDriverAgentLib/Routing/FBSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
*/
NSString *const FBDefaultApplicationAuto = @"auto";

NSString *const FB_SAFARI_BUNDLE_ID = @"com.apple.mobilesafari";

@interface FBSession ()
@property (nullable, nonatomic) XCUIApplication *testedApplication;
@property (nonatomic) BOOL isTestedApplicationExpectedToRun;
Expand Down
3 changes: 3 additions & 0 deletions WebDriverAgentLib/Routing/FBWebServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "FBUnknownCommands.h"
#import "FBConfiguration.h"
#import "FBLogger.h"
#import "FBWebServerParams.h"

#import "XCUIDevice+FBHelpers.h"

Expand Down Expand Up @@ -102,6 +103,8 @@ - (void)startHTTPServer

serverStarted = [self attemptToStartServer:self.server onPort:port withError:&error];
if (serverStarted) {
FBWebServerParams* wsParams = FBWebServerParams.sharedInstance;
wsParams.port = @(self.server.port);
break;
}

Expand Down
23 changes: 23 additions & 0 deletions WebDriverAgentLib/Utilities/FBWebServerParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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 <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface FBWebServerParams : NSObject

/** The local port number WDA server is running on */
@property (nonatomic, nullable) NSNumber *port;

+ (id)sharedInstance;

@end

NS_ASSUME_NONNULL_END
24 changes: 24 additions & 0 deletions WebDriverAgentLib/Utilities/FBWebServerParams.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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 "FBWebServerParams.h"

@implementation FBWebServerParams

+ (instancetype)sharedInstance
{
static FBWebServerParams *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}

@end
6 changes: 2 additions & 4 deletions WebDriverAgentTests/IntegrationTests/FBSafariAlertTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ @interface FBSafariAlertIntegrationTests : FBIntegrationTestCase
@end


static NSString *const SAFARI_BUNDLE_ID = @"com.apple.mobilesafari";

@implementation FBSafariAlertIntegrationTests

- (void)setUp
{
[super setUp];
self.session = [FBSession initWithApplication:XCUIApplication.fb_activeApplication];
[self.session launchApplicationWithBundleId:SAFARI_BUNDLE_ID
[self.session launchApplicationWithBundleId:FB_SAFARI_BUNDLE_ID
shouldWaitForQuiescence:nil
arguments:nil
environment:nil];
Expand All @@ -42,7 +40,7 @@ - (void)setUp

- (void)tearDown
{
[self.session terminateApplicationWithBundleId:SAFARI_BUNDLE_ID];
[self.session terminateApplicationWithBundleId:FB_SAFARI_BUNDLE_ID];
}

- (void)disabled_testCanHandleSafariInputPrompt
Expand Down

0 comments on commit b17e52c

Please sign in to comment.