【发布时间】:2018-08-22 21:13:11
【问题描述】:
我正在 XCUITest 中编写测试。在我的测试进行时,有一个介绍性横幅可能会或可能不会出现在移动屏幕上。我想以这样的方式编写我的案例,如果横幅存在,我必须验证该横幅上的某些元素并单击其上的“继续”按钮,如果不存在,我必须继续我的测试用例。
我知道在Java中,我们在try-catch块中编写这样的代码,这样即使没有找到banner并且代码失败,指针也会进入catch块并且剩余的程序继续。
当我在互联网上搜索时,我只发现单个语句的这种功能,而不是一些代码块。我们在 Swift 中是否有类似的东西,我可以在 try-catch 中编写一些类似代码块的代码? 请查看我的测试用例和要在横幅中单击的代码。
class MyTestClass: BaseTest {
func testMapSearch() {
do {
let app = try XCUIApplication()
let element = try app.children(matching: .window).element(boundBy: 0).children(matching: .other).element.children(matching: .other).element(boundBy: 1).children(matching: .other).element(boundBy: 0)
element.buttons["Continue"].tap()
} catch let error {
print("Error: \(error)")
}
let logInButton = app.scrollViews.otherElements.buttons["Log In"]
checkLoginPage(login: logInButton)
logInButton.tap()
}
}
do 块用于单击不确定在屏幕上显示的横幅上的“继续”按钮。 do 块后面是更多代码。如果不显示横幅,则do 块中编写的代码将失败,并且测试用例会抛出错误。我想写一个案例,无论是否显示横幅,都应该执行登录按钮代码。
【问题讨论】:
-
请阅读 Swift 书中Error Handling 章节。
-
以上代码失败在哪里?有什么例外?请注意,在 Swift 中,错误和异常之间存在巨大差异。它与 Java 大不相同。
-
你没有回答我的问题。哪条线路失败?实际的确切错误是什么?您的
catch中的print是否被调用? -
@rmaddy,测试在: element.buttons["Continue"].tap() 行失败。并且 catch 块中的 print 语句没有被执行。错误是: t = 6.66s 找到“继续”按钮 t = 7.70s 找到“继续”按钮(重试 1) t = 8.73s 找到“继续”按钮(重试 2) t = 8.80s 断言失败:MyTestClass .swift:18:没有找到匹配项:从输入 {(
标签: swift xctest xcuitest xctestcase