Reputation: 15
I'm new to XCTest UI Testing. I've created a bot for Xcode continuous integration. I've added iPhone 4s, 5s and 6 for parallel testing.
The test is simple. When I click a button, a label with "Hai" should change to "Hello". I've just asserted the final label value is "Hello". When I run the test locally in Xcode, it works for all devices. But when it is committed and integrate using bot, the test case is failed for iPhone 4s only, showing that
Bot Issue for Begin Bot (test failure in -[BeginUITests testExample()]) Integration #41 of Begin Bot
Assertion: XCTAssertEqual failed: ("Optional("Hai")") is not equal to ("Optional("Hello!")") - File: Begin/BeginUITests/BeginUITests.swift:43
I don't know the reason why the test failed. The test case i've written is shown below:
func testExample() {
let app = XCUIApplication()
let firstLabel = app.staticTexts.elementBoundByIndex(0)
let button = app.buttons["Button"]
XCTAssert(button.exists)
XCTAssert(firstLabel.exists)
button.tap()
sleep(3)
let changedLabel = app.staticTexts.elementBoundByIndex(0)
XCTAssertEqual(changedLabel.label, "Hello!")
}
Upvotes: 0
Views: 87
Reputation: 7659
It's possible that the 4S is slightly slower on your CI server than on your local machine, and it generally operates more slowly than simulators of later devices.
Putting sleep(3)
wouldn't help if the operation wasn't completed before the view hierarchy was sampled after the tap was completed, as the view hierarchy is not necessarily refreshed at any point after the tap.
To combat this, use expectations, which will ensure that the view hierarchy the assertion is being made against will be refreshed before each check.
expectationForPredicate(NSPredicate(format: "label == 'Hello!'"), evaluatedWithObject: changedLabel, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
Upvotes: 0