XCTest waitForExpectations(...) 等待多个结果

3

在UITest中,点击登录按钮后,我需要等待两个事件,无论是更新还是在成功登录后显示主屏幕,并决定接下来该怎么做。 文档说道:

一次只能有一个“-waitForExpectationsWithTimeout:handler:”处于活动状态,但是可以将多个离散的{ expectations -> wait }序列链接在一起。

所以需要稍微调整一下,但最终找到了解决方法。

2个回答

3
您可以向谓词添加参数,并在条件之间使用 OR。
let skipUpdateButtonPredicate = NSPredicate(format: "%@.exists == 1 OR %@.exists == 1", skipUpdateButton, homeTabBar)
self.expectation(for: skipUpdateButtonPredicate, evaluatedWith: [Any](), handler: nil)
testCase.waitForExpectations(timeout: Constants.loginTimeout,
                                 handler: nil)

如果出现主页或更新,则可以在此处分支:

if skipUpdateButton.exists {
    skipUpdateButton.tap()
}

2

使用expectations时,您可以设置需要满足的次数。

let expectations = expectation(description: "AsyncExpectations")
expectations.expectedFulfillmentCount = 4


    DispatchQueue.global(qos: .userInteractive).async {
    // do work here
    expectations.fulfill()
}


DispatchQueue.global(qos: .background).async {
    // do work here
    expectations.fulfill()
}

DispatchQueue.global(qos: .default).async {
    // do work here
    expectations.fulfill()
}

DispatchQueue.main.async {
    // do work here
    expectations.fulfill()
}

// wait for all 4 expectations
waitForExpectations(timeout: 2, handler: nil)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接