Rewrite all UI tests following fail-fast TEST_RULES patterns
Rewrote 60+ test files to follow honeydue-style test guidelines:
- defaultTimeout=2s, navigationTimeout=5s — fail fast, no long waits
- No coordinate taps (except onboarding paged TabView swipes)
- No sleep(), no retry loops
- No guard...else { return } silent passes — XCTFail everywhere
- All elements by accessibility ID via UITestID constants
- Screen objects for all navigation/actions/assertions
- One logical assertion per test method
Added missing accessibility identifiers to app views:
- MonthView.swift: added AccessibilityID.MonthView.grid to ScrollView
- YearView.swift: added AccessibilityID.YearView.heatmap to ScrollView
Framework rewrites:
- BaseUITestCase: added session ID, localeArguments, extraLaunchArguments
- WaitHelpers: waitForExistenceOrFail, waitUntilHittableOrFail,
waitForNonExistence, scrollIntoView, forceTap
- All 7 screen objects rewritten with fail-fast semantics
- TEST_RULES.md added with non-negotiable rules
Known remaining issues:
- OnboardingTests: paged TabView swipes unreliable on iOS 26 simulator
- SettingsLegalLinksTests: EULA/Privacy buttons too deep in DEBUG scroll
- Customization horizontal picker scrolling needs further tuning
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,8 +3,7 @@
|
||||
// Tests iOS
|
||||
//
|
||||
// Base class for all UI tests. Handles launch arguments,
|
||||
// state reset, screenshot capture on failure, and parallel
|
||||
// test isolation via per-session data sandboxing.
|
||||
// parallel test isolation, and screenshot capture on failure.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
@@ -13,6 +12,11 @@ class BaseUITestCase: XCTestCase {
|
||||
|
||||
var app: XCUIApplication!
|
||||
|
||||
/// Element on current screen — if it's not there in 2s, the app is broken
|
||||
let defaultTimeout: TimeInterval = 2
|
||||
/// Screen transitions, tab switches
|
||||
let navigationTimeout: TimeInterval = 5
|
||||
|
||||
// MARK: - Parallel Test Isolation
|
||||
|
||||
/// Unique session ID for this test class instance.
|
||||
@@ -34,13 +38,10 @@ class BaseUITestCase: XCTestCase {
|
||||
/// Whether to force the trial to be expired. Default: false.
|
||||
var expireTrial: Bool { false }
|
||||
|
||||
/// Override to change the test locale/language.
|
||||
/// Default: English (US). Locale tests override this instead of setUp().
|
||||
/// Override to change the test locale/language. Default: English (US).
|
||||
var localeArguments: [String] { ["-AppleLanguages", "(en)", "-AppleLocale", "en_US"] }
|
||||
|
||||
/// Extra launch arguments for tests needing special settings
|
||||
/// (accessibility sizes, reduce motion, high contrast, etc.).
|
||||
/// Override in subclasses instead of overriding setUp().
|
||||
/// Extra launch arguments (accessibility sizes, reduce motion, etc.).
|
||||
var extraLaunchArguments: [String] { [] }
|
||||
|
||||
// MARK: - Lifecycle
|
||||
@@ -65,18 +66,10 @@ class BaseUITestCase: XCTestCase {
|
||||
private func buildLaunchArguments(resetState: Bool) -> [String] {
|
||||
var args = ["--ui-testing", "--disable-animations"]
|
||||
args.append(contentsOf: localeArguments)
|
||||
if resetState {
|
||||
args.append("--reset-state")
|
||||
}
|
||||
if bypassSubscription {
|
||||
args.append("--bypass-subscription")
|
||||
}
|
||||
if skipOnboarding {
|
||||
args.append("--skip-onboarding")
|
||||
}
|
||||
if expireTrial {
|
||||
args.append("--expire-trial")
|
||||
}
|
||||
if resetState { args.append("--reset-state") }
|
||||
if bypassSubscription { args.append("--bypass-subscription") }
|
||||
if skipOnboarding { args.append("--skip-onboarding") }
|
||||
if expireTrial { args.append("--expire-trial") }
|
||||
args.append(contentsOf: extraLaunchArguments)
|
||||
return args
|
||||
}
|
||||
@@ -84,9 +77,7 @@ class BaseUITestCase: XCTestCase {
|
||||
private func buildLaunchEnvironment() -> [String: String] {
|
||||
var env = [String: String]()
|
||||
env["UI_TEST_SESSION_ID"] = testSessionID
|
||||
if let fixture = seedFixture {
|
||||
env["UI_TEST_FIXTURE"] = fixture
|
||||
}
|
||||
if let fixture = seedFixture { env["UI_TEST_FIXTURE"] = fixture }
|
||||
return env
|
||||
}
|
||||
|
||||
@@ -99,7 +90,7 @@ class BaseUITestCase: XCTestCase {
|
||||
add(screenshot)
|
||||
}
|
||||
|
||||
// MARK: - Shared Test Utilities
|
||||
// MARK: - Launch Helpers
|
||||
|
||||
@discardableResult
|
||||
func launchApp(resetState: Bool) -> XCUIApplication {
|
||||
@@ -110,8 +101,7 @@ class BaseUITestCase: XCTestCase {
|
||||
return application
|
||||
}
|
||||
|
||||
/// Relaunch the app with custom bypass setting, preserving the session ID.
|
||||
/// Use when a test needs to toggle subscription bypass mid-test.
|
||||
/// Relaunch with a different bypass setting, preserving session ID.
|
||||
@discardableResult
|
||||
func relaunchApp(resetState: Bool, bypassSubscription overrideBypass: Bool) -> XCUIApplication {
|
||||
app.terminate()
|
||||
@@ -137,10 +127,4 @@ class BaseUITestCase: XCTestCase {
|
||||
app = relaunched
|
||||
return relaunched
|
||||
}
|
||||
|
||||
func assertDayContentVisible(timeout: TimeInterval = 8, file: StaticString = #file, line: UInt = #line) {
|
||||
let hasEntry = app.firstEntryRow.waitForExistence(timeout: timeout)
|
||||
let hasMoodHeader = app.element(UITestID.Day.moodHeader).waitForExistence(timeout: 2)
|
||||
XCTAssertTrue(hasEntry || hasMoodHeader, "Day view should show entry list or mood header", file: file, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
// WaitHelpers.swift
|
||||
// Tests iOS
|
||||
//
|
||||
// Centralized, explicit wait helpers. No sleep() allowed.
|
||||
// Centralized wait helpers and element extensions. No sleep() allowed.
|
||||
// Follows fail-fast principles: if an element isn't there, fail immediately.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
// MARK: - Test Accessibility Identifiers (mirrors AccessibilityID in app target)
|
||||
|
||||
enum UITestID {
|
||||
enum Tab {
|
||||
static let day = "tab_day"
|
||||
@@ -33,7 +36,6 @@ enum UITestID {
|
||||
static let browseThemesButton = "browse_themes_button"
|
||||
static let clearDataButton = "settings_clear_data"
|
||||
static let analyticsToggle = "settings_analytics_toggle"
|
||||
static let bypassSubscriptionToggle = "settings_bypass_subscription"
|
||||
static let eulaButton = "settings_eula"
|
||||
static let privacyPolicyButton = "settings_privacy_policy"
|
||||
}
|
||||
@@ -104,72 +106,99 @@ enum UITestID {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - XCUIElement Extensions (fail-fast, no retry loops)
|
||||
|
||||
extension XCUIElement {
|
||||
|
||||
/// Wait for the element to exist in the hierarchy.
|
||||
/// - Parameters:
|
||||
/// - timeout: Maximum seconds to wait.
|
||||
/// - message: Custom failure message.
|
||||
/// - Returns: `true` if the element exists within the timeout.
|
||||
/// Wait for element to exist; XCTFail if it doesn't.
|
||||
@discardableResult
|
||||
func waitForExistence(timeout: TimeInterval = 5, message: String? = nil) -> Bool {
|
||||
let result = waitForExistence(timeout: timeout)
|
||||
if !result, let message = message {
|
||||
XCTFail(message)
|
||||
func waitForExistenceOrFail(
|
||||
timeout: TimeInterval,
|
||||
message: String? = nil,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) -> XCUIElement {
|
||||
if !waitForExistence(timeout: timeout) {
|
||||
XCTFail(message ?? "Expected element to exist: \(self)", file: file, line: line)
|
||||
}
|
||||
return result
|
||||
return self
|
||||
}
|
||||
|
||||
/// Wait until the element is hittable (exists and is enabled/visible).
|
||||
/// - Parameter timeout: Maximum seconds to wait.
|
||||
/// Wait for element to become hittable; XCTFail if it doesn't.
|
||||
@discardableResult
|
||||
func waitUntilHittable(timeout: TimeInterval = 5) -> Bool {
|
||||
let predicate = NSPredicate(format: "isHittable == true")
|
||||
func waitUntilHittableOrFail(
|
||||
timeout: TimeInterval,
|
||||
message: String? = nil,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) -> XCUIElement {
|
||||
let predicate = NSPredicate(format: "exists == true AND isHittable == true")
|
||||
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: self)
|
||||
let result = XCTWaiter.wait(for: [expectation], timeout: timeout)
|
||||
return result == .completed
|
||||
let result = XCTWaiter().wait(for: [expectation], timeout: timeout)
|
||||
if result != .completed {
|
||||
XCTFail(message ?? "Expected element to become hittable: \(self)", file: file, line: line)
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
/// Tap the element after waiting for it to become hittable.
|
||||
/// - Parameter timeout: Maximum seconds to wait before tapping.
|
||||
func tapWhenReady(timeout: TimeInterval = 5, file: StaticString = #file, line: UInt = #line) {
|
||||
guard waitForExistence(timeout: timeout) else {
|
||||
XCTFail("Element \(identifier) not found after \(timeout)s", file: file, line: line)
|
||||
return
|
||||
}
|
||||
if isHittable {
|
||||
tap()
|
||||
return
|
||||
}
|
||||
|
||||
// Coordinate tap fallback for iOS 26 overlays where XCUI reports false-negative hittability.
|
||||
coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()
|
||||
}
|
||||
|
||||
/// Wait for the element to disappear from the hierarchy.
|
||||
/// - Parameter timeout: Maximum seconds to wait.
|
||||
/// Wait for element to disappear; XCTFail if it doesn't.
|
||||
@discardableResult
|
||||
func waitForDisappearance(timeout: TimeInterval = 5) -> Bool {
|
||||
func waitForNonExistence(
|
||||
timeout: TimeInterval,
|
||||
message: String? = nil,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) -> Bool {
|
||||
let predicate = NSPredicate(format: "exists == false")
|
||||
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: self)
|
||||
let result = XCTWaiter.wait(for: [expectation], timeout: timeout)
|
||||
return result == .completed
|
||||
let result = XCTWaiter().wait(for: [expectation], timeout: timeout)
|
||||
if result != .completed {
|
||||
XCTFail(message ?? "Expected element to disappear: \(self)", file: file, line: line)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// Scroll element into view within a scrollable container. Fail-fast if not found.
|
||||
func scrollIntoView(
|
||||
in container: XCUIElement,
|
||||
direction: SwipeDirection = .up,
|
||||
maxSwipes: Int = 5,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
if exists && isHittable { return }
|
||||
|
||||
for _ in 0..<maxSwipes {
|
||||
switch direction {
|
||||
case .up: container.swipeUp()
|
||||
case .down: container.swipeDown()
|
||||
case .left: container.swipeLeft()
|
||||
case .right: container.swipeRight()
|
||||
}
|
||||
if exists && isHittable { return }
|
||||
}
|
||||
|
||||
XCTFail("Failed to scroll element into view after \(maxSwipes) swipes: \(self)", file: file, line: line)
|
||||
}
|
||||
|
||||
/// Tap the element if it exists; XCTFail otherwise.
|
||||
func forceTap(file: StaticString = #filePath, line: UInt = #line) {
|
||||
guard exists else {
|
||||
XCTFail("Element does not exist for tap: \(self)", file: file, line: line)
|
||||
return
|
||||
}
|
||||
tap()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - XCUIApplication Extensions
|
||||
|
||||
extension XCUIApplication {
|
||||
|
||||
/// Find any element matching an accessibility identifier.
|
||||
func element(_ identifier: String) -> XCUIElement {
|
||||
let element = descendants(matching: .any).matching(identifier: identifier).firstMatch
|
||||
return element
|
||||
}
|
||||
|
||||
/// Wait for any element matching the identifier to exist.
|
||||
func waitForElement(identifier: String, timeout: TimeInterval = 5) -> XCUIElement {
|
||||
let element = element(identifier)
|
||||
_ = element.waitForExistence(timeout: timeout)
|
||||
return element
|
||||
descendants(matching: .any).matching(identifier: identifier).firstMatch
|
||||
}
|
||||
|
||||
var entryRows: XCUIElementQuery {
|
||||
@@ -180,61 +209,26 @@ extension XCUIApplication {
|
||||
entryRows.firstMatch
|
||||
}
|
||||
|
||||
func tapTab(identifier: String, labels: [String], timeout: TimeInterval = 5, file: StaticString = #file, line: UInt = #line) {
|
||||
/// Tap a tab by identifier, falling back to labels.
|
||||
func tapTab(identifier: String, labels: [String], timeout: TimeInterval = 5, file: StaticString = #filePath, line: UInt = #line) {
|
||||
let idMatch = tabBars.buttons[identifier]
|
||||
if idMatch.waitForExistence(timeout: 1) {
|
||||
idMatch.tapWhenReady(timeout: timeout, file: file, line: line)
|
||||
idMatch.forceTap(file: file, line: line)
|
||||
return
|
||||
}
|
||||
|
||||
for label in labels {
|
||||
let labelMatch = tabBars.buttons[label]
|
||||
if labelMatch.waitForExistence(timeout: 1) {
|
||||
labelMatch.tapWhenReady(timeout: timeout, file: file, line: line)
|
||||
labelMatch.forceTap(file: file, line: line)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
XCTFail("Unable to find tab by id \(identifier) or labels \(labels)", file: file, line: line)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func swipeUntilExists(
|
||||
_ element: XCUIElement,
|
||||
direction: SwipeDirection = .up,
|
||||
maxSwipes: Int = 6,
|
||||
timeoutPerTry: TimeInterval = 0.6
|
||||
) -> Bool {
|
||||
if element.waitForExistence(timeout: timeoutPerTry) {
|
||||
return true
|
||||
}
|
||||
|
||||
for _ in 0..<maxSwipes {
|
||||
switch direction {
|
||||
case .up:
|
||||
swipeUp()
|
||||
case .down:
|
||||
swipeDown()
|
||||
case .left:
|
||||
swipeLeft()
|
||||
case .right:
|
||||
swipeRight()
|
||||
@unknown default:
|
||||
swipeUp()
|
||||
}
|
||||
|
||||
if element.waitForExistence(timeout: timeoutPerTry) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
enum SwipeDirection {
|
||||
case up
|
||||
case down
|
||||
case left
|
||||
case right
|
||||
case up, down, left, right
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user