|
| 1 | +import XCTest |
| 2 | + |
| 3 | +extension RunnerTests { |
| 4 | + struct RunnerAlert { |
| 5 | + let root: XCUIElement |
| 6 | + let ownerApp: XCUIApplication |
| 7 | + let buttons: [XCUIElement] |
| 8 | + } |
| 9 | + |
| 10 | + func resolveAlert(app activeApp: XCUIApplication) -> RunnerAlert? { |
| 11 | + if let alert = firstExistingElement(in: activeApp.alerts.allElementsBoundByIndex) { |
| 12 | + return runnerAlert(root: alert, ownerApp: activeApp) |
| 13 | + } |
| 14 | + if let popup = firstDismissPopupWindow(in: activeApp) { |
| 15 | + return runnerAlert(root: popup, ownerApp: activeApp) |
| 16 | + } |
| 17 | +#if os(macOS) |
| 18 | + return nil |
| 19 | +#else |
| 20 | + if let systemModal = firstBlockingSystemModal(in: springboard) { |
| 21 | + return runnerAlert(root: systemModal, ownerApp: springboard) |
| 22 | + } |
| 23 | + return nil |
| 24 | +#endif |
| 25 | + } |
| 26 | + |
| 27 | + func handleAlert(_ alert: RunnerAlert, action: String) -> Response { |
| 28 | + if action == "accept" || action == "dismiss" { |
| 29 | + guard let button = chooseAlertButton(alert.buttons, action: action) else { |
| 30 | + return Response(ok: false, error: ErrorPayload(message: "alert \(action) button not found")) |
| 31 | + } |
| 32 | + let outcome = activateElement(app: alert.ownerApp, element: button, action: "alert \(action)") |
| 33 | + if let response = unsupportedResponse(for: outcome) { |
| 34 | + return response |
| 35 | + } |
| 36 | + return Response(ok: true, data: DataPayload(message: action == "accept" ? "accepted" : "dismissed")) |
| 37 | + } |
| 38 | + |
| 39 | + return Response( |
| 40 | + ok: true, |
| 41 | + data: DataPayload( |
| 42 | + message: preferredAlertTitle(alert.root, buttons: alert.buttons), |
| 43 | + items: alert.buttons.map { $0.label.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 44 | + ) |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + private func runnerAlert(root: XCUIElement, ownerApp: XCUIApplication) -> RunnerAlert? { |
| 49 | + let buttons = actionableElements(in: root).filter { isEnabledElement($0) } |
| 50 | + guard !buttons.isEmpty else { |
| 51 | + return nil |
| 52 | + } |
| 53 | + return RunnerAlert(root: root, ownerApp: ownerApp, buttons: buttons) |
| 54 | + } |
| 55 | + |
| 56 | + private func firstExistingElement(in elements: [XCUIElement]) -> XCUIElement? { |
| 57 | + elements.first { isVisibleElement($0) } |
| 58 | + } |
| 59 | + |
| 60 | + private func firstDismissPopupWindow(in app: XCUIApplication) -> XCUIElement? { |
| 61 | + safeElementsQuery { |
| 62 | + app.windows.allElementsBoundByIndex |
| 63 | + }.first { window in |
| 64 | + if !isVisibleElement(window) { return false } |
| 65 | + if isDismissPopupMarker(window.label) || isDismissPopupMarker(window.identifier) { |
| 66 | + return true |
| 67 | + } |
| 68 | + return safeElementsQuery { |
| 69 | + window.descendants(matching: .any).allElementsBoundByIndex |
| 70 | + }.contains { descendant in |
| 71 | + isDismissPopupMarker(descendant.label) || isDismissPopupMarker(descendant.identifier) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private func chooseAlertButton(_ buttons: [XCUIElement], action: String) -> XCUIElement? { |
| 77 | + if action == "accept" { |
| 78 | + if let accept = buttons.first(where: { isAcceptButton($0.label) }) { |
| 79 | + return accept |
| 80 | + } |
| 81 | + return buttons.count == 1 && !isDismissButton(buttons[0].label) ? buttons[0] : nil |
| 82 | + } |
| 83 | + |
| 84 | + return buttons.first(where: { isDismissButton($0.label) }) ?? buttons.last |
| 85 | + } |
| 86 | + |
| 87 | + private func isAcceptButton(_ label: String) -> Bool { |
| 88 | + let normalized = label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() |
| 89 | + return [ |
| 90 | + "ok", |
| 91 | + "allow", |
| 92 | + "yes", |
| 93 | + "continue", |
| 94 | + "done", |
| 95 | + "open settings" |
| 96 | + ].contains(normalized) || normalized.hasPrefix("confirm") |
| 97 | + } |
| 98 | + |
| 99 | + private func isDismissButton(_ label: String) -> Bool { |
| 100 | + [ |
| 101 | + "cancel", |
| 102 | + "close", |
| 103 | + "dismiss", |
| 104 | + "don't allow", |
| 105 | + "don’t allow", |
| 106 | + "not now", |
| 107 | + "no", |
| 108 | + "keep browsing", |
| 109 | + "later" |
| 110 | + ].contains(label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()) |
| 111 | + } |
| 112 | + |
| 113 | + private func preferredAlertTitle(_ element: XCUIElement, buttons: [XCUIElement]) -> String { |
| 114 | + let buttonLabels = Set(buttons.map { $0.label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() }) |
| 115 | + let descendants = element.descendants(matching: .any).allElementsBoundByIndex |
| 116 | + for descendant in descendants { |
| 117 | + let text = descendant.label.trimmingCharacters(in: .whitespacesAndNewlines) |
| 118 | + if text.isEmpty || |
| 119 | + isGenericAlertLabel(text) || |
| 120 | + buttonLabels.contains(text.lowercased()) || |
| 121 | + descendant.elementType == .navigationBar || |
| 122 | + actionableTypes.contains(descendant.elementType) |
| 123 | + { |
| 124 | + continue |
| 125 | + } |
| 126 | + return text |
| 127 | + } |
| 128 | + let label = element.label.trimmingCharacters(in: .whitespacesAndNewlines) |
| 129 | + return label.isEmpty || isGenericAlertLabel(label) ? "Alert" : label |
| 130 | + } |
| 131 | + |
| 132 | + private func isGenericAlertLabel(_ label: String) -> Bool { |
| 133 | + let normalized = label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() |
| 134 | + return isDismissPopupMarker(normalized) || |
| 135 | + normalized.hasPrefix("vertical scroll bar") || |
| 136 | + normalized.hasPrefix("horizontal scroll bar") || |
| 137 | + normalized == "tab bar" |
| 138 | + } |
| 139 | + |
| 140 | + private func isVisibleElement(_ element: XCUIElement) -> Bool { |
| 141 | + element.exists && !element.frame.isNull && !element.frame.isEmpty |
| 142 | + } |
| 143 | + |
| 144 | + private func isEnabledElement(_ element: XCUIElement) -> Bool { |
| 145 | + var enabled = false |
| 146 | + _ = RunnerObjCExceptionCatcher.catchException({ |
| 147 | + enabled = element.exists && element.isEnabled |
| 148 | + }) |
| 149 | + return enabled |
| 150 | + } |
| 151 | + |
| 152 | + private func isDismissPopupMarker(_ label: String) -> Bool { |
| 153 | + label.trimmingCharacters(in: .whitespacesAndNewlines).caseInsensitiveCompare("dismiss popup") == .orderedSame |
| 154 | + } |
| 155 | +} |
0 commit comments