iPhone 4s 设备中的 XCTest 持续集成失败

XCTest Continuous Integration Failure in iPhone 4s device

我是 XCTest UI 测试的新手。我已经为 Xcode 持续集成创建了一个机器人。我添加了 iPhone 4s、5s 和 6 用于并行测试。

测试很简单。当我单击按钮时,带有 "Hai" 的标签应更改为 "Hello"。我刚刚断言最终标签值为 "Hello"。当我 运行 在 Xcode 本地测试时,它适用于所有设备。但是当它提交并使用bot集成时,测试用例仅失败iPhone 4s,显示

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

我不知道测试失败的原因。我写的测试用例如下所示:

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!")
}

可能 4S 在您的 CI 服务器上比在您的本地机器上稍慢,并且它通常比后来设备的模拟器运行得慢。

如果在完成点击后对视图层次结构进行采样之前操作未完成,那么放置 sleep(3) 将无济于事,因为视图层次结构在点击后的任何时候都不一定会刷新。

为了解决这个问题,使用期望,这将确保在每次检查之前刷新断言所针对的视图层次结构。

expectationForPredicate(NSPredicate(format: "label == 'Hello!'"), evaluatedWithObject: changedLabel, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)