DomainDigUITests/AccessibilityScreenshotTests.swift

v5.5.5
domain-dig/DomainDigUITests/AccessibilityScreenshotTests.swift history · blame · raw

107 lines · 4609 bytes

  1import XCTest
  2
  3/// Captures the Phase-6 visual-pass evidence as result-bundle attachments:
  4/// the seeded dense screens in Light and Dark, and again at an accessibility
  5/// text size. Driven from XCUITest (not `simctl`) for two reasons proven during
  6/// this pass: the seed fixtures only populate under the test harness's launch,
  7/// and `xcrun simctl ui appearance` does not propagate to a headless-booted
  8/// simulator — so appearance is switched through the app's own Settings →
  9/// Display picker, exercising the real code path.
 10///
 11/// **This is a capture utility, not a pass/fail test.** Every step is
 12/// best-effort and never asserts: a control it cannot reach on a given runtime
 13/// simply yields no screenshot, so adding it to the audit suite can never gate
 14/// CI. Run with an explicit `-resultBundlePath` and export the attachments.
 15@MainActor
 16final class AccessibilityScreenshotTests: XCTestCase {
 17    override func setUp() {
 18        continueAfterFailure = true
 19    }
 20
 21    /// Dashboard, Watchlist, and batch results in Light then Dark.
 22    func testLightAndDarkScreens() {
 23        let app = AccessibilityAuditHarness.launch(seeded: true)
 24
 25        for appearance in ["Light", "Dark"] {
 26            guard setAppearance(app, to: appearance) else { continue }
 27            captureSeededScreens(app, suffix: appearance.lowercased())
 28        }
 29    }
 30
 31    /// The same seeded screens at the largest accessibility text size, where
 32    /// reflow and clipping surface. Launched fresh with the content-size arg.
 33    func testAccessibilityTextSizeScreens() {
 34        let app = AccessibilityAuditHarness.launch(
 35            contentSizeCategory: "UICTContentSizeCategoryAccessibilityXXXL",
 36            seeded: true
 37        )
 38        captureSeededScreens(app, suffix: "axxxl")
 39    }
 40
 41    // MARK: Capture (best-effort)
 42
 43    private func captureSeededScreens(_ app: XCUIApplication, suffix: String) {
 44        app.selectRootTab("Dashboard")
 45        _ = app.buttons["Refresh all tracked domains"].waitForExistence(timeout: 5)
 46        attach(app, name: "dashboard-\(suffix)")
 47
 48        let watchlist = app.buttons["Watchlist"]
 49        if watchlist.waitForExistence(timeout: 5) {
 50            watchlist.tap()
 51            _ = element(app, labelled: "healthy.example").waitForExistence(timeout: 5)
 52            attach(app, name: "watchlist-\(suffix)")
 53            // Pop the push, so a later pass's Dashboard capture is the
 54            // Dashboard rather than this view again.
 55            app.selectRootTab("Dashboard")
 56        }
 57
 58        app.selectRootTab("Inspect")
 59        _ = element(app, labelled: "broken.example").waitForExistence(timeout: 5)
 60        attach(app, name: "batch-\(suffix)")
 61    }
 62
 63    private func element(_ app: XCUIApplication, labelled label: String) -> XCUIElement {
 64        app.descendants(matching: .any)
 65            .matching(NSPredicate(format: "label == %@", label))
 66            .firstMatch
 67    }
 68
 69    private func attach(_ app: XCUIApplication, name: String) {
 70        let attachment = XCTAttachment(screenshot: app.screenshot())
 71        attachment.name = name
 72        attachment.lifetime = .keepAlways
 73        add(attachment)
 74    }
 75
 76    // MARK: Appearance (best-effort; returns whether it switched)
 77
 78    /// Drives Settings → Display → Appearance to the given option. The `Picker`
 79    /// renders as an inline `.menu` whose trigger is labelled
 80    /// "Appearance, <current value>". Returns `false` (rather than failing) if
 81    /// any step is unreachable on this runtime.
 82    private func setAppearance(_ app: XCUIApplication, to option: String) -> Bool {
 83        // A prior step may have left the Settings tab on a pushed view.
 84        // Re-selecting the active tab pops it back to root.
 85        app.selectRootTab("Settings")
 86        let display = app.buttons["Display"]
 87        if !display.waitForExistence(timeout: 2) {
 88            app.selectRootTab("Settings")
 89        }
 90        guard display.waitForExistence(timeout: 5) else { return false }
 91        display.tap()
 92
 93        let trigger = app.buttons
 94            .matching(NSPredicate(format: "label BEGINSWITH %@", "Appearance"))
 95            .firstMatch
 96        guard trigger.waitForExistence(timeout: 5) else { return false }
 97        trigger.tap()
 98
 99        // The popped menu exposes System / Light / Dark as buttons (or menu
100        // items on some runtimes).
101        let asButton = app.buttons[option]
102        let choice = asButton.waitForExistence(timeout: 3) ? asButton : app.menuItems[option]
103        guard choice.waitForExistence(timeout: 3) else { return false }
104        choice.tap()
105        return true
106    }
107}