2727#import " GREYLogger.h"
2828#import " GREYSetup.h"
2929#import " GREYSwizzler.h"
30+ #import " GREYXCTestAppleInternals.h"
3031#import " GREYTestConfiguration.h"
3132#import " XCUIApplication+GREYEnvironment.h"
3233
@@ -52,6 +53,55 @@ + (NSString *)greyTestRigName {
5253- (void )grey_launch {
5354 [self modifyKeyboardSettings ];
5455
56+ static NSString *gPrimaryAppBundleID ;
57+ static dispatch_once_t onceToken;
58+ dispatch_once (&onceToken, ^{
59+ @try {
60+ gPrimaryAppBundleID = XCTestConfiguration.activeTestConfiguration .targetApplicationBundleID ;
61+ } @catch (NSException *exception) {
62+ GREYLog (@" Failed to read targetApplicationBundleID from XCTestConfiguration: %@ " , exception);
63+ }
64+ });
65+
66+ // It is the primary app under test if the bundleID matches the static target,
67+ // or if it is nil/empty (representing the default init rig target).
68+ NSString *currentBundleID = self.bundleID ;
69+ BOOL isPrimaryApp = (gPrimaryAppBundleID .length == 0 ) || (currentBundleID.length == 0 ) ||
70+ [gPrimaryAppBundleID isEqualToString: currentBundleID];
71+
72+ NSArray <NSString *> *excludedRegexes =
73+ GREY_CONFIG_ARRAY (kGREYConfigKeyBundleIDsExcludedFromSetup );
74+ BOOL isExcluded = NO ;
75+ if (currentBundleID.length > 0 ) {
76+ NSError *error;
77+ for (NSString *regexStr in excludedRegexes) {
78+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: regexStr
79+ options: 0
80+ error: &error];
81+ GREYFatalAssertWithMessage (!error, @" Invalid regex:\" %@ \" . See error: %@ " , regexStr, error);
82+ NSRange firstMatch =
83+ [regex rangeOfFirstMatchInString: currentBundleID
84+ options: 0
85+ range: NSMakeRange (0 , [currentBundleID length ])];
86+ if (firstMatch.location != NSNotFound ) {
87+ isExcluded = YES ;
88+ break ;
89+ }
90+ }
91+ }
92+
93+ if (isExcluded) {
94+ GREYLog (@" Launching excluded application: %@ . Skipping EarlGrey setup." , currentBundleID);
95+ INVOKE_ORIGINAL_IMP (void , @selector (grey_launch ));
96+ return ;
97+ }
98+
99+ if (!isPrimaryApp) {
100+ GREYLog (@" WARNING: Launching secondary EarlGrey application: %@ . "
101+ @" Ensure it links EarlGrey AppFramework and matches your multi-app setup." ,
102+ currentBundleID);
103+ }
104+
55105 // Setup the Launch Environments.
56106 [self grey_configureApplicationForLaunch ];
57107 // Setup the Launch Arguments for eDO.
@@ -80,8 +130,8 @@ - (void)grey_launch {
80130 NSTimer *validTimer = AddTimerForLaunchTimeout ();
81131 INVOKE_ORIGINAL_IMP (void , @selector (grey_launch ));
82132 [validTimer invalidate ];
83- // When the identifier is @c nil or empty, it is the TestRig application being launched.
84- if (self. identifier . length == 0 ) {
133+
134+ if (isPrimaryApp ) {
85135 objc_setAssociatedObject ([XCUIApplication class ], @selector (greyTestRigName ), self.label ,
86136 OBJC_ASSOCIATION_RETAIN_NONATOMIC );
87137 }
0 commit comments