Skip to content

Instantly share code, notes, and snippets.

@atineoSE
Last active March 12, 2020 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atineoSE/5f8e9bdbf2c67811737373abb02221ad to your computer and use it in GitHub Desktop.
Save atineoSE/5f8e9bdbf2c67811737373abb02221ad to your computer and use it in GitHub Desktop.
Lightweight waiting API for XCUITests based on NSPredicate
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