@@ -6,6 +6,10 @@ import XCTest
66// and those are obviously specific to each screen, hence should be added by each subclass.
77open class ScreenObject {
88
9+ public enum WaitForScreenError : Equatable , Error {
10+ case timedOut
11+ }
12+
913 /// The `XCUIApplication` instance this screen is part of. This is the value passed at
1014 /// initialization time.
1115 public let app : XCUIApplication
@@ -34,17 +38,31 @@ open class ScreenObject {
3438
3539 @discardableResult
3640 func waitForScreen( ) throws -> Self {
37- XCTContext . runActivity ( named: " Confirm screen \( self ) is loaded " ) { ( activity) in
38- let result = waitFor ( element: expectedElement, predicate: " isEnabled == true " , timeout: 20 )
39- XCTAssert ( result, " Screen \( self ) is not loaded. " )
41+ try XCTContext . runActivity ( named: " Confirm screen \( self ) is loaded " ) { ( activity) in
42+ let result = waitFor (
43+ element: expectedElement,
44+ predicate: " isEnabled == true " ,
45+ timeout: self . waitTimeout
46+ )
47+
48+ guard result == . completed else { throw WaitForScreenError . timedOut }
4049 }
4150 return self
4251 }
4352
44- private func waitFor( element: XCUIElement , predicate: String , timeout: Int = 5 ) -> Bool {
45- let elementPredicate = XCTNSPredicateExpectation ( predicate: NSPredicate ( format: predicate) , object: element)
46- let result = XCTWaiter . wait ( for: [ elementPredicate] , timeout: TimeInterval ( timeout) )
47-
48- return result == . completed
53+ private func waitFor(
54+ element: XCUIElement ,
55+ predicate: String ,
56+ timeout: TimeInterval
57+ ) -> XCTWaiter . Result {
58+ XCTWaiter . wait (
59+ for: [
60+ XCTNSPredicateExpectation (
61+ predicate: NSPredicate ( format: predicate) ,
62+ object: element
63+ )
64+ ] ,
65+ timeout: timeout
66+ )
4967 }
5068}
0 commit comments