8

我正在寻找帮助编写一种方法,该方法一直等到页面上不再存在指定的元素。我正在使用 Swift 2.2 和 XCTest 进行开发。如您所见,我是这里的新手,也是编程新手。对你的帮助表示感谢。

4

3 回答 3

14

您必须为要测试的条件设置谓词:

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

然后在您的测试用例中为您的谓词和 UI 元素创建一个期望:

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

然后等待您的期望(指定超时,如果不满足期望,测试将失败,这里我使用 5 秒):

self.waitForExpectationsWithTimeout(5.0, handler: nil)
于 2016-05-25T20:35:46.070 回答
5

我为此编写了一个超级简单的waitForNonExistence(timeout:)扩展函数,XCUIElement它反映了现有的XCUIElement.waitForExistence(timeout:)函数,如下所示:

extension XCUIElement {

    /**
     * Waits the specified amount of time for the element’s `exists` property to become `false`.
     *
     * - Parameter timeout: The amount of time to wait.
     * - Returns: `false` if the timeout expires without the element coming out of existence.
     */
    func waitForNonExistence(timeout: TimeInterval) -> Bool {
    
        let timeStart = Date().timeIntervalSince1970
    
        while (Date().timeIntervalSince1970 <= (timeStart + timeout)) {
            if !exists { return true }
        }
    
        return false
    }
}
于 2020-07-25T14:53:52.650 回答
0

您可以每秒检查元素XCUIElement.exists10 秒,然后断言该元素。有关 ActivityIndi​​cator,请参见以下内容:

public func waitActivityIndicator() {
    var numberTry = 0
    var activityIndicatorNotVisible = false
    while numberTry < 10 {
        if activityIdentifier.exists {
            sleep(1)
            numberTry += 1
        } else {
            activityIndicatorNotVisible = true
            break
        }
    }
    
    XCTAssert(activityIndicatorNotVisible, "Activity indicator is still visible")
}
于 2020-11-18T15:19:43.837 回答