How to use accessibility container in UITests

Issue #690

Use accessibilityElements to specify containment for contentView and buttons. You can use Accessibility Inspector from Xcode to verify.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ArticleCell: UICollectionViewCell {
let authorLabel: UILabel
let dateLabel: UILabel

let viewLabel: UILabel
let deleteButton: UIButton

private func setupAccessibility() {
contentView.isAccessibilityElement = true
contentView.accessibilityLabel = "This article is written by Nobita on Dec 4th 2020"

viewLabel.isAccessibilityElement = true // Default is false
viewLabel.accessibilityTraits.insert(.button) // Treat UILabel as button to VoiceOver

accessibilityElements = [contentView, viewLabel, deleteButton]
isAccessibilityElement = false
}
}

This works OK under Voice Over and Accessibility Inspector. However in iOS 14 UITests, only the contentView is recognizable. The workaround is to use XCUICoordinate

1
2
3
4
5
6
7
8
9
let deleteButton = cell.buttons["Delete article"]
if deleteButton.waitForExistence(timeout: 1) {
deleteButton.tap()
} else {
let coordinate = cell.coordinate(
withNormalizedOffset: CGVector(dx: 0.9, dy: 0.9)
)
coordinate.tap()
}

Updated at 2020-11-04 09:42:05

Comments