Last active
March 12, 2020 14:39
-
-
Save atineoSE/5f8e9bdbf2c67811737373abb02221ad to your computer and use it in GitHub Desktop.
Lightweight waiting API for XCUITests based on NSPredicate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let standardTimeout:Double = 10 | |
let shortTimeout: Double = 5 | |
//------ Definition | |
func wait(for object: Any, satisfying predicateFormat: String, errorMessage: String, fails: Bool = true, timeout:Double = standardTimeout, completion: (()->())? = nil) { | |
let predicate = NSPredicate(format: predicateFormat) | |
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: object) | |
let result = XCTWaiter().wait(for: [expectation], timeout: timeout) | |
if (result != .completed) { | |
if (fails) { | |
XCTFail(errorMessage) | |
} | |
} else { | |
completion?() | |
} | |
} | |
//------ Use | |
// 1. Wait for existence | |
let loginButton = XCUIApplication().buttons["LoginButtonIdentifier"] | |
wait(for: loginButton, satisfying: "exists == YES", errorMessage: "Could not find login/logout button", | |
fails: false, timeout: standardTimeout) { | |
// perform action | |
} | |
// 2. Wait for change of value | |
let navBarLeftButton = XCUIApplication().navigationBars.buttons["LogoutButtonIdentifier"] | |
wait(for: navBarLeftButton, satisfying: "label == \"logout\"", errorMessage: "Could not login") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment