Vikas Thange
Vikas Thange

Reputation: 250

expectationForPredicate Fails test case

Recently started working on XCode UI test with SWIFT.

My problem is I need to wait until a element appears on iPhone screen.

I found a solution with '''expectationForPredicate''' and '''waitForExpectationsWithTimeout''' but the problem this is this methods are designed to fail test case if expected predicate not matched within timeout.

I need a code which can wait for element to appear on screen if the element did not appear and timeout exceeded then I don't want test case to fail. rather I would like to return true (element exists) / false (not exists)

Upvotes: 1

Views: 354

Answers (1)

Vikas Thange
Vikas Thange

Reputation: 250

I found a solution by avoiding the above mentioned functions as those are failing my tests instead of returning true or false

Below is the method i created

func waitForElementToAppear(element: XCUIElement, file: String = #file, line: UInt = #line) -> Bool {
    let TIMEOUT: Double = 120 ;
    var isFound = false;
    let start = NSDate();
    var diff : Double = 0;
    repeat{
        print("Is element \(element) found : \(element.exists)")
        print("Printing debugDescription -> ")
        print(XCUIApplication().debugDescription)
        if(element.exists){
            isFound = true;
            break;
        }
        print("Waiting for element to exists... Time counter :\(diff)")
        sleep(1)
        let end = NSDate();
        diff =  end.timeIntervalSinceDate(start);
    }while(diff <= TIMEOUT);
    return isFound;
}

I hope this will help others, But if you still have any other better solution please answer here.

Upvotes: 0

Related Questions