ios – XCUIElement tap()不工作

我有一个非常简单的XCTestCase实现,它测试按下按钮并期望出现一个Alert控制器.问题是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"),handler: nil)    
    }

我在Xcode 7.3.1中遇到这个问题我错过了什么吗?这是一个错误吗?

解决方法

因此,一位Apple工程师回复了我的错误报告:

The second possibility is that you are running into a problem that
sometimes occurs where the application finishes launching but the
splash screen doesn’t immediately disappear and events dispatched to
the app are not handled properly.

To try to work around that issue,consider placing a small delay at
the beginning of your test (sleep(1) should be enough).

所以我做了它现在它的工作原理:

override func setUp() {
    super.setUp()
    continueAfterFailure = false
    app = XCUIApplication()
    app.launch()
    sleep(1)
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...