SPB Git

Repository Public

SystemShots.swift 9.9 KB 191 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/SystemUITests/SystemShots.swift3// Description: Simulator-only UI automation for the system-integration screenshots (driven by Scripts/system-shots.sh): grants the4// notification permission, opens Settings › Notifications, waits on the Home Screen for the pushes the script delivers with5// `xcrun simctl push` (banner, expanded actions), adds the widgets and shares a photo to the share extension. It talks to the host6// through a marker file (`push:<name>`, `shot:<name>`): the script delivers the push or captures the simulator screen.7import XCTest89@MainActor10final class SystemShots: XCTestCase {11    private let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")12    private let app = XCUIApplication(bundleIdentifier: "ai.spboucher.spmail.ios.dev")13    private var marker: URL { URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("spm-marker.txt") }14    private var counter = 01516    /// Asks the host script for something and leaves it the time to do it.17    private func signal(_ what: String, wait seconds: Double = 2.5) {18        counter += 119        try? "\(counter) \(what)".write(to: marker, atomically: true, encoding: .utf8)20        Thread.sleep(forTimeInterval: seconds)21    }22    private func shot(_ name: String) { signal("shot:\(name)", wait: 2) }23    private func tapIfPresent(_ element: XCUIElement, timeout: TimeInterval = 4) -> Bool {24        guard element.waitForExistence(timeout: timeout) else { return false }25        element.tap()26        return true27    }28    private func allowNotifications() {29        for label in ["Autoriser", "Allow"] where tapIfPresent(springboard.buttons[label], timeout: 6) { return }30    }31    private func launchApp(extra: [String] = []) {32        let env = ProcessInfo.processInfo.environment33        app.launchArguments = ["-spmBaseURL", env["SPM_BASE_URL"] ?? "", "-spmDevToken", env["SPM_TOKEN"] ?? "", "-spmFakePushToken", "YES"] + extra34        app.launch()35    }3637    /// Settings › Notifications, then the notification banners with their actions.38    func testNotifications() throws {39        launchApp(extra: ["-spmOpenSettings", "YES"])40        allowNotifications()41        let row = app.staticTexts["Notifications"].firstMatch42        if !row.waitForExistence(timeout: 40) { shot("debug-no-settings"); XCTFail("the Settings list did not appear"); return }43        Thread.sleep(forTimeInterval: 6) // first sync, device registration44        row.tap()45        Thread.sleep(forTimeInterval: 4)46        shot("settings-notifications")47        app.swipeUp()48        shot("settings-notifications-2")49        app.swipeUp()50        shot("settings-notifications-3")51        // close Settings: the conversation opened by the notification tap below must be visible52        app.navigationBars.buttons.firstMatch.tap()53        Thread.sleep(forTimeInterval: 1)54        for label in ["OK"] where tapIfPresent(app.buttons[label], timeout: 3) { break }5556        XCUIDevice.shared.press(.home)57        Thread.sleep(forTimeInterval: 2)58        // the simulator posts a banner between one and ten seconds after the push (notification summary step): wait for it59        signal("push:new", wait: 0.5)60        if let banner = waitForBanner() {61            shot("banner-new")62            banner.press(forDuration: 1.3)63            Thread.sleep(forTimeInterval: 2)64            shot("banner-new-actions")65            // run one action for real: « Marquer comme lu » (the script checks the server afterwards)66            for label in ["Marquer comme lu", "Mark as Read"] where tapIfPresent(springboard.buttons[label], timeout: 2) { break }67            Thread.sleep(forTimeInterval: 8)68        } else { shot("debug-no-banner") }69        XCUIDevice.shared.press(.home)70        Thread.sleep(forTimeInterval: 2)71        signal("push:reminder", wait: 0.5)72        if let banner = waitForBanner() {73            shot("banner-reminder")74            banner.press(forDuration: 1.3)75            Thread.sleep(forTimeInterval: 2)76            shot("banner-reminder-actions")77            springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.95)).tap()78        }79        XCUIDevice.shared.press(.home)80        Thread.sleep(forTimeInterval: 2)81        // a tap on a notification opens the exact conversation82        signal("push:new2", wait: 0.5)83        if let banner = waitForBanner() { banner.tap(); Thread.sleep(forTimeInterval: 7); shot("tap-opens-conversation") }84        signal("done", wait: 1)85    }8687    /// The banner of a notification just pushed (its sender is the test mailbox « Apple Essai A »).88    private func waitForBanner() -> XCUIElement? {89        let named = springboard.otherElements["NotificationShortLookView"].firstMatch90        let byText = springboard.staticTexts.matching(NSPredicate(format: "label CONTAINS 'Apple Essai A'")).firstMatch91        let end = Date().addingTimeInterval(22)92        while Date() < end {93            if named.exists { return named }94            if byText.exists { return byText }95            Thread.sleep(forTimeInterval: 0.4)96        }97        return nil98    }99100    /// The widgets in the gallery and on the Home Screen.101    func testWidgets() throws {102        launchApp()103        allowNotifications()104        Thread.sleep(forTimeInterval: 12) // sync, snapshot written, timelines reloaded105        XCUIDevice.shared.press(.home)106        Thread.sleep(forTimeInterval: 1.5)107        XCUIDevice.shared.press(.home)108        springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.72)).press(forDuration: 2.0)109        Thread.sleep(forTimeInterval: 1)110        for label in ["Modifier", "Edit"] where tapIfPresent(springboard.buttons[label], timeout: 3) { break }111        for label in ["Ajouter un widget", "Add Widget"] where tapIfPresent(springboard.buttons[label], timeout: 3) { break }112        Thread.sleep(forTimeInterval: 2)113        shot("widget-gallery-root")114        let search = springboard.searchFields.firstMatch115        if search.waitForExistence(timeout: 5) {116            search.tap()117            Thread.sleep(forTimeInterval: 2)118            if !springboard.keyboards.firstMatch.exists { search.coordinate(withNormalizedOffset: CGVector(dx: 0.4, dy: 0.5)).tap(); Thread.sleep(forTimeInterval: 2) }119            springboard.typeText("SP Mail")120            Thread.sleep(forTimeInterval: 2)121            shot("widget-gallery-search")122            let cell = springboard.staticTexts.matching(NSPredicate(format: "label BEGINSWITH 'SP Mail'")).firstMatch123            if cell.waitForExistence(timeout: 4) {124                cell.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()125                Thread.sleep(forTimeInterval: 3)126                shot("widget-gallery-inbox")127                for page in 1...4 {128                    springboard.swipeLeft()129                    Thread.sleep(forTimeInterval: 1.5)130                    shot("widget-gallery-\(page)")131                }132                for label in ["Ajouter le widget", "Add Widget"] where tapIfPresent(springboard.buttons[label], timeout: 3) { break }133                Thread.sleep(forTimeInterval: 2)134                for label in ["OK", "Terminé", "Done"] where tapIfPresent(springboard.buttons[label], timeout: 3) { break }135                Thread.sleep(forTimeInterval: 2)136                shot("home-widget")137                springboard.swipeLeft()138                Thread.sleep(forTimeInterval: 2)139                shot("home-widget-2")140            }141        }142        signal("done", wait: 1)143    }144145    /// The share sheet: a photo shared from Photos into the SP Mail share extension.146    func testShare() throws {147        launchApp()148        allowNotifications()149        Thread.sleep(forTimeInterval: 10) // the session token and the state reach the shared keychain group150        let photos = XCUIApplication(bundleIdentifier: "com.apple.mobileslideshow")151        photos.launch()152        Thread.sleep(forTimeInterval: 4)153        for label in ["Continuer", "Continue"] where tapIfPresent(photos.buttons[label], timeout: 3) { break }154        photos.coordinate(withNormalizedOffset: CGVector(dx: 0.17, dy: 0.23)).tap()155        Thread.sleep(forTimeInterval: 3)156        shot("debug-photo-open")157        let share = photos.buttons.matching(NSPredicate(format: "label CONTAINS[c] 'Partager' OR label CONTAINS[c] 'Share' OR identifier CONTAINS[c] 'share'")).firstMatch158        if share.waitForExistence(timeout: 5) { share.tap() }159        Thread.sleep(forTimeInterval: 3)160        shot("share-sheet")161        var target = photos.cells.matching(NSPredicate(format: "label CONTAINS 'SP Mail'")).firstMatch162        if !target.waitForExistence(timeout: 3) {163            for label in ["Plus", "More"] where tapIfPresent(photos.cells[label], timeout: 2) { break }164            Thread.sleep(forTimeInterval: 2)165            shot("share-sheet-more")166            target = photos.staticTexts.matching(NSPredicate(format: "label CONTAINS 'SP Mail'")).firstMatch167        }168        if target.waitForExistence(timeout: 4) {169            target.tap()170            Thread.sleep(forTimeInterval: 5)171            shot("share-extension")172            let to = photos.textFields.firstMatch173            if to.waitForExistence(timeout: 3) {174                to.tap()175                to.typeText(ProcessInfo.processInfo.environment["SPM_SHARE_TO"] ?? "")176                Thread.sleep(forTimeInterval: 1)177                shot("share-extension-filled")178                for label in ["Enregistrer le brouillon dans SP Mail", "Save the Draft in SP Mail"] where tapIfPresent(photos.buttons[label], timeout: 3) { break }179                Thread.sleep(forTimeInterval: 4)180                shot("share-extension-queued")181                for label in ["OK"] where tapIfPresent(photos.buttons[label], timeout: 3) { break }182            }183        }184        // back in the app: the queued draft is imported and opens in the composer185        app.activate()186        Thread.sleep(forTimeInterval: 8)187        shot("share-draft-in-app")188        signal("done", wait: 1)189    }190}191
Files main
Menu

Theme