这就是我在代码中设置的方式:
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,因此所有这些都不是必需的。