我有一个非常简单的XCTestCase实现,它测试按下按钮并期望出现一个Alert控制器.问题是tap()方法不起作用.在相关按钮的IBAction中放置一个断点我意识到逻辑甚至没有被调用.
class uitestsampleUITests: XCTestCase { var app: XCUIApplication! override func setUp() { super.setUp() continueAfterFailure = false app = XCUIApplication() app.launch() } func testButton() { let button = app.buttons["Button"] button.tap() expectationForPredicate(NSPredicate(format: "exists == 1"),evaluatedWithObject: button,handler: nil) waitForExpectationsWithTimeout(5.0,handler: nil) } }
另外,复制button.tap()指令会使测试通过,如下所示:
func testButton() { let button = app.buttons["Button"] button.tap() button.tap() expectationForPredicate(NSPredicate(format: "exists == 1"),handler: nil) }
我在Xcode 7.3.1中遇到这个问题我错过了什么吗?这是一个错误吗?
解决方法
因此,一位Apple工程师回复了我的错误报告:
The second possibility is that you are running into a problem that
sometimes occurs where the application finishes launching but the
splash screen doesn’t immediately disappear and events dispatched to
the app are not handled properly.To try to work around that issue,consider placing a small delay at
the beginning of your test (sleep(1) should be enough).
所以我做了它现在它的工作原理:
override func setUp() { super.setUp() continueAfterFailure = false app = XCUIApplication() app.launch() sleep(1) }