XCTestCase

public extension XCTestCase

XCTestCase extension adds additional methods which allow waiting for existing or visible elements. Useful if some background activity is being performed in the background that would change appearance of the application when finished. If this doesn’t happen then test will fail.

Example:

Wait, by default 10 seconds, until the button will be visible (exists and hittable properties will be true).

let button = view.buttons["appearingButton"]
wait(forVisibilityOf: button)

In addition, those extensions support also the IdentifiableByElement protocol which allows to use for example page objects.

Example:

lazy var mainPage: MainPage = MainPage(in: self.app)
wait(forVisibilityOf: mainPage)
  • Wait for an identifiable element to exist in a view hierarchy. After given interval, if element is not found, test fails.

    Example:

    lazy var mainPage: MainPage = MainPage(in: self.app)
    wait(forExistanceOf: mainPage)
    

    Declaration

    Swift

    func wait<T>(forExistanceOf element: T, timeout: TimeInterval = XCTestCase.defaultTimeOut, file: StaticString = #file, line: UInt = #line) where T : IdentifiableByElement

    Parameters

    element

    XCUIElement to wait for.

    timeout

    Waiting time (default: 10 seconds).

    file

    Current source file.

    line

    Current source line.

  • Wait for an identifiable element to appear in a view hierarchy. After given interval seconds, if element is not found, test fails.

    Example:

    lazy var mainPage: MainPage = MainPage(in: self.app)
    wait(forVisibilityOf: mainPage)
    

    Declaration

    Swift

    func wait<T>(forVisibilityOf element: T, timeout: TimeInterval = XCTestCase.defaultTimeOut, file: StaticString = #file, line: UInt = #line) where T : IdentifiableByElement

    Parameters

    element

    XCUIElement to wait for.

    timeout

    Waiting time (default: 10 seconds).

    file

    Current source file.

    line

    Current source line.

  • Default timeout used in wait methods. By default set to 10 seconds.

    Declaration

    Swift

    class var defaultTimeOut: TimeInterval { get }
  • Wait for an UI element to fulfill the predicate. After given time, if the predicate is not fulfilled for a given element, test fails.

    Example:

    let button = view.buttons["appearingButton"]
    let existancePredicate = NSPredicate(format: "exists == true")
    wait(forFulfillmentOf: existancePredicate, for: button)
    

    Declaration

    Swift

    func wait(forFulfillmentOf predicate: NSPredicate,
              for element: XCUIElement,
              withFailingMessage message: String = "Failed to fulfill predicate %@ for %@ within %.2f seconds.",
              timeout: TimeInterval = XCTestCase.defaultTimeOut,
              file: StaticString = #file,
              line: UInt = #line)

    Parameters

    predicate

    NSPredicate to fulfill.

    element

    XCUIElement to wait for.

    message

    String as format for failing message. You must use %@ for predicate value, %@ for element value and %f for timeout value.

    timeout

    Waiting time in seconds (default: 10 seconds).

    file

    Current source file.

    line

    Current source line.

  • Wait for an UI element to exist in a view hierarchy. After given time, if element is not found, test fails.

    Example:

    let button = view.buttons["appearingButton"]
    wait(forExistanceOf: button)
    

    Declaration

    Swift

    func wait(forExistanceOf element: XCUIElement, timeout: TimeInterval = XCTestCase.defaultTimeOut, file: StaticString = #file, line: UInt = #line)

    Parameters

    element

    XCUIElement to wait for.

    timeout

    Waiting time in seconds (default: 10 seconds).

    file

    Current source file.

    line

    Current source line.

  • Wait for an UI element to be visible in a view hierarchy. After given time, if the element is still not visible, test fails.

    Example:

    let button = view.buttons["appearingButton"]
    wait(forVisibilityOf: button)
    

    Declaration

    Swift

    func wait(forVisibilityOf element: XCUIElement, timeout: TimeInterval = XCTestCase.defaultTimeOut, file: StaticString = #file, line: UInt = #line)

    Parameters

    element

    XCUIElement to wait for.

    timeout

    Waiting time in seconds (default: 10 seconds).

    file

    Current source file.

    line

    Current source line.

  • Wait for an UI element to be not visible in a view hierarchy. After given time, if the element is still visible, test fails.

    Example:

    let button = view.buttons["appearingButton"]
    wait(forInvisibilityOf: button)
    

    Declaration

    Swift

    func wait(forInvisibilityOf element: XCUIElement, timeout: TimeInterval = XCTestCase.defaultTimeOut, file: StaticString = #file, line: UInt = #line)

    Parameters

    element

    XCUIElement to wait for.

    timeout

    Waiting time in seconds (default: 10 seconds).

    file

    Current source file.

    line

    Current source line.