微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/iOS/332916.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