Ale*_*lex 13 swift swift2 xcode-ui-testing
我希望测试暂停并等待元素出现在屏幕上然后再继续.
我没有看到为此创建期望并等待使用的好方法
public func waitForExpectationsWithTimeout(timeout: NSTimeInterval, handler: XCWaitCompletionHandler?)
Run Code Online (Sandbox Code Playgroud)
创造我一直在使用的期望的方法一直是
public func expectationForPredicate(predicate: NSPredicate, evaluatedWithObject object: AnyObject, handler: XCPredicateExpectationHandler?) -> XCTestExpectation
Run Code Online (Sandbox Code Playgroud)
但这需要一个已经存在的元素,而我想让测试等待一个尚不存在的元素.
有谁知道最好的方法吗?
Tom*_*Bąk 20
在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)
Run Code Online (Sandbox Code Playgroud)
查看UI测试备忘单和从标题生成的文档(目前没有官方文档),全部由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)
}
Run Code Online (Sandbox Code Playgroud)
在 Xcode 9、iOS 11 中,您可以使用新的 APIwaitForExistence
这个问题是关于 Swift2 的,但它仍然是 2019 年的热门搜索结果,所以我想给出一个最新的答案。
使用 Xcode 9.0+,事情变得更简单了,这要归功于waitForExistence:
let app = XCUIApplication()
let myButton = app.buttons["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()
Run Code Online (Sandbox Code Playgroud)
WebView 示例:
let app = XCUIApplication()
let webViewsQuery = app.webViews
let myButton = webViewsQuery.staticTexts["My Button"]
XCTAssertTrue(myButton.waitForExistence(timeout: 10))
sleep(1)
myButton.tap()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7113 次 |
| 最近记录: |