问题描述
我有一个非常简单的XCTestCase
实现,该实现测试按钮上的轻击并期望出现Alert控制器.问题是tap()
方法不起作用.在相关按钮的IBAction中放置一个断点,我意识到逻辑甚至没有被调用.
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)
}
}
此外,复制button.tap()
指令会使测试通过,如下所示:
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)
}
我在Xcode 7.3.1中面临此问题,我缺少什么吗?是虫子吗?
I am facing this issue in Xcode 7.3.1 Am I missing something? Is it a bug?
推荐答案
因此,Apple工程师对我的错误报告做出了回应:
So an Apple engineer responded to my bug report saying:
要尝试解决该问题,请考虑在以下位置稍加延迟 测试的开始(sleep(1)就足够了.)
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)
}
这篇关于XCUIElement tap()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!