// A walk by hand, written as steps rather than code, so a person walking an app as its owner will can take the next // step without a rebuild: `TEST_RUNNER_WALK="launch;;shot:first;;label:Settings;;shot:settings" xcodebuild // test-without-building … -only-testing:/HandWalkTests`. Every step keeps the screen (and, on `dump`, what // VoiceOver reads); a step that finds nothing is said in the log and the walk goes on, since the pictures are the // point. Without WALK it is skipped, so the test plans run nothing here. // // Steps, separated by ";;": // launch[:arg arg …] launch the app under test (its scheme's), with launch arguments // app: walk another app from here on (Safari, the springboard) // terminate end the app // tap: tap the first element with that identifier // label: tap the first element whose label is exactly those words (a button first) // has: tap the first element whose label contains those words // hold: a long press on it // type: type into whatever has focus // alert: tap a springboard alert's button with that label (iOS's own asks) // up | down | edge swipe up, swipe down, swipe in from the left edge (Back) // left | right swipe left, swipe right (a deck's next and previous card) // drag:x1,y1,x2,y2 drag between two points, in fractions of the screen // field: tap the text field with that placeholder (a web page's) // home the Home button (the app leaves the front) // delete: n backspaces into whatever has focus // wait: wait // await:[,s] wait until an element shows (what another phone did), at most s (120) seconds // gone:[,s] wait until an element is gone // typefrom: type a file's words (never a secret: XCTest logs what it types) // pastefile: put a file's words on the pasteboard for a Paste, never logged (a dev sign-in code); // with no path, empty it // shot: keep the screen as // dump: keep the element tree as text import UIKit import XCTest final class HandWalkTests: XCTestCase { @MainActor func testWalk() throws { guard let walk = ProcessInfo.processInfo.environment["WALK"], !walk.isEmpty else { throw XCTSkip("no WALK given") } continueAfterFailure = true var app = XCUIApplication() let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") var n = 0 func say(_ line: String) { print("HANDWALK \(line)") } func any(_ predicate: NSPredicate) -> XCUIElement { let button = app.buttons.matching(predicate).firstMatch return button.exists ? button : app.descendants(matching: .any).matching(predicate).firstMatch } func tapped(_ element: XCUIElement, _ what: String) { guard element.waitForExistence(timeout: 8) else { return say("MISS \(what)") } for _ in 0..<5 where !element.isHittable { app.swipeUp() } element.tap() say("tap \(what)") } for step in walk.components(separatedBy: ";;") { let parts = step.split(separator: ":", maxSplits: 1).map(String.init) let verb = parts.first?.trimmingCharacters(in: .whitespaces) ?? "" let rest = parts.count > 1 ? parts[1] : "" switch verb { case "launch": app.launchArguments = rest.isEmpty ? [] : rest.components(separatedBy: " ") app.launch() say("launch \(rest)") case "app": app = XCUIApplication(bundleIdentifier: rest) app.activate() case "terminate": app.terminate() case "openurl": // A link opened through the system, as another app hands it over: cold when the app is not running. if let url = URL(string: rest) { XCUIDevice.shared.system.open(url); say("openurl \(rest)") } else { say("MISS openurl \(rest)") } case "tap": // "albums-set-row-*" taps the first whose identifier starts so (a row keyed by an id the walk can't know). tapped(rest.hasSuffix("*") ? app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", String(rest.dropLast()))).firstMatch : app.descendants(matching: .any).matching(identifier: rest).firstMatch, rest) case "label": tapped(any(NSPredicate(format: "label == %@", rest)), rest) case "has": tapped(any(NSPredicate(format: "label CONTAINS %@", rest)), rest) case "hold": // "sites-row-*" holds the first whose identifier starts so (a row keyed by a fresh id). let element = rest.hasSuffix("*") ? app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", String(rest.dropLast()))).firstMatch : app.descendants(matching: .any).matching(identifier: rest).firstMatch if element.waitForExistence(timeout: 8) { element.press(forDuration: 0.9) } else { say("MISS \(rest)") } case "type": let focused = app.descendants(matching: .any).matching(NSPredicate(format: "hasKeyboardFocus == true")).firstMatch if focused.waitForExistence(timeout: 3) { app.typeText(rest) } else { say("MISS type (nothing has the keyboard)") } case "alert": let button = springboard.buttons[rest] if button.waitForExistence(timeout: 8) { button.tap(); say("alert \(rest)") } else { say("MISS alert \(rest)") } case "drag": // drag:x1,y1,x2,y2 in fractions of the screen (a fan turned at its own height). let v = rest.split(separator: ",").compactMap { Double($0.trimmingCharacters(in: .whitespaces)) } if v.count == 4 { let from = app.coordinate(withNormalizedOffset: CGVector(dx: v[0], dy: v[1])) from.press(forDuration: 0.05, thenDragTo: app.coordinate(withNormalizedOffset: CGVector(dx: v[2], dy: v[3]))) say("drag \(rest)") } else { say("MISS drag \(rest)") } case "field": // A text field by its placeholder (a web page's fields carry no identifier). let byPlaceholder = NSPredicate(format: "placeholderValue == %@", rest) let field = app.textFields.matching(byPlaceholder).firstMatch tapped(field.exists ? field : app.secureTextFields.matching(byPlaceholder).firstMatch, rest) case "home": XCUIDevice.shared.press(.home) case "delete": app.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: Int(rest) ?? 1)) say("delete \(rest)") case "left": app.swipeLeft() case "right": app.swipeRight() case "up": app.swipeUp() case "down": app.swipeDown() case "edge": let start = app.windows.firstMatch.coordinate(withNormalizedOffset: CGVector(dx: 0.01, dy: 0.5)) start.press(forDuration: 0.05, thenDragTo: start.withOffset(CGVector(dx: 300, dy: 0))) case "wait": Thread.sleep(forTimeInterval: Double(rest) ?? 1) case "await": // await:[,seconds]: wait until it shows (another phone's act: a ring, a hang-up), at most 120 s. let bits = rest.split(separator: ",").map(String.init) let element = app.descendants(matching: .any).matching(identifier: bits[0]).firstMatch if element.waitForExistence(timeout: Double(bits.count > 1 ? bits[1] : "") ?? 120) { say("seen \(bits[0])") } else { say("MISS await \(bits[0])") } case "gone": // gone:[,seconds]: wait until it is no longer there (the call screen after the other side hung up). let bits = rest.split(separator: ",").map(String.init) let element = app.descendants(matching: .any).matching(identifier: bits[0]).firstMatch let until = Date().addingTimeInterval(Double(bits.count > 1 ? bits[1] : "") ?? 120) while element.exists, Date() < until { Thread.sleep(forTimeInterval: 0.5) } say(element.exists ? "MISS gone \(bits[0])" : "gone \(bits[0])") case "typefrom": // A file's words typed (a walk's invented code a helper fetched mid-walk); XCTest logs what it types. let words = (try? String(contentsOfFile: rest, encoding: .utf8))?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" if words.isEmpty { say("MISS typefrom \(rest)") } else { app.typeText(words); say("typefrom") } case "pastefile": // A file's words put on the pasteboard, for the walk's Paste: never typed (XCTest logs what it // types) and never said in the log (the dev sign-in code). `pastefile:` with no path empties it. let words = rest.isEmpty ? "" : (try? String(contentsOfFile: rest, encoding: .utf8))?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" UIPasteboard.general.string = words if !rest.isEmpty, words.isEmpty { say("MISS pastefile: nothing readable") } else { say("pastefile") } case "shot": n += 1 let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot()) shot.name = String(format: "hw-%02d-%@", n, rest) shot.lifetime = .keepAlways add(shot) case "dump": let tree = XCTAttachment(string: app.debugDescription) tree.name = "hwdump-\(rest)" tree.lifetime = .keepAlways add(tree) default: say("UNKNOWN \(step)") } } } }