Files
honeyDueKMP/iosApp/HoneyDueUITests/Framework/AuthenticatedUITestCase.swift
Trey T 4df8707b92 UI test infrastructure overhaul — 58% to 96% pass rate (231/241)
Major infrastructure changes:
- BaseUITestCase: per-suite app termination via class setUp() prevents
  stale state when parallel clones share simulators
- relaunchBetweenTests override for suites that modify login/onboarding state
- focusAndType: dedicated SecureTextField path handles iOS strong password
  autofill suggestions (Choose My Own Password / Not Now dialogs)
- LoginScreenObject: tapSignUp/tapForgotPassword use scrollIntoView for
  offscreen buttons instead of simple swipeUp
- Removed all coordinate taps from ForgotPasswordScreen, VerifyResetCodeScreen,
  ResetPasswordScreen (Rule 3 compliance)
- Removed all usleep calls from screen objects (Rule 14 compliance)

App fixes exposed by tests:
- ContractorsListView: added onDismiss to sheet for list refresh after save
- AllTasksView: added Task.RefreshButton accessibility identifier
- AccessibilityIdentifiers: added Task.refreshButton
- DocumentsWarrantiesView: onDismiss handler for document list refresh
- Various form views: textContentType, submitLabel, onSubmit for keyboard flow

Test fixes:
- PasswordResetTests: handle auto-login after reset (app skips success screen)
- AuthenticatedUITestCase: refreshTasks() helper for kanban toolbar button
- All pre-login suites use relaunchBetweenTests for test independence
- Deleted dead code: AuthenticatedTestCase, SeededTestData, SeedTests,
  CleanupTests, old Suite0/2/3, Suite1_RegistrationRebuildTests

10 remaining failures: 5 iOS strong password autofill (simulator env),
3 pull-to-refresh gesture on empty lists, 2 feature coverage edge cases.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 15:05:37 -05:00

218 lines
8.1 KiB
Swift

