Search code examples
swiftuixctestwatchos

Add button in SwiftUI Stepper not hittable during XCTest


My stepper is defined as follows (Standalone WatchOS app)

Stepper(value: $myCount) {
    Text("\(myCount)").font(.footnote).accessibilityIdentifier("count_label")
}.accessibilityIdentifier("my_stepper")

It is fully functional on the real / simulator devices. During a test case, defined below, I am unable to invoke the increment button. (I get an error and the button itself is not hittable, ever)

XCTAssertTrue(app.steppers["my_stepper"].waitForExistence(timeout: 10))
XCTAssertFalse(app.steppers["my_stepper"].buttons["Remove"].isEnabled)
XCTAssertTrue(app.steppers["my_stepper"].buttons["Add"].isEnabled)
-> (Error)  app.steppers["my_stepper"].buttons["Add"].tap()

Error kAXErrorCannotComplete performing AXAction kAXScrollToVisibleAction on element AX element pid

I tried to forceTap (using coordinates) with no luck. Any idea how to invoke the increment action?


Solution

  • While the increment and decrement buttons exist in the view stack, they are not hittable. Likely a bug in SwiftUI that impacts either WatchOS or all platforms. Best way I have found to temporarily get past the issue is using the following tutorial :

    app.steppers["my_stepper"].coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.5)).tap()
        
    

    This is a workaround that will probably fail on different devices. For me, it would only work with Ultra 49mm watchOS 9.0. Accepting this until a better answer is found.