Swift2 UI测试-等待元素出现 [英] Swift2 UI Test - Wait for Element to Appear
问题描述
我想暂停测试并等待元素出现在屏幕上,然后再继续.
I want to have a test pause and wait for an element to appear on screen before proceeding.
我看不出有什么好的方法可以为此创建期望并等待使用
I don't see a good way to create an expectation for this and wait using
public func waitForExpectationsWithTimeout(timeout: NSTimeInterval, handler: XCWaitCompletionHandler?)
我一直在使用的创建期望的方法
The method to create an expectation I have been using has been
public func expectationForPredicate(predicate: NSPredicate, evaluatedWithObject object: AnyObject, handler: XCPredicateExpectationHandler?) -> XCTestExpectation
但这需要一个已经存在的元素,而我想让测试等待不存在的元素.
But this takes an already existing element, whereas I would like to have the test wait for an element that does not yet exist.
有人知道最好的方法吗?
Does anyone know the best way to do this?
推荐答案
在expectationForPredicate(predicate: evaluatedWithObject: handler:)
中,您没有给出实际的对象,而是给出了在视图层次结构中查找该对象的查询.因此,例如,这是一个有效的测试:
In expectationForPredicate(predicate: evaluatedWithObject: handler:)
you don't give an actual object, but a query to find it in the view hierarchy. So, for example, this is a valid test:
let predicate = NSPredicate(format: "exists == 1")
let query = XCUIApplication().buttons["Button"]
expectationForPredicate(predicate, evaluatedWithObject: query, handler: nil)
waitForExpectationsWithTimeout(3, handler: nil)
查看 UI测试备忘单和文档 a>由标题(目前没有任何官方文档)生成,全部由Joe Masilotti提供.
Check out UI Testing Cheat Sheet and documentation generated from the headers (there is no official documentation at the moment), all by Joe Masilotti.
这篇关于Swift2 UI测试-等待元素出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!