我希望有一个测试暂停,并等待一个元素出现在屏幕上,然后再继续。
我看不出有什么好的方法来为这个创建一个期望并等待使用
public func waitForExpectationsWithTimeout(timeout: NSTimeInterval, handler: XCWaitCompletionHandler?)创建我一直使用的期望的方法是
public func expectationForPredicate(predicate: NSPredicate, evaluatedWithObject object: AnyObject, handler: XCPredicateExpectationHandler?) -> XCTestExpectation但是这需要一个已经存在的元素,而我希望让测试等待一个尚未存在的元素。
有人知道最好的方法吗?
发布于 2015-12-02 17:00:51
在expectationForPredicate(predicate: evaluatedWithObject: handler:)中,您不会给出一个实际的对象,而是在视图层次结构中提供一个查找它的查询。因此,例如,这是一个有效的测试:
let predicate = NSPredicate(format: "exists == 1")
let query = XCUIApplication().buttons["Button"]
expectationForPredicate(predicate, evaluatedWithObject: query, handler: nil)
waitForExpectationsWithTimeout(3, handler: nil)查看从头生成的UI测试备忘单和文档 (目前还没有正式文档),都是由Joe编写的。
发布于 2019-07-05 22:23:44
这个问题是关于Swift2的,但在2019年它仍然是一个顶级的搜索结果,所以我想给出一个最新的答案。
有了Xcode 9.0+,事情就简单了一些,这要感谢waitForExistence
let app = XCUIApplication()
let myButton = app.buttons["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()WebViews示例:
let app = XCUIApplication()
let webViewsQuery = app.webViews
let myButton = webViewsQuery.staticTexts["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()发布于 2017-05-31 08:12:36
你可以用这个,在Swift 3里
func wait(element: XCUIElement, duration: TimeInterval) {
let predicate = NSPredicate(format: "exists == true")
let _ = expectation(for: predicate, evaluatedWith: element, handler: nil)
// We use a buffer here to avoid flakiness with Timer on CI
waitForExpectations(timeout: duration + 0.5)
}在Xcode 9,iOS 11中,可以使用新的API waitForExistence。
https://stackoverflow.com/questions/34047887
复制相似问题