如何解决如何在UIAlertController的textField中添加一个accessibilityIdentifier
使用XCUITest,我正在努力在UIAlertController上找到用于将项目添加到列表的textField。
我已经向警报本身添加了accessibilityIdentifier,但是如果可能的话,我也想在textField中添加一个,因为到目前为止,我的测试(已部分记录)只能在函数第一次运行时定位该字段。第二次打开警报将项目添加到列表时,找不到焦点在textField上。
UIAlertConroller如下所示:
let alert = UIAlertController(title: "Add A Task",message: "Add new",preferredStyle: .alert)
alert.view.accessibilityIdentifier = "Add Task Panel" // Added this identifier to help with UI tests
alert.addTextField(configurationHandler: nil)
alert.addAction(UIAlertAction(title: "Cancel",style: .default,handler: nil))
alert.addAction(UIAlertAction(title: "Add",handler: { [weak self] (action) -> Void in
测试功能看起来像这样,但是部分是使用“记录”功能生成的,因此并不理想,因为我觉得必须有一种更好的方法来识别textField(屏幕上只有一个)。
func addTextToOpenPopUp(textToAdd: String) {
let popUpTextField = app.alerts["Add Task Panel"].scrollViews.otherElements.collectionViews.cells
.children(matching: .other).element
.children(matching: .other).element
.children(matching: .other).element
.children(matching: .other).element(boundBy: 1)
.children(matching: .textField).element
popUpTextField.tap()
popUpTextField.typeText(textToAdd)
}
我花了数小时试图找到一种向警报中的组件添加标识符的方法,但是找不到任何答案。
解决方法
您错过了configurationHandler
,它专门用于修改您的文本字段:
alert.addTextField(configurationHandler: { textField in
textField.accessibilityIdentifier = "Task Text Field"
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。