如何正确错误处理 swift 中未找到的 XCUI 元素
how to properly error handle XCUI element not found in swift
我正尝试在 swift 中为我的 iOS UI 测试编写一个 do-try-catch,它使用 XCUI 测试。我正在阅读错误处理部分:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508
但是我不确定当找不到元素时应该抛出哪个错误。
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?
}
}
.....
func waitFor(element:XCUIElement, seconds waitSeconds:Double) {
NSLog("Waiting for element: \(element)")
let exists = NSPredicate(format: "exists == 1")
expectationForPredicate(exists, evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(waitSeconds, handler: nil)
}
非常感谢任何帮助!!
您不需要在 UI 测试中尝试捕获查找元素。在尝试 tap()
之前询问框架元素是否 exists()
。
let app = XCUIApplication()
let element = app.staticTexts["item"]
if element.exists {
element.tap()
} else {
NSLog("Element does not exist")
}
看看我的 blog post on getting started with UI Testing for more specific examples, like tapping an button。
您应该使用 element.exists
和 element.isHittable
element.exists 检查元素是否是 UIApplication/ScrollView/..
的元素
element.isHittable 确定是否可以为元素计算生命值。
如果您不检查两者,那么 element.tap()
会抛出以下错误,例如,如果元素位于 keyboard 下:
Failed: Failed to scroll to visible (by AX action) TextField,...
示例代码:
let textField = elementsQuery.textFields.allElementsBoundByIndex[i]
if textField.exists && textField.isHittable {
textField.tap()
} else {
// text field is not hittable or doesn't exist!
XCTFail()
}
我正尝试在 swift 中为我的 iOS UI 测试编写一个 do-try-catch,它使用 XCUI 测试。我正在阅读错误处理部分:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508 但是我不确定当找不到元素时应该抛出哪个错误。
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?
}
}
.....
func waitFor(element:XCUIElement, seconds waitSeconds:Double) {
NSLog("Waiting for element: \(element)")
let exists = NSPredicate(format: "exists == 1")
expectationForPredicate(exists, evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(waitSeconds, handler: nil)
}
非常感谢任何帮助!!
您不需要在 UI 测试中尝试捕获查找元素。在尝试 tap()
之前询问框架元素是否 exists()
。
let app = XCUIApplication()
let element = app.staticTexts["item"]
if element.exists {
element.tap()
} else {
NSLog("Element does not exist")
}
看看我的 blog post on getting started with UI Testing for more specific examples, like tapping an button。
您应该使用 element.exists
和 element.isHittable
element.exists 检查元素是否是 UIApplication/ScrollView/..
的元素element.isHittable 确定是否可以为元素计算生命值。
如果您不检查两者,那么 element.tap()
会抛出以下错误,例如,如果元素位于 keyboard 下:
Failed: Failed to scroll to visible (by AX action) TextField,...
示例代码:
let textField = elementsQuery.textFields.allElementsBoundByIndex[i]
if textField.exists && textField.isHittable {
textField.tap()
} else {
// text field is not hittable or doesn't exist!
XCTFail()
}