Reputation: 209
I was just practicing TDD and wrote a very simple function for a testing app. The UI is simple: it has a button with "0" as its title initially. In my view controller, I have a local variable "score". Every time I tap the button it would increase the "score". The button title will be updated with the new "score" value. The button ui update logic is in "score"'s "didSet" property observer.
Everything is cool except the UI testing. I have two ui test functions, one is to tap the button once and another is to tap the button twice. Now the weird thing happens. Below are the screenshots of my two ui test functions.
Every time I ran the test, sometimes both functions passed while sometimes one or two of them failed. The error is always as shown "aNumber is not equal to aNumber + 1". Looks like there are some respond time issue. It seems like the UI update is much slower than the button label result retrieval. I'm not sure whether I get the value of button.label too soon before it was really updated. Should I even update the UI in property observers? Is there any solution to pass the ui test? Appreciates for your helps!
Upvotes: 2
Views: 1134
Reputation: 41
I had the same issue in the simulator while in my iPhone the test passed.
I ended up adding a delay for the simulator like this:
struct Platform {
static let isSimulator: Bool = {
var isSim = false
#if arch(i386) || arch(x86_64)
isSim = true
#endif
return isSim
}()
}
class YourUITests: XCTestCase {
override func setUp() {
super.setUp()
if Platform.isSimulator {
sleep(1)
}
}
}
Hope that helps.
Upvotes: 1
Reputation: 2632
Regarding
both functions passed while sometimes one or two of them failed
UITest is execute in separate process. so if button title update is slower than XCTAssert, It will fail because compare oldValue with expected Value
After tap your button just wait to test correctly
using this function waitForExpectationsWithTimeout(3,nil)
func testTapNumberButtonIncrementScore() {
let app = XCUIApplication()
app.buttons["numberButton"].tap()
waitForExpectationsWithTimeout(3, handler: nil)
XCTAssertEqual(app.buttons["numberButton"].title, "1")
}
func testTapNumberButtonTwiceIncrementTo2() {
let app = XCUIApplication()
app.buttons["numberButton"].tapWithNumberOfTaps(2, numberOfTouches: 1)
waitForExpectationsWithTimeout(3, handler: nil)
XCTAssertEqual(app.buttons["numberButton"].title, "2")
}
I hope it works to you :)
Upvotes: 1