[英]Using a conditional in XCUITest doesn't seem to work
I have launch arguments set up to clear user defaults and log out between tests however, half the time this doesn't seem to work. 我设置了启动参数,以清除用户默认设置并在测试之间注销,但这似乎有一半时间不起作用。 I have been chasing down a possible bug that might be the root cause but in the mean time I would like to have less flaky tests so developers have more confidence in them.
我一直在追逐一个可能的错误,可能是根本原因,但在同时,我想有更少的片状测试,以便开发人员对他们有更多的信心。 So I added a conditional around the login step that should only execute if the login button exists.
因此,我在登录步骤周围添加了一个条件,该条件仅在登录按钮存在时才应执行。 When running the tests, its like the if statement is ignored completely an the tests look for the login button and then fail when its not found.
在运行测试时,其类似if语句将被完全忽略,测试将查找登录按钮,然后在找不到登录按钮时失败。
Code: 码:
func login() {
app.buttons["Have an account? Log in"].tap()
let emailAddressTextField = app.textFields["Email Address"]
let passwordSecureTextField = app.secureTextFields["Password"]
emailAddressTextField.tap()
emailAddressTextField.typeText(EMAIL_ALPHA_USER)
passwordSecureTextField.tap()
passwordSecureTextField.typeText(PASSWORD)
if app.staticTexts["Success!"].waitForExistence(timeout: 5) {
app.buttons["OK"].tap()
}
}
func testTapControlMode() {
if app.buttons["Have and Account?"].exists {
login()
}
// ... proceed with test
}
What am I not getting here? 我什么不来? I've tried using
.isHittable
and that doesn't work either. 我已经尝试使用
.isHittable
和,不能正常工作。 I've put breakpoints in the tests and printed the result of app.buttons["name"].exists
and it returns false whereas .isHittable
returns some error. 我已经把断点测试和印刷的结果
app.buttons["name"].exists
和而返回false .isHittable
返回一些错误。 So it seems like .exists
here should do what I expect. 因此,似乎
.exists
在这里应符合我的期望。
In many cases XCUITest Framework doesn't wait long enough before view becomes available for interaction. 在许多情况下,XCUITest Framework在视图可用于交互之前没有等待足够长的时间。 To fix that, you should write some wait logic, best with writing extension for the
XCTestCase
class, like this: 为了解决这个问题,您应该编写一些等待逻辑,最好是为
XCTestCase
类编写扩展,如下所示:
extension XCTestCase {
enum Condition: String {
case appear = "exists == true"
case disappear = "exists == false"
}
func waitFor(_ element: XCUIElement, to condition: Condition) -> Bool {
let predicate = NSPredicate(format: condition.rawValue)
let expectationConstant = expectation(for: predicate, evaluatedWith: element, handler: nil)
let result = XCTWaiter().wait(for: [expectationConstant], timeout: 5)
return result == .completed
}
}
Then, you could have something like this in your test: 然后,您可以在测试中输入以下内容:
func testTapControlMode() {
let haveAnAccountButton = app.buttons["Have and Account?"]
if waitFor(haveAnAccountButton, to: .appear) {
login()
}
// ... proceed with test
}
And in your login
method: 并在您的
login
方法中:
func login() {
app.buttons["Have an account? Log in"].tap()
let emailAddressTextField = app.textFields["Email Address"]
let passwordSecureTextField = app.secureTextFields["Password"]
let successLabel = app.staticTexts["Success!"]
emailAddressTextField.tap()
emailAddressTextField.typeText(EMAIL_ALPHA_USER)
passwordSecureTextField.tap()
passwordSecureTextField.typeText(PASSWORD)
if waitFor(successLabel, to: .appear) {
app.buttons["OK"].tap()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.