我正在为我的 iOS Swift 应用程序编写 UITest 案例。在应用程序中,我以这种方式在导航栏上创建了一个自定义的右键项目按钮:
let barButtonItem = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItem = barButtonItem
现在我不知道如何在 XCUITest 中访问这个自定义的右键项目按钮,真的需要一些帮助。提前致谢。
最佳答案
您无法访问 UIBarButtonItem
,因为它不是真正的 UIElement(它不是 UIView
子类),但您可能想要访问 UIButton
无论如何,在您的右侧栏按钮项目中。
有几种方法可以访问该按钮,这里有两个想法:
1、查询导航栏第一个按钮
let rightNavBarButton = XCUIApplication().navigationBars.children(matching: .button).firstMatch
XCTAssert(rightNavBarButton.exists)
这样你就可以访问第一个
UIButton
里面UINavigationBar
.这仅适用于导航栏中只有一个按钮的情况。所以当你添加另一个按钮时它会中断。
2. 使用可访问性标识符
您可以为右侧栏按钮项内的按钮定义可访问性标识符,并在测试期间使用它来访问它:
在您的应用中:
let barButtonItem = UIBarButtonItem(customView: customView)
barButtonItem.accessibilityIdentifier = "navbarRightItem"
navigationItem.rightBarButtonItem = barButtonItem
在您的测试中:
let rightNavBarButton = XCUIApplication().navigationBars.buttons["navbarRightItem"]
XCTAssert(rightNavBarButton.exists)
只要确保您使用的是
accessibilityIdentifier
而不是 accessibilityLabel
.因为 accessibilityLabel
VoiceOver 将为残障用户朗读,并应包含有用的文本。
关于ios - 如何访问 XCUITest 导航栏上的右侧项目按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54117040/