Assertions & waitForExistence

XCTAssert functions and waiting for dynamic UI.

XCTAssert functions

swift
// Element exists
XCTAssertTrue(app.buttons["Submit"].exists)
XCTAssertFalse(app.alerts["Error"].exists)

// Element is enabled
XCTAssertTrue(app.buttons["Submit"].isEnabled)

// Text content
XCTAssertEqual(app.staticTexts["title"].label, "Welcome, Alice!")

// Hittable (visible and tappable)
XCTAssertTrue(app.buttons["Login"].isHittable)

waitForExistence (essential for async UI)

swift
// Wait up to 5 seconds for element to appear
let successBanner = app.staticTexts["Login successful"]
XCTAssertTrue(successBanner.waitForExistence(timeout: 5))

// Wait for element to disappear
let spinner = app.activityIndicators["Loading"]
// Spin wait manually for disappearance
let expectation = XCTNSPredicateExpectation(
    predicate: NSPredicate(format: "exists == false"),
    object: spinner
)
wait(for: [expectation], timeout: 10)