I have a very simple XCTestCase
implementation that tests a tap on a button and expects an Alert controller to show up. The problem is that the tap()
method doesn't work. Placing a breakpoint in the associated button's IBAction I realise the logic doesn't even get called.
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)
}
}
Also, duplicating the button.tap()
instruction makes the test pass, like this:
func testButton() {
let button = app.buttons["Button"]
button.tap()
button.tap()
expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: button, handler: nil)
waitForExpectationsWithTimeout(5.0, handler: nil)
}
I am facing this issue in Xcode 7.3.1 Am I missing something? Is it a bug?
So an Apple engineer responded to my bug report saying:
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).
So I did it and now it works:
override func setUp() {
super.setUp()
continueAfterFailure = false
app = XCUIApplication()
app.launch()
sleep(1)
}