Created
April 28, 2020 17:37
-
-
Save igorcferreira/b7b5204f160f43d0f0c5fdebcf284c72 to your computer and use it in GitHub Desktop.
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
/// 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