Performing UIAccessibilityCustomAction from UITests
Asked Answered
G

3

4

I've got a subclass of UIView, let's say it's class DemoView: UIView { } which contains UILabel and UIButton. I needed to group it and add UIAccessibilityCustomAction so I've overriden the var accessibilityElements: [Any]? and used union to connect both elements. I've also assigned "Users" string to accessibilityLabel.

From user perspective this works as it should, VoiceOver reads Users and then user can select custom action which is named Edit.

Problem is that I don't know how can I fire this custom action from UITests. I know that XCUIElement contains array of UICustomActions and I can get its selector but then what?

Gurule answered 10/4, 2019 at 18:48 Comment(2)
Have you already tried to get the UIAccessibilityCustomActions you mentioned from your XCUIElement during UITest using a breakpoint for instance ? Isn't it nil ?Allx
@Allx Yes, in UI Tests it is nilGurule
G
5

I talked to Apple Engineers during WWDC19 Accessibility and Testing Labs and they said it is not possible right now. The reason why accessibility is not available during testing are security concerns. What they also said is that they don't use UITests for testing UIAccessibility and when creating accessibility elements support two cases - when there are UITests running and not.

I recommend making a suggestion using Feedback Assistant.

Gurule answered 11/6, 2019 at 13:24 Comment(0)
A
1

The purpose you're tring to reach isn't possible currently with iOS13 and Xcode 11.

The UITesting framework doesn't access the application code as unit tests do: you can't get an instance to perform selector + the array of custom actions is nil when in UITest ⇒ every custom action isn't reachable and then can't be fired at all.

XCUITEST works thanks to the accessibility properties like accessibilityIdentifier but isn't definitely designed to simply work for VoiceOver (inconceivable and incomprehensible in my view).

I hope that something new with UI testing will be introduced during the next WWDC for REAL accessibility testing.

Allx answered 2/5, 2019 at 13:7 Comment(0)
U
-1

For anyone else stuck on this, if you have a direct reference to the UIView in question, I've (well, a coworker of mine) found the following 'hack' to work quite well.

let view: UIView = YourViewWithCustomAction()
let customActionName: String = "<Your custom action name here>"
let action: UIAccessibilityCustomAction = view.accessibilityCustomActions!.first(where: { $0.name == customActionName })!
_ = action.target!.perform(action.selector, with: action)

As a disclaimer, this might only work when you have a custom UIView subclass that implements its own override var accessibilityElements.

Unawares answered 25/2, 2021 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.