UITests:如何通过带有谓词的accessibilityIdentifier找到UIBarButtonItem?

Bar*_*zyk 5 xcode ios swift xcode-ui-testing

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

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

然后在里面UITestCase我需要找到这个:

XCUIApplication().otherElements["userBarButtonItem"] //doesnt work, and the reason is:
Run Code Online (Sandbox Code Playgroud)

断言失败:UI测试失败 - 未找到"userBarButtonItem"匹配的其他内容

有没有办法如何使用谓词来找到它?

San*_*ndy 3

UIBarButtonItem没有实现UIAccessibilityIdentification,所以设置accessibilityIdentifier不起作用。

相反尝试

userBarButtonItem.accessibilityLabel = "userBarButtonItem"

然后在测试用例中

XCUIApplication().buttons["userBarButtonItem"]

这应该有效。

更新 :

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

  • 现在 UIBarButtonItem 确实符合 UIAccessibilityIdentification。https://developer.apple.com/reference/uikit/uiaccessibilityidentification (2认同)