diff --git a/testing/scenario_app/ios/Scenarios/ScenariosUITests/AppExtensionTests.m b/testing/scenario_app/ios/Scenarios/ScenariosUITests/AppExtensionTests.m index 70d4e0a190a3b..32555de44ef81 100644 --- a/testing/scenario_app/ios/Scenarios/ScenariosUITests/AppExtensionTests.m +++ b/testing/scenario_app/ios/Scenarios/ScenariosUITests/AppExtensionTests.m @@ -19,6 +19,11 @@ - (void)setUp { } - (void)testAppExtensionLaunching { + // Launch the Scenarios app first to ensure it's installed then close it. + XCUIApplication* app = [[XCUIApplication alloc] init]; + [app launch]; + [app terminate]; + [self.hostApplication launch]; XCUIElement* button = self.hostApplication.buttons[@"Open Share"]; if (![button waitForExistenceWithTimeout:10]) { @@ -27,15 +32,27 @@ - (void)testAppExtensionLaunching { } [button tap]; BOOL launchedExtensionInFlutter = NO; - // Custom share extension button (like the one in this test) does not have a unique - // identity. They are all identified as `XCElementSnapshotPrivilegedValuePlaceholder`. - // Loop through all the `XCElementSnapshotPrivilegedValuePlaceholder` and find the Flutter one. - for (int i = 0; i < self.hostApplication.collectionViews.cells.count; i++) { - XCUIElement* shareSheetCell = - [self.hostApplication.collectionViews.cells elementBoundByIndex:i]; - if (![shareSheetCell.label isEqualToString:@"XCElementSnapshotPrivilegedValuePlaceholder"]) { - continue; - } + + // Wait for first cell of share sheet to appear. + XCUIElement* firstCell = self.hostApplication.collectionViews.cells.firstMatch; + if (![firstCell waitForExistenceWithTimeout:10]) { + NSLog(@"%@", self.hostApplication.debugDescription); + XCTFail(@"Failed due to not able to find any cells with %@ seconds", @(10)); + } + + // Custom share extension button (like the one in this test) does not have a + // unique identity on older versions of iOS. They are all identified as + // `XCElementSnapshotPrivilegedValuePlaceholder`. On iOS 17, they are + // identified by name. Loop through all the buttons labeled + // `XCElementSnapshotPrivilegedValuePlaceholder` or `Scenarios` to find the + // Flutter one. + NSPredicate* cellPredicate = [NSPredicate + predicateWithFormat: + @"label == 'XCElementSnapshotPrivilegedValuePlaceholder' OR label = 'Scenarios'"]; + NSArray* shareSheetCells = + [self.hostApplication.collectionViews.cells matchingPredicate:cellPredicate] + .allElementsBoundByIndex; + for (XCUIElement* shareSheetCell in shareSheetCells) { [shareSheetCell tap]; XCUIElement* flutterView = self.hostApplication.otherElements[@"flutter_view"];