Skip to content

Instantly share code, notes, and snippets.

@igorcferreira
Created April 28, 2020 17:37
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 igorcferreira/b7b5204f160f43d0f0c5fdebcf284c72 to your computer and use it in GitHub Desktop.
Save igorcferreira/b7b5204f160f43d0f0c5fdebcf284c72 to your computer and use it in GitHub Desktop.
/// Method used to retry a UI Test check, and wait for a while before declaring failure.
/// This can be useful to bypass animation or screen transition failures.
/// The original snippet was extracted from [Javi](https://twitter.com/Javi)
/// on a [thread](https://twitter.com/Javi/status/1255181595515863042) regarding UI Test strategies.
///
/// - Parameters:
/// - test: Block that will be evaluated on each test loop
/// - description: Description of the operation. Used on fail message
/// - timeout: Maximum time that will be waited before failing
/// - fatal: Indicator if the failure on the test block will be fatal to the test or not
/// - file: File that called the operation, used for fail messages
/// - line: Code line that called the operation, user for fail messages
/// - Returns: Indicator if the test passed or not during the wait time
@discardableResult
func wait(until test: @autoclosure @escaping () -> Bool, description: String = "", timeout: TimeInterval = 1, fatal: Bool = true, file: StaticString = #file, line: UInt = #line) -> Bool {
let endTime = Date(timeIntervalSinceNow: timeout)
while !test() && Date() < endTime {
CFRunLoopRunInMode(.defaultMode, 0.1, false)
}
if test() {
return true
}
if fatal {
XCTFail("Timeout: \(description)", file: file, line: line)
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment