Ios 如何正确处理swift中未找到的XCUI元素错误
我正在尝试用swift为我的iosui测试编写一个do-try-catch,它使用XCUI测试。我正在阅读错误处理部分: 但是我不确定在找不到元素时应该抛出哪个错误Ios 如何正确处理swift中未找到的XCUI元素错误,ios,swift,xcode-ui-testing,Ios,Swift,Xcode Ui Testing,我正在尝试用swift为我的iosui测试编写一个do-try-catch,它使用XCUI测试。我正在阅读错误处理部分: 但是我不确定在找不到元素时应该抛出哪个错误 func tapElement(string:String) throws { do{ waitFor(app.staticTexts[string], 5) try app.staticTexts[string].tap() } catch { NSLog(
func tapElement(string:String) throws {
do{
waitFor(app.staticTexts[string], 5)
try app.staticTexts[string].tap()
}
catch {
NSLog("Element was not found: \(string)")
//how can i check specifically that the element was not found?
}
}
非常感谢您的帮助 您不需要在UI测试中尝试捕获查找元素。在尝试轻触()元素之前,询问该元素是否存在()
let app = XCUIApplication()
let element = app.staticTexts["item"]
if element.exists {
element.tap()
} else {
NSLog("Element does not exist")
}
查看my以获得更具体的示例。您应该使用
元素.exists
和元素.isHittable
element.exists检查该元素是否为UIApplication/ScrollView/的元素
元素。isHittable确定是否可以为元素计算生命点
如果不同时检查这两个元素,则thanelement.tap()
会抛出以下错误,例如,如果元素位于键盘下方:
失败:无法滚动到可见(通过AX操作)文本字段
示例代码:
let textField = elementsQuery.textFields.allElementsBoundByIndex[i]
if textField.exists && textField.isHittable {
textField.tap()
} else {
// text field is not hittable or doesn't exist!
XCTFail()
}
有一些时候,我想优雅地处理当元素没有找到我已经读过你的博客,感谢它是一个伟大的来源。但我不是在寻找关于点击按钮的建议。任何关于使用try-catch优雅地处理错误的见解都会让人大跌眼镜为什么要尝试捕捉?查看我的编辑以检查元素是否存在。我认为这可能是一个更好的方法,但您在那里放置的内容实际上就是我最后所做的。谢谢
let textField = elementsQuery.textFields.allElementsBoundByIndex[i]
if textField.exists && textField.isHittable {
textField.tap()
} else {
// text field is not hittable or doesn't exist!
XCTFail()
}