iOS test automation for complex view, UIAccessabilityContainer?

65 Views Asked by At

I am building UI automation tests for an app with a complex view hierarchy.

In my situation, I would like to use automation to tap on a UITextField subclass. That textfield is contained within a collectionView, which is itself contained within another collection view.

That textview has a known accessibility label+identifier, but I can't find any way to access this view via the normal XCTest api. For example, the following does not locate the element:

let app = XCUIApplication()
let textField = app.textFields["FOO"]
let textField2 = app.secureTextFields["FOO"]
let textField3 = app.otherElements["FOO"]

As another way to get a reference to the textfield, I tried setting up as a UIAccessabilityContainer, but I'm not sure I'm using it correctly.

//inside the textfield

//should return false if using UIAccessibilityContainer?
self.isAccessibilityElement = false
let e = UIAccessibilityElement(accessibilityContainer: self)
e.accessibilityIdentifier = placeholder
e.accessibilityLabel = placeholder
e.accessibilityTraits = .allowsDirectInteraction
            
//append this element to the window's list of elements
var eles = window?.accessibilityElements ?? []
eles.append(e)
window?.accessibilityElements = eles

Doing this seems to break all of my existing automation code using the "normal" api. What is the correct way to go about this?

0

There are 0 best solutions below