import XCTest
/// Base class for all tests that require a logged-in user.
class AuthenticatedUITestCase: BaseUITestCase {
// MARK: - Configuration (override in subclasses)
var testCredentials: (username: String, password: String) {
("testuser", "TestPass123!")
}
var needsAPISession: Bool { false }
var apiCredentials: (username: String, password: String) {
("admin", "test1234")
}
override var includeResetStateLaunchArgument: Bool { false }
// MARK: - API Session
private(set) var session: TestSession!
private(set) var cleaner: TestDataCleaner!
// MARK: - Lifecycle
override func setUpWithError() throws {
if needsAPISession {
guard TestAccountAPIClient.isBackendReachable() else {
throw XCTSkip("Backend not reachable at \(TestAccountAPIClient.baseURL)")
}
}
try super.setUpWithError()
// If already logged in (tab bar visible), skip the login flow
let tabBar = app.tabBars.firstMatch
if tabBar.waitForExistence(timeout: defaultTimeout) {
// Already logged in just set up API session if needed
if needsAPISession {
guard let apiSession = TestAccountManager.loginSeededAccount(
username: apiCredentials.username,
password: apiCredentials.password
) else {
XCTFail("Could not login API account '\(apiCredentials.username)'")
return
}
session = apiSession
cleaner = TestDataCleaner(token: apiSession.token)
}
return
}
// Not logged in do the full login flow
UITestHelpers.ensureLoggedOut(app: app)
loginToMainApp()
if needsAPISession {
guard let apiSession = TestAccountManager.loginSeededAccount(
username: apiCredentials.username,
password: apiCredentials.password
) else {
XCTFail("Could not login API account '\(apiCredentials.username)'")
return
}
session = apiSession
cleaner = TestDataCleaner(token: apiSession.token)
}
}
override func tearDownWithError() throws {
cleaner?.cleanAll()
try super.tearDownWithError()
}
// MARK: - Login
func loginToMainApp() {
let creds = testCredentials
UITestHelpers.ensureOnLoginScreen(app: app)
let login = LoginScreenObject(app: app)
login.waitForLoad(timeout: loginTimeout)
login.enterUsername(creds.username)
login.enterPassword(creds.password)
let loginButton = app.buttons[AccessibilityIdentifiers.Authentication.loginButton]
loginButton.waitForExistenceOrFail(timeout: defaultTimeout)
loginButton.tap()
waitForMainApp()
}
func waitForMainApp() {
let tabBar = app.tabBars.firstMatch
let verification = VerificationScreen(app: app)
let deadline = Date().addingTimeInterval(loginTimeout)
while Date() < deadline {
if tabBar.exists { break }
if verification.codeField.exists {
verification.enterCode(TestAccountAPIClient.debugVerificationCode)
verification.submitCode()
_ = tabBar.waitForExistence(timeout: loginTimeout)
break
}
RunLoop.current.run(until: Date().addingTimeInterval(0.5))
}
XCTAssertTrue(tabBar.exists, "Expected tab bar after login with '\(testCredentials.username)'")
}
// MARK: - Tab Navigation
func navigateToTab(_ label: String) {
let tabBar = app.tabBars.firstMatch
tabBar.waitForExistenceOrFail(timeout: navigationTimeout)
let tab = tabBar.buttons.containing(
NSPredicate(format: "label CONTAINS[c] %@", label)
).firstMatch
tab.waitForExistenceOrFail(timeout: navigationTimeout)
tab.tap()
// Verify navigation happened wait for isSelected (best effort, won't fail)
// sidebarAdaptable tabs sometimes need a moment
let selected = NSPredicate(format: "isSelected == true")
let exp = XCTNSPredicateExpectation(predicate: selected, object: tab)
let result = XCTWaiter().wait(for: [exp], timeout: navigationTimeout)
// If first tap didn't register, tap again
if result != .completed {
tab.tap()
_ = XCTWaiter().wait(for: [XCTNSPredicateExpectation(predicate: selected, object: tab)], timeout: navigationTimeout)
}
}
func navigateToResidences() { navigateToTab("Residences") }
func navigateToTasks() { navigateToTab("Tasks") }
func navigateToContractors() { navigateToTab("Contractors") }
func navigateToDocuments() { navigateToTab("Doc") }
func navigateToProfile() { navigateToTab("Profile") }
// MARK: - Pull to Refresh
func pullToRefresh() {
let scrollable = app.collectionViews.firstMatch.exists
? app.collectionViews.firstMatch
: app.scrollViews.firstMatch
guard scrollable.waitForExistence(timeout: defaultTimeout) else { return }
let start = scrollable.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.15))
let end = scrollable.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.85))
start.press(forDuration: 0.3, thenDragTo: end)
_ = app.activityIndicators.firstMatch.waitForNonExistence(timeout: navigationTimeout)
}
func pullToRefreshUntilVisible(_ element: XCUIElement, maxRetries: Int = 3) {
for _ in 0..<maxRetries {
if element.waitForExistence(timeout: defaultTimeout) { return }
pullToRefresh()
}
}
/// Tap the refresh button on the Tasks/Kanban screen (no pull-to-refresh on kanban).
func refreshTasks() {
let refreshButton = app.buttons[AccessibilityIdentifiers.Task.refreshButton]
if refreshButton.waitForExistence(timeout: defaultTimeout) && refreshButton.isEnabled {
refreshButton.tap()
_ = app.activityIndicators.firstMatch.waitForNonExistence(timeout: navigationTimeout)
}
}
// MARK: - Preconditions (Rule 17: validate assumptions via API before tests run)
/// Ensure at least one residence exists for the current user.
/// Required precondition for: task creation, document creation.
func ensureResidenceExists() {
guard let token = session?.token else { return }
if let residences = TestAccountAPIClient.listResidences(token: token),
!residences.isEmpty { return }
// No residence create one via API
let _ = TestDataSeeder.createResidence(token: token, name: "Precondition Home \(Int(Date().timeIntervalSince1970))")
}
/// Ensure the current user has a specific minimum of residences.
func ensureResidenceCount(minimum: Int) {
guard let token = session?.token else { return }
let existing = TestAccountAPIClient.listResidences(token: token) ?? []
for i in existing.count..<minimum {
let _ = TestDataSeeder.createResidence(token: token, name: "Precondition Home \(i) \(Int(Date().timeIntervalSince1970))")
}
}
// MARK: - Shared Helpers
/// Fill a text field by accessibility identifier. The ONE way to type into fields.
func fillTextField(identifier: String, text: String, file: StaticString = #filePath, line: UInt = #line) {
let field = app.textFields[identifier].firstMatch
field.waitForExistenceOrFail(timeout: defaultTimeout, file: file, line: line)
field.focusAndType(text, app: app, file: file, line: line)
}
/// Dismiss keyboard using the Return key or toolbar Done button.
func dismissKeyboard() {
let returnKey = app.keyboards.buttons["return"]
let doneKey = app.keyboards.buttons["Done"]
if returnKey.exists {
returnKey.tap()
} else if doneKey.exists {
doneKey.tap()
}
_ = app.keyboards.firstMatch.waitForNonExistence(timeout: defaultTimeout)
}
}