Search code examples
swiftxctestxcode-ui-testingxctestexpectation

Wait until object is not visible on the screen using Swift and XCTest


I'm looking for help writing a method which waits until the specified element is not present on a page anymore. I am developing with Swift 2.2 and XCTest. As you can see, I'm a new here and new to programming. Your help will be greatly appreciated.


Solution

  • You will have to setup predicate for the condition you want to test:

    let doesNotExistPredicate = NSPredicate(format: "exists == FALSE")
    

    Then create an expectation for your predicate and UI element in your test case:

    self.expectationForPredicate(doesNotExistPredicate, evaluatedWithObject: element, handler: nil)
    

    Then wait for your expectation (specifying a timeout after which the test will fail if the expectation is not met, here I use 5 seconds):

    self.waitForExpectationsWithTimeout(5.0, handler: nil)