[英]Using a conditional in XCUITest doesn't seem to work
我設置了啟動參數,以清除用戶默認設置並在測試之間注銷,但這似乎有一半時間不起作用。 我一直在追逐一個可能的錯誤,可能是根本原因,但在同時,我想有更少的片狀測試,以便開發人員對他們有更多的信心。 因此,我在登錄步驟周圍添加了一個條件,該條件僅在登錄按鈕存在時才應執行。 在運行測試時,其類似if語句將被完全忽略,測試將查找登錄按鈕,然后在找不到登錄按鈕時失敗。
碼:
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
}
我什么不來? 我已經嘗試使用.isHittable
和,不能正常工作。 我已經把斷點測試和印刷的結果app.buttons["name"].exists
和而返回false .isHittable
返回一些錯誤。 因此,似乎.exists
在這里應符合我的期望。
在許多情況下,XCUITest Framework在視圖可用於交互之前沒有等待足夠長的時間。 為了解決這個問題,您應該編寫一些等待邏輯,最好是為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
}
}
然后,您可以在測試中輸入以下內容:
func testTapControlMode() {
let haveAnAccountButton = app.buttons["Have and Account?"]
if waitFor(haveAnAccountButton, to: .appear) {
login()
}
// ... proceed with test
}
並在您的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.