【发布时间】:2016-05-17 12:44:02
【问题描述】:
我有一个非常简单的XCTestCase 实现,它测试对按钮的点击并期望出现一个警报控制器。问题是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"), evaluatedWithObject: button, handler: nil)
waitForExpectationsWithTimeout(5.0, handler: nil)
}
我在 Xcode 7.3.1 中遇到了这个问题我错过了什么吗?是bug吗?
【问题讨论】:
-
我已经向 Apple 报告了一个错误:openradar.appspot.com/26320475
-
这可能是时间问题吗?如果您在尝试点击之前添加 1 秒等待时间,会发生什么情况?不幸的是,UI 自动化框架,尤其是事件生成,充满了问题。
-
恐怕不行。即使我等待这个期望也不起作用
expectationForPredicate(NSPredicate(format: "hittable == 1"), evaluatedWithObject: button, handler: nil) -
你等了多久?您是否尝试了谓词格式“exists == true”?您是否也在 tap() 之前尝试了 sleep(5)?
-
检查这是否有效:>
/*Sends a tap event to a hittable/unhittable element.*/ public extension XCUIElement { func forceTapElement() { if self.hittable { self.tap() } else { let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0)) coordinate.tap() } } }
标签: ios swift integration-testing xcode-ui-testing