// CONTACTS ON IPHONE, AGAINST THE REAL CORE (a five-minute walk; assets/design/contacts/SCREENS.md): the app opens on // its home with the tabs drawn and each tab opens; then a contact is added, its page opens, a second card with the same // number makes a Same person? pair, a group is made with the picker, and the phone's contacts are read. Invented names // and +1 555 numbers only. The walk script grants Contacts access first, so the read needs no system prompt. import XCTest final class ContactsUITests: XCTestCase { override func setUp() { continueAfterFailure = false } @MainActor private func element(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch } @MainActor private func shot(_ app: XCUIApplication, _ name: String) { let attachment = XCTAttachment(screenshot: app.screenshot()) attachment.name = name attachment.lifetime = .keepAlways add(attachment) } /// Waits for a screen; a refused read (Try again) fails with the tree printed. @MainActor private func sees(_ app: XCUIApplication, _ id: String, _ what: String, timeout: TimeInterval = 20) { let found = element(app, id).waitForExistence(timeout: timeout) let refused = element(app, "contacts-try-again").exists if !found || refused { print("CONTACTS TREE \(what):\n\(app.debugDescription)") } XCTAssertFalse(refused, "\(what): the core refused the read") XCTAssertTrue(found, what) } /// A tap that lands: the first tap after launch can be lost, so a button that did not open its screen is tapped /// once more near its top. @MainActor private func open(_ app: XCUIApplication, _ button: String, _ screen: String, _ what: String) { let target = element(app, button) XCTAssertTrue(target.waitForExistence(timeout: 20), "\(what): \(button)") target.tap() if !element(app, screen).waitForExistence(timeout: 5) { target.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.25)).tap() } sees(app, screen, what, timeout: 15) } @MainActor private func back(_ app: XCUIApplication) { app.navigationBars.buttons.element(boundBy: 0).tap() } @MainActor private func type(_ app: XCUIApplication, _ id: String, _ text: String) { let field = element(app, id) XCTAssertTrue(field.waitForExistence(timeout: 10), id) field.tap() field.typeText(text) } /// Adds a contact through the form and lands on its page. @MainActor private func add(_ app: XCUIApplication, name: String, phone: String) { open(app, "contacts-add", "contacts-form", "the form") type(app, "contacts-form-name", name) type(app, "contacts-form-phones-0", phone) shot(app, "Contacts form \(name)") element(app, "contacts-form-save").tap() sees(app, "contacts-person", "the new contact's page") XCTAssertTrue(app.staticTexts[name].waitForExistence(timeout: 10), "the page names \(name)") } @MainActor func testItOpensOnItsHome() throws { let app = XCUIApplication() app.launch() sees(app, "contacts-home", "the home", timeout: 60) XCTAssertTrue(element(app, "contacts-tab-contacts").exists) Thread.sleep(forTimeInterval: 2) shot(app, "Contacts 1 home") for tab in ["groups", "circle", "you"] { open(app, "contacts-tab-\(tab)", "contacts-\(tab)", "the \(tab) tab") shot(app, "Contacts \(tab)") } open(app, "contacts-tab-contacts", "contacts-home", "back home") } @MainActor func testSectionF() throws { let app = XCUIApplication() app.launch() sees(app, "contacts-home", "the home", timeout: 60) Thread.sleep(forTimeInterval: 2) let stamp = String(Int(Date().timeIntervalSince1970) % 10_000) let name = "Orla Fenwick \(stamp)" let number = "+1555555\(String(format: "%04d", Int(stamp)!))" // Add: the form, saved, opens the new contact's page. add(app, name: name, phone: number) shot(app, "Contacts 2 person") back(app) sees(app, "contacts-home", "home after adding") // A person from the list: search, tap the row, Edit at the top right. type(app, "contacts-search", stamp) let row = app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH 'contacts-row-' AND label CONTAINS %@", stamp)).firstMatch XCTAssertTrue(row.waitForExistence(timeout: 15), "the row found by search") row.tap() sees(app, "contacts-person", "a person from the list") XCTAssertTrue(element(app, "contacts-person-edit").waitForExistence(timeout: 10), "Edit at the top right") back(app) let search = element(app, "contacts-search") search.tap() search.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: stamp.count) + "\n") // Same person?: a second card with the same number makes a pair. add(app, name: "O. Fenwick \(stamp)", phone: number) back(app) sees(app, "contacts-home", "home after the second card") open(app, "contacts-same-banner", "contacts-same", "Same person?") shot(app, "Contacts 3 same person") XCTAssertTrue(element(app, "contacts-same-later").waitForExistence(timeout: 10), "the pair's Later") element(app, "contacts-same-later").tap() Thread.sleep(forTimeInterval: 1) back(app) // Groups: New group, its name, the picker, the group. open(app, "contacts-tab-groups", "contacts-groups", "the groups tab") element(app, "contacts-group-new").tap() let alert = app.alerts.firstMatch XCTAssertTrue(alert.waitForExistence(timeout: 10), "the new group's name is asked") alert.textFields.firstMatch.typeText("Walk group \(stamp)") alert.buttons["Done"].firstMatch.tap() sees(app, "contacts-pick", "the picker") shot(app, "Contacts 4 picker") let pick = app.buttons.matching(NSPredicate(format: "label CONTAINS %@", stamp)).firstMatch XCTAssertTrue(pick.waitForExistence(timeout: 10), "the new contact in the picker") Thread.sleep(forTimeInterval: 1) pick.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap() element(app, "contacts-pick-done").tap() sees(app, "contacts-group", "the new group") XCTAssertTrue(app.staticTexts["Walk group \(stamp)"].waitForExistence(timeout: 10), "the group's name") shot(app, "Contacts 5 group") back(app) // the foot hides while a page is open over Groups sees(app, "contacts-groups", "Groups again, with the new group") open(app, "contacts-tab-contacts", "contacts-home", "back home") // Import: read the phone's contacts (access granted by the walk script), said, Done. if element(app, "contacts-import").waitForExistence(timeout: 5) { open(app, "contacts-import", "contacts-read", "the import screen") sees(app, "contacts-import-said", "the import said", timeout: 60) shot(app, "Contacts 6 import") element(app, "contacts-import-close").tap() sees(app, "contacts-home", "home after the import") } else { print("CONTACTS: this phone's contacts were read on an earlier walk; the home offers no import") } shot(app, "Contacts 7 home") } }