这就是我在代码中设置的方式:

let userBarButtonItem = UIBarButtonItem(image: userIcon,
                                        style: .Plain,
                                        target: self,
                                        action: Selector("userButtonTapped:"))
userBarButtonItem.accessibilityIdentifier = "userBarButtonItem"

然后在UITestCase内部,我需要使用以下命令找到它:
XCUIApplication().otherElements["userBarButtonItem"] //doesnt work, and the reason is:



有没有一种方法可以使用谓词找到该实例?

最佳答案

UIBarButtonItem不实现UIAccessibilityIdentification,因此设置accessibilityIdentifier不起作用。

试一试
userBarButtonItem.accessibilityLabel = "userBarButtonItem"
然后在测试用例中
XCUIApplication().buttons["userBarButtonItem"]
这应该工作。

更新:

现在,UIBarButtonItem确实符合UIAccessibilityIdentification,因此所有这些都不是必需的。

10-08 00:25