Skip to content

Commit bb5089a

Browse files
authored
fix(ios): find a popover dismiss region in one query instead of reading every element (#2901)
* fix(ios): find a popover dismiss region in one query instead of reading every element alert get/accept/dismiss with no alert walked every descendant of every window, one XCTest round trip per element plus a retry cycle for each element that vanished mid-walk. On the WebView lab it outran the 10 s alert budget and kept the runner main thread busy past 30 s, so the following wait saw RUNNER_BUSY on every poll until its deadline (smoke:webview-remote-content, #2491). * refactor(ios): share one dismiss-popup marker rule and widen the crowded-screen margin The query predicate, the alert-title filter, and the post-activation observation now all read one pattern. The crowded fixture grows to 500 labels so the absent alert test fails by a wide margin without the query (282 s against a 5 s cap), and a new test covers a window that is itself the marker, which the single containing query also returns. * chore(gates): run the alert resolution crowded-screen tests in the PR XCTest lane
1 parent 81aff32 commit bb5089a

6 files changed

Lines changed: 103 additions & 18 deletions

File tree

‎.github/workflows/ios.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ jobs:
201201
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertHittableProbeCompletingAfterDeadlineLeavesTheOriginalUntouched \
202202
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertActivationIgnoresAnAppThatNeverSettlesBeforeTheDeadline \
203203
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertActivationDoesNotWaitOutANotificationBanner \
204+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertResolutionWithoutAnAlertDoesNotReadEveryElementOfTheScreen \
205+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertResolutionFindsADismissPopupMarkerOnACrowdedScreen \
206+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertResolutionFindsAWindowThatIsItselfTheDismissPopupMarker \
204207
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testSystemModalProbeSliceSharesAndClampsToPlanDeadline \
205208
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testDispatchRecoverySkipsBookkeepingWhileXCTestChannelOccupied \
206209
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testBoundedSystemModalProbeTimeoutRecoversThenReleasesOnDrain \

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
the parser read only the old element, so every row resolved no stack at all. Both spellings now
99
parse through the same `id`/`ref` resolution, so a profile recorded with an older Xcode reports
1010
what it did before. (#2860)
11+
- Fixed (ios): `alert get`, `accept`, or `dismiss` with no alert on screen no longer reads every
12+
element of the app to look for a popover's dismiss region. That walk cost one XCTest round trip
13+
per element, plus XCTest's retry cycle for each element that vanished mid-walk. On a loading
14+
WebView it outran the 10 s alert budget and kept the runner's main thread busy for more than 30 s
15+
after the command failed, so later commands failed with `RUNNER_BUSY`. The dismiss region is now
16+
found with one predicate query per window set. (#2491)
1117
- Changed (apple): a read-only runner command is resent inside the same request only when the
1218
runner refused it as `RUNNER_BUSY`. Before, any `COMMAND_FAILED` carrying `details.retriable:
1319
true` was sent up to three times. That flag tells a caller's own poll, such as `wait`, to try

‎apple/runner/AgentDeviceRunner/AgentDeviceRunner/AgentDeviceRunnerApp.m‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,24 @@ - (void)viewDidLoad {
295295
]];
296296
}
297297

298+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-crowded-screen"]) {
299+
for (NSUInteger row = 0; row < 500; row++) {
300+
UILabel *rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60 + (row % 30) * 24, 400, 16)];
301+
rowLabel.text = [NSString stringWithFormat:@"Crowded row %lu", (unsigned long)row];
302+
rowLabel.accessibilityIdentifier = [NSString stringWithFormat:@"agent-device-crowded-row-%lu", (unsigned long)row];
303+
[self.view addSubview:rowLabel];
304+
}
305+
}
306+
307+
BOOL markedButton = [NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-dismiss-popup"];
308+
if (markedButton || [NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-dismiss-popup-window"]) {
309+
UIButton *dismissRegion = [UIButton buttonWithType:UIButtonTypeSystem];
310+
dismissRegion.accessibilityIdentifier = markedButton ? @" Dismiss Popup " : @"agent-device-close-popover";
311+
[dismissRegion setTitle:@"Close popover" forState:UIControlStateNormal];
312+
dismissRegion.frame = CGRectMake(40, 40, 200, 44);
313+
[self.view addSubview:dismissRegion];
314+
}
315+
298316
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-selector-read-regression"]) {
299317
NSString *const duplicateIdentifier = @"agent-device-selector-read-duplicate";
300318

@@ -338,6 +356,11 @@ - (void)scene:(UIScene *)scene
338356

339357
self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
340358
self.window.rootViewController = [[AgentDeviceRunnerViewController alloc] init];
359+
#if TARGET_OS_IOS
360+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-dismiss-popup-window"]) {
361+
self.window.accessibilityIdentifier = @"Dismiss popup";
362+
}
363+
#endif
341364
[self.window makeKeyAndVisible];
342365
}
343366

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Alert.swift‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,26 @@ extension RunnerTests {
161161
elements.first { isVisibleElement($0) }
162162
}
163163

164+
/// The marker is matched inside XCTest's query, so the screen is read once per query. Reading each
165+
/// descendant instead costs one round trip per element, and on a screen whose tree changes while
166+
/// it is read (a loading web view) each vanished element adds XCTest's retry cycle. `containing`
167+
/// also matches a window that is itself the marker.
164168
private func firstDismissPopupWindow(in app: XCUIApplication) -> XCUIElement? {
165-
safeElementsQuery {
166-
app.windows.allElementsBoundByIndex
167-
}.first { window in
168-
if !isVisibleElement(window) { return false }
169-
if isDismissPopupMarker(window.label) || isDismissPopupMarker(window.identifier) {
170-
return true
171-
}
172-
return safeElementsQuery {
173-
window.descendants(matching: .any).allElementsBoundByIndex
174-
}.contains { descendant in
175-
isDismissPopupMarker(descendant.label) || isDismissPopupMarker(descendant.identifier)
176-
}
177-
}
169+
firstExistingElement(in: safeElementsQuery {
170+
app.windows.containing(Self.dismissPopupMarker).allElementsBoundByIndex
171+
})
178172
}
179173

174+
/// The one definition of a popover's dismiss region: a label or identifier that reads "dismiss
175+
/// popup", in any case, with any surrounding whitespace. XCTest queries take it as a format predicate.
176+
private static let dismissPopupMarkerPattern = #"\s*dismiss popup\s*"#
177+
private static let dismissPopupMarker = NSPredicate(
178+
format: "label MATCHES[c] %@ OR identifier MATCHES[c] %@",
179+
dismissPopupMarkerPattern,
180+
dismissPopupMarkerPattern
181+
)
182+
private static let dismissPopupMarkerText = NSPredicate(format: "SELF MATCHES[c] %@", dismissPopupMarkerPattern)
183+
180184
private func chooseAlertButton(_ buttons: [XCUIElement], action: String) -> XCUIElement? {
181185
if action == "accept" {
182186
if let accept = buttons.first(where: { isAcceptButton($0.label) }) {
@@ -285,7 +289,7 @@ extension RunnerTests {
285289
return hittable
286290
}
287291

288-
private func isDismissPopupMarker(_ label: String) -> Bool {
289-
label.trimmingCharacters(in: .whitespacesAndNewlines).caseInsensitiveCompare("dismiss popup") == .orderedSame
292+
func isDismissPopupMarker(_ text: String) -> Bool {
293+
Self.dismissPopupMarkerText.evaluate(with: text)
290294
}
291295
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+AlertObservation.swift‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ extension RunnerTests {
6868
}
6969

7070
private func containsDismissPopupMarker(_ snapshot: XCUIElementSnapshot) -> Bool {
71-
[snapshot.label, snapshot.identifier].contains {
72-
$0.trimmingCharacters(in: .whitespacesAndNewlines).caseInsensitiveCompare("dismiss popup") == .orderedSame
73-
} || snapshot.children.contains { containsDismissPopupMarker($0) }
71+
[snapshot.label, snapshot.identifier].contains { isDismissPopupMarker($0) } ||
72+
snapshot.children.contains { containsDismissPopupMarker($0) }
7473
}
7574

7675
private func alertPresentation(_ snapshot: XCUIElementSnapshot) -> RunnerAlertPresentation {

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+AlertTests.swift‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,53 @@ extension RunnerTests {
77
}
88
}
99
#endif
10+
11+
#if AGENT_DEVICE_RUNNER_UNIT_TESTS && os(iOS)
12+
extension RunnerTests {
13+
func testAlertResolutionWithoutAnAlertDoesNotReadEveryElementOfTheScreen() throws {
14+
launchCrowdedScreen(extraArguments: [])
15+
defer { terminateCrowdedScreen() }
16+
17+
let startedAt = Date()
18+
XCTAssertNil(resolveAlert(app: app, deadline: startedAt.addingTimeInterval(RunnerTests.defaultAlertCommandTimeout)))
19+
// Half the command's own budget: an absent alert answers ALERT_NOT_FOUND well inside it on a
20+
// contended host, while a read per element spends it many times over on this screen.
21+
XCTAssertLessThan(Date().timeIntervalSince(startedAt), RunnerTests.defaultAlertCommandTimeout / 2)
22+
}
23+
24+
func testAlertResolutionFindsADismissPopupMarkerOnACrowdedScreen() throws {
25+
launchCrowdedScreen(extraArguments: ["--agent-device-dismiss-popup"])
26+
defer { terminateCrowdedScreen() }
27+
28+
let alert = try XCTUnwrap(
29+
resolveAlert(app: app, deadline: Date().addingTimeInterval(RunnerTests.defaultAlertCommandTimeout))
30+
)
31+
XCTAssertEqual(alert.source, .dismissPopup)
32+
XCTAssertEqual(alert.root.elementType, .window)
33+
XCTAssertTrue(alert.buttons.contains { $0.identifier == " Dismiss Popup " })
34+
}
35+
36+
func testAlertResolutionFindsAWindowThatIsItselfTheDismissPopupMarker() throws {
37+
launchCrowdedScreen(extraArguments: ["--agent-device-dismiss-popup-window"])
38+
defer { terminateCrowdedScreen() }
39+
40+
let alert = try XCTUnwrap(
41+
resolveAlert(app: app, deadline: Date().addingTimeInterval(RunnerTests.defaultAlertCommandTimeout))
42+
)
43+
XCTAssertEqual(alert.source, .dismissPopup)
44+
XCTAssertEqual(alert.root.identifier, "Dismiss popup")
45+
XCTAssertTrue(alert.buttons.contains { $0.identifier == "agent-device-close-popover" })
46+
}
47+
48+
private func launchCrowdedScreen(extraArguments: [String]) {
49+
app.launchArguments = ["--agent-device-crowded-screen"] + extraArguments
50+
app.launch()
51+
XCTAssertTrue(app.staticTexts["agent-device-crowded-row-499"].waitForExistence(timeout: appExistenceTimeout))
52+
}
53+
54+
private func terminateCrowdedScreen() {
55+
invalidateCachedTarget(reason: "unit_test_cleanup")
56+
app.terminate()
57+
}
58+
}
59+
#endif

0 commit comments

Comments
 (0)