Swift2 UI 测试 - 等待元素出现
Swift2 UI Test - Wait for Element to Appear
我想暂停测试并等待元素出现在屏幕上再继续。
我没有看到一个好的方法来创建一个期望并等待使用
public func waitForExpectationsWithTimeout(timeout: NSTimeInterval, handler: XCWaitCompletionHandler?)
我一直在使用的创建期望的方法是
public func expectationForPredicate(predicate: NSPredicate, evaluatedWithObject object: AnyObject, handler: XCPredicateExpectationHandler?) -> XCTestExpectation
但这需要一个已经存在的元素,而我想让测试等待一个尚不存在的元素。
有谁知道最好的方法吗?
它不采用已经存在的元素。您只需要定义以下谓词:
let exists = NSPredicate(format: "exists = 1")
那么就在你的期望中使用这个谓词吧。那当然就等你的期待了。
在 expectationForPredicate(predicate: evaluatedWithObject: handler:)
中,您没有提供实际的 object,而是提供在视图层次结构中查找它的查询。因此,例如,这是一个有效的测试:
let predicate = NSPredicate(format: "exists == 1")
let query = XCUIApplication().buttons["Button"]
expectationForPredicate(predicate, evaluatedWithObject: query, handler: nil)
waitForExpectationsWithTimeout(3, handler: nil)
查看由 headers 生成的 UI Testing Cheat Sheet and documentation(目前没有官方文档),全部由 Joe Masilotti 提供。
你可以在 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)
}
在Xcode9、iOS11中,可以使用新的APIwaitForExistence
对于 Xcode 8.3 及更高版本,您可以等待新的 class - XCTWaiter
的预期,示例测试如下所示:
func testExample() {
let element = // ...
let predicate = NSPredicate(format: "exists == true")
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: element)
let result = XCTWaiter().wait(for: [expectation], timeout: 1)
XCTAssertEqual(.completed, result)
}
Read the documentation 了解更多信息。
基于 我想到了扩展 (Swift 3.2):
extension XCTestCase {
func wait(for element: XCUIElement, timeout: TimeInterval) {
let p = NSPredicate(format: "exists == true")
let e = expectation(for: p, evaluatedWith: element, handler: nil)
wait(for: [e], timeout: timeout)
}
}
extension XCUIApplication {
func getElement(withIdentifier identifier: String) -> XCUIElement {
return otherElements[identifier]
}
}
所以在您的呼叫站点上您可以使用:
wait(for: app.getElement(withIdentifier: "ViewController"), timeout: 10)
这个问题是关于 Swift2 的,但它仍然是 2019 年的热门搜索结果,所以我想给出一个 up-to-date 的答案。
使用 Xcode 9.0+ 多亏了 waitForExistence
:
let app = XCUIApplication()
let myButton = app.buttons["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()
WebView 示例:
let app = XCUIApplication()
let webViewsQuery = app.webViews
let myButton = webViewsQuery.staticTexts["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()
我想暂停测试并等待元素出现在屏幕上再继续。
我没有看到一个好的方法来创建一个期望并等待使用
public func waitForExpectationsWithTimeout(timeout: NSTimeInterval, handler: XCWaitCompletionHandler?)
我一直在使用的创建期望的方法是
public func expectationForPredicate(predicate: NSPredicate, evaluatedWithObject object: AnyObject, handler: XCPredicateExpectationHandler?) -> XCTestExpectation
但这需要一个已经存在的元素,而我想让测试等待一个尚不存在的元素。
有谁知道最好的方法吗?
它不采用已经存在的元素。您只需要定义以下谓词:
let exists = NSPredicate(format: "exists = 1")
那么就在你的期望中使用这个谓词吧。那当然就等你的期待了。
在 expectationForPredicate(predicate: evaluatedWithObject: handler:)
中,您没有提供实际的 object,而是提供在视图层次结构中查找它的查询。因此,例如,这是一个有效的测试:
let predicate = NSPredicate(format: "exists == 1")
let query = XCUIApplication().buttons["Button"]
expectationForPredicate(predicate, evaluatedWithObject: query, handler: nil)
waitForExpectationsWithTimeout(3, handler: nil)
查看由 headers 生成的 UI Testing Cheat Sheet and documentation(目前没有官方文档),全部由 Joe Masilotti 提供。
你可以在 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)
}
在Xcode9、iOS11中,可以使用新的APIwaitForExistence
对于 Xcode 8.3 及更高版本,您可以等待新的 class - XCTWaiter
的预期,示例测试如下所示:
func testExample() {
let element = // ...
let predicate = NSPredicate(format: "exists == true")
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: element)
let result = XCTWaiter().wait(for: [expectation], timeout: 1)
XCTAssertEqual(.completed, result)
}
Read the documentation 了解更多信息。
基于
extension XCTestCase {
func wait(for element: XCUIElement, timeout: TimeInterval) {
let p = NSPredicate(format: "exists == true")
let e = expectation(for: p, evaluatedWith: element, handler: nil)
wait(for: [e], timeout: timeout)
}
}
extension XCUIApplication {
func getElement(withIdentifier identifier: String) -> XCUIElement {
return otherElements[identifier]
}
}
所以在您的呼叫站点上您可以使用:
wait(for: app.getElement(withIdentifier: "ViewController"), timeout: 10)
这个问题是关于 Swift2 的,但它仍然是 2019 年的热门搜索结果,所以我想给出一个 up-to-date 的答案。
使用 Xcode 9.0+ 多亏了 waitForExistence
:
let app = XCUIApplication()
let myButton = app.buttons["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()
WebView 示例:
let app = XCUIApplication()
let webViewsQuery = app.webViews
let myButton = webViewsQuery.staticTexts["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()