【问题标题】:expectationForPredicate Fails test caseexpectForPredicate 测试用例失败
【发布时间】:2016-11-25 15:06:09
【问题描述】:

最近开始使用 SWIFT 进行 XCode UI 测试。

我的问题是我需要等到元素出现在 iPhone 屏幕上。

我找到了一个使用 '''expectationForPredicate''' 和 '''waitForExpectationsWithTimeout''' 的解决方案,但问题是,如果预期的谓词在超时内不匹配,这种方法会导致测试用例失败。

我需要一个可以等待元素出现在屏幕上的代码,如果元素没有出现并且超时时间超出那么我不希望测试用例失败。而是我想返回 true(元素存在)/false(不存在)

【问题讨论】:

    标签: swift xcode ios10 xcode-ui-testing


    【解决方案1】:

    我通过避免上述函数找到了解决方案,因为这些函数未通过我的测试,而不是返回 true 或 false

    下面是我创建的方法

    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;
    }
    

    我希望这对其他人有帮助,但如果您还有其他更好的解决方案,请在此处回答。

    【讨论】:

      猜你喜欢
      • 2011-03-29
      • 2018-12-02
      • 1970-01-01
      • 2022-10-07
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多