Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
/// 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](
/// on a [thread]( 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
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