XCUIElement exists, but is not hittable
Asked Answered
S

1

13

In my UI tests, I create a UIView programmatically using (shortened)

let topMarker = UIView.init(frame: CGRect.init())
…
topMarker.accessibilityIdentifier = kTopMarker  

The topMarker view is in a custom table view cell. In my UI tests, I use

let new1stCell = app.cells.element(boundBy: 0)
let topMarker = new1stCell.otherElements[kTopMarker]
let topMarkerExists = topMarker.waitForExistence(timeout: 15)
XCTAssertTrue(topMarkerExists, "Top marker does not exist")
XCTAssertTrue(topMarker.isHittable, "Top marker is not hittable")

When I set a Test Failure breakpoint, the test stops at the last line, i.e. topMarker exists, but is not hittable.
On the other hand, I can see the view in the snapshot, i.e. it exists and is visible.
This is stange, because the docs say:

isHittable returns true if the element exists and can be clicked, tapped, or pressed at its current location. It returns false if the element does not exist, is offscreen, or is covered by another element.

I thought, maybe it is visible, but cannot be clicked, tapped, or pressed, because userInteractionEnable is not true, but even if I set this property to true, the view does not become hittable.

What am I missing?

Sextain answered 17/2, 2018 at 20:14 Comment(0)
S
13

Problem solved:
An XCUIElement is only hittable, if its isAccessibilityElement property is set to true.
The docs to property isAccessibilityElement say

The default value for this property is false unless the receiver is a standard UIKit control, in which case the value is true.
Assistive applications can get information only about objects that are represented by accessibility elements. Therefore, if you implement a custom control or view that should be accessible to users with disabilities, set this property to true.

My UIView that I instantiated programmatically is not a standard UIKit control. As soon as I added

topMarker.isAccessibilityElement = true  

the test

XCTAssertTrue(topMarker.isHittable, "Top marker is not hittable")  

succeeded.

Sextain answered 18/2, 2018 at 7:12 Comment(2)
Anyone know how to do this in SwiftUI?Melinamelinda
Apples framework is messed up. This is a broken API. Turning on isAccessibilityElement will tell the accessibility engine that this item has a label and value that can be read. That is not the same thing as being hittable. This is really bad implementation by Apple.Ibby

© 2022 - 2024 — McMap. All rights reserved.