以下测试调用的方法是XCTestCase的扩展。目标:
waitForElementExists
方法返回,因为元素存在或waitForElementExists
方法未通过调用它的测试用例/ setUp方法,因为该元素在指定时间内不存在extension XCTestCase
{
/**
Wait for the view to load
Note: Must be a part of XCTestCase in order to utilize expectationForPredicate and waitForExpectationsWithTimeout
- Parameter
- element: The XCUIElement representing the view
- timeout: The NSTimeInterval for how long you want to wait for the view to be loaded
- file: The file where this method was called
- line: The line where this method was called
*/
func waitForElementExists(element: XCUIElement, timeout: NSTimeInterval = 60,
file: String = #file, line: UInt = #line)
{
let exists = NSPredicate(format: "exists == true")
expectationForPredicate(exists, evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(timeout) { (error) -> Void in
if (error != nil)
{
let message = "Failed to find \(element) after \(timeout) seconds."
self.recordFailureWithDescription(message,
inFile: file, atLine: line, expected: true)
}
}
}
}
override func setUp()
{
super.setUp()
// Stop immediately when a failure occurs.
continueAfterFailure = false
XCUIApplication().launch()
waitForElementExists(XCUIApplication().buttons["Foo"])
}
func testSample()
{
print("Success")
}
这个有效!永远不会调用testSample
。
但是如果我们将waitForElementExists调用移动到辅助方法呢?
这里,测试用例继续,好像断言从未发生过一样。如果我在waitForElementExists
中放置一个断点,我会看到continueAfterFailure
设置为true,所以很明显它没有与主要测试用例连接到相同的代码。
lazy var SomeHelper = SomeHelperClass()
override func setUp()
{
super.setUp()
// Stop immediately when a failure occurs.
continueAfterFailure = false
XCUIApplication().launch()
SomeHelper.waitForReady()
}
func testSample()
{
print("Success")
}
class SomeHelperClass: XCTestCase
{
/**
Wait for the button to be loaded
*/
func waitForReady()
{
waitForElementExists(XCUIApplication().buttons["Foo"])
}
}
答案 0 :(得分:0)
由于您的助手类是XCTestCase的子类,因此它具有自己的continueAfterFailure
属性,默认情况下为true。
如果你想要一个帮助器类,它不应该从XCTestCase下降,因为XCTestCase子类应该实现测试方法。如果需要从辅助类中的XCTestCase扩展访问功能,请在创建辅助类时通过组合传入测试用例对象。
class SomeHelper {
let testCase: XCTestCase
init(for testCase: XCTestCase) {
self.testCase = testCase
}
func await(_ element: XCUIElement) {
testCase.waitForElementExists(element)
}
}
class MyTests: XCTestCase {
let app = XCUIApplication()
var helper: SomeHelper!
func setUp() {
continueAfterFailure = false
helper = SomeHelper(for: self)
helper.await(app.buttons["foo"])
}
}