diff --git a/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift b/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift index 5573001ce8..cd626e8fcd 100644 --- a/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +++ b/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift @@ -1259,23 +1259,6 @@ final class RunnerTests: XCTestCase { center.press(forDuration: 0.05, thenDragTo: endPoint) } - private func aggregatedLabel(for element: XCUIElement, depth: Int = 0) -> String? { - if depth > 2 { return nil } - let text = element.label.trimmingCharacters(in: .whitespacesAndNewlines) - if !text.isEmpty { return text } - if let value = element.value { - let valueText = String(describing: value).trimmingCharacters(in: .whitespacesAndNewlines) - if !valueText.isEmpty { return valueText } - } - let children = element.children(matching: .any).allElementsBoundByIndex - for child in children { - if let childLabel = aggregatedLabel(for: child, depth: depth + 1) { - return childLabel - } - } - return nil - } - private func elementTypeName(_ type: XCUIElement.ElementType) -> String { switch type { case .application: return "Application" @@ -1334,9 +1317,16 @@ final class RunnerTests: XCTestCase { return DataPayload(nodes: nodes, truncated: truncated) } + let (flatSnapshots, snapshotRanges) = flattenedSnapshots(rootSnapshot) + let rootLaterNodes = laterSnapshots( + for: rootSnapshot, + in: flatSnapshots, + ranges: snapshotRanges + ) let rootLabel = aggregatedLabel(for: rootSnapshot) ?? rootSnapshot.label.trimmingCharacters(in: .whitespacesAndNewlines) let rootIdentifier = rootSnapshot.identifier.trimmingCharacters(in: .whitespacesAndNewlines) let rootValue = snapshotValueText(rootSnapshot) + let rootHittable = computedSnapshotHittable(rootSnapshot, viewport: viewport, laterNodes: rootLaterNodes) nodes.append( SnapshotNode( index: 0, @@ -1351,7 +1341,7 @@ final class RunnerTests: XCTestCase { height: Double(rootSnapshot.frame.size.height), ), enabled: rootSnapshot.isEnabled, - hittable: snapshotHittable(rootSnapshot), + hittable: rootHittable, depth: 0, ) ) @@ -1369,6 +1359,12 @@ final class RunnerTests: XCTestCase { let label = aggregatedLabel(for: snapshot) ?? snapshot.label.trimmingCharacters(in: .whitespacesAndNewlines) let identifier = snapshot.identifier.trimmingCharacters(in: .whitespacesAndNewlines) let valueText = snapshotValueText(snapshot) + let laterNodes = laterSnapshots( + for: snapshot, + in: flatSnapshots, + ranges: snapshotRanges + ) + let hittable = computedSnapshotHittable(snapshot, viewport: viewport, laterNodes: laterNodes) let hasContent = !label.isEmpty || !identifier.isEmpty || (valueText != nil) if !isVisibleInViewport(snapshot.frame, viewport) && !hasContent { continue @@ -1379,7 +1375,8 @@ final class RunnerTests: XCTestCase { label: label, identifier: identifier, valueText: valueText, - options: options + options: options, + hittable: hittable ) let key = "\(snapshot.elementType)-\(label)-\(identifier)-\(snapshot.frame.origin.x)-\(snapshot.frame.origin.y)" @@ -1411,7 +1408,7 @@ final class RunnerTests: XCTestCase { height: Double(snapshot.frame.size.height), ), enabled: snapshot.isEnabled, - hittable: snapshotHittable(snapshot), + hittable: hittable, depth: min(maxDepth, visibleDepth), ) ) @@ -1426,55 +1423,68 @@ final class RunnerTests: XCTestCase { return blocking } - let root = options.scope.flatMap { findScopeElement(app: app, scope: $0) } ?? app + let queryRoot = options.scope.flatMap { findScopeElement(app: app, scope: $0) } ?? app var nodes: [SnapshotNode] = [] var truncated = false let viewport = app.frame - func walk(_ element: XCUIElement, depth: Int) { + let rootSnapshot: XCUIElementSnapshot + do { + rootSnapshot = try queryRoot.snapshot() + } catch { + return DataPayload(nodes: nodes, truncated: truncated) + } + + let (flatSnapshots, snapshotRanges) = flattenedSnapshots(rootSnapshot) + + func walk(_ snapshot: XCUIElementSnapshot, depth: Int) { if nodes.count >= maxSnapshotElements { truncated = true return } if let limit = options.depth, depth > limit { return } - if !isVisibleInViewport(element.frame, viewport) { return } - - let label = aggregatedLabel(for: element) ?? element.label.trimmingCharacters(in: .whitespacesAndNewlines) - let identifier = element.identifier.trimmingCharacters(in: .whitespacesAndNewlines) - let valueText: String? = { - guard let value = element.value else { return nil } - let text = String(describing: value).trimmingCharacters(in: .whitespacesAndNewlines) - return text.isEmpty ? nil : text - }() - if shouldInclude(element: element, label: label, identifier: identifier, valueText: valueText, options: options) { + if !isVisibleInViewport(snapshot.frame, viewport) { return } + + let label = aggregatedLabel(for: snapshot) ?? snapshot.label.trimmingCharacters(in: .whitespacesAndNewlines) + let identifier = snapshot.identifier.trimmingCharacters(in: .whitespacesAndNewlines) + let valueText = snapshotValueText(snapshot) + let laterNodes = laterSnapshots( + for: snapshot, + in: flatSnapshots, + ranges: snapshotRanges + ) + let hittable = computedSnapshotHittable(snapshot, viewport: viewport, laterNodes: laterNodes) + if shouldInclude( + snapshot: snapshot, + label: label, + identifier: identifier, + valueText: valueText, + options: options, + hittable: hittable + ) { nodes.append( SnapshotNode( index: nodes.count, - type: elementTypeName(element.elementType), + type: elementTypeName(snapshot.elementType), label: label.isEmpty ? nil : label, identifier: identifier.isEmpty ? nil : identifier, value: valueText, - rect: SnapshotRect( - x: Double(element.frame.origin.x), - y: Double(element.frame.origin.y), - width: Double(element.frame.size.width), - height: Double(element.frame.size.height), - ), - enabled: element.isEnabled, - hittable: element.isHittable, + rect: snapshotRect(from: snapshot.frame), + enabled: snapshot.isEnabled, + hittable: hittable, depth: depth, ) ) } - let children = element.children(matching: .any).allElementsBoundByIndex + let children = snapshot.children for child in children { walk(child, depth: depth + 1) if truncated { return } } } - walk(root, depth: 0) + walk(rootSnapshot, depth: 0) return DataPayload(nodes: nodes, truncated: truncated) } @@ -1703,52 +1713,93 @@ final class RunnerTests: XCTestCase { } private func shouldInclude( - element: XCUIElement, + snapshot: XCUIElementSnapshot, label: String, identifier: String, valueText: String?, - options: SnapshotOptions + options: SnapshotOptions, + hittable: Bool ) -> Bool { - let type = element.elementType + let type = snapshot.elementType let hasContent = !label.isEmpty || !identifier.isEmpty || (valueText != nil) - if options.compact && type == .other && !hasContent && !element.isHittable { - let children = element.children(matching: .any).allElementsBoundByIndex - if children.count <= 1 { return false } + if options.compact && type == .other && !hasContent && !hittable { + if snapshot.children.count <= 1 { return false } } if options.interactiveOnly { if interactiveTypes.contains(type) { return true } - if element.isHittable && type != .other { return true } + if hittable && type != .other { return true } if hasContent { return true } return false } if options.compact { - return hasContent || element.isHittable + return hasContent || hittable } return true } - private func shouldInclude( - snapshot: XCUIElementSnapshot, - label: String, - identifier: String, - valueText: String?, - options: SnapshotOptions + private func computedSnapshotHittable( + _ snapshot: XCUIElementSnapshot, + viewport: CGRect, + laterNodes: ArraySlice ) -> Bool { - let type = snapshot.elementType - let hasContent = !label.isEmpty || !identifier.isEmpty || (valueText != nil) - if options.compact && type == .other && !hasContent && !snapshotHittable(snapshot) { - if snapshot.children.count <= 1 { return false } + guard snapshot.isEnabled else { return false } + let frame = snapshot.frame + if frame.isNull || frame.isEmpty { return false } + let center = CGPoint(x: frame.midX, y: frame.midY) + if !viewport.contains(center) { return false } + for node in laterNodes { + if !isOccludingType(node.elementType) { continue } + let nodeFrame = node.frame + if nodeFrame.isNull || nodeFrame.isEmpty { continue } + if nodeFrame.contains(center) { return false } } - if options.interactiveOnly { - if interactiveTypes.contains(type) { return true } - if snapshotHittable(snapshot) && type != .other { return true } - if hasContent { return true } + return true + } + + private func isOccludingType(_ type: XCUIElement.ElementType) -> Bool { + switch type { + case .application, .window: return false + default: + return true } - if options.compact { - return hasContent || snapshotHittable(snapshot) + } + + private func flattenedSnapshots( + _ root: XCUIElementSnapshot + ) -> ([XCUIElementSnapshot], [ObjectIdentifier: (Int, Int)]) { + var ordered: [XCUIElementSnapshot] = [] + var ranges: [ObjectIdentifier: (Int, Int)] = [:] + + @discardableResult + func visit(_ snapshot: XCUIElementSnapshot) -> Int { + let start = ordered.count + ordered.append(snapshot) + var end = start + for child in snapshot.children { + end = max(end, visit(child)) + } + ranges[ObjectIdentifier(snapshot)] = (start, end) + return end } - return true + + _ = visit(root) + return (ordered, ranges) + } + + private func laterSnapshots( + for snapshot: XCUIElementSnapshot, + in ordered: [XCUIElementSnapshot], + ranges: [ObjectIdentifier: (Int, Int)] + ) -> ArraySlice { + guard let (_, subtreeEnd) = ranges[ObjectIdentifier(snapshot)] else { + return ordered.suffix(from: ordered.count) + } + let nextIndex = subtreeEnd + 1 + if nextIndex >= ordered.count { + return ordered.suffix(from: ordered.count) + } + return ordered.suffix(from: nextIndex) } private func snapshotValueText(_ snapshot: XCUIElementSnapshot) -> String? { @@ -1757,11 +1808,6 @@ final class RunnerTests: XCTestCase { return text.isEmpty ? nil : text } - private func snapshotHittable(_ snapshot: XCUIElementSnapshot) -> Bool { - // XCUIElementSnapshot does not expose isHittable; use enabled as a lightweight proxy. - return snapshot.isEnabled - } - private func aggregatedLabel(for snapshot: XCUIElementSnapshot, depth: Int = 0) -> String? { if depth > 4 { return nil } let text = snapshot.label.trimmingCharacters(in: .whitespacesAndNewlines)