首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOS UI测试-等待网络呼叫

IOS UI测试-等待网络呼叫
EN

Stack Overflow用户
提问于 2018-02-21 18:31:59
回答 1查看 1.2K关注 0票数 1

我正在使用XCTest框架为IOS编写一个测试用例。如何等待网络调用,并在网络调用结束后开始执行。目前,我正在使用sleep()来等待。但是,这并不是最好的方式。

那么,有没有其他的方法呢?

EN

回答 1

Stack Overflow用户

发布于 2018-02-22 20:09:21

请阅读文档here

代码语言:javascript
复制
func testDownloadWebData() {
    // Create an expectation for a background download task.
    let expectation = XCTestExpectation(description: "Download apple.com home page")

    // Create a URL for a web page to be downloaded.
    let url = URL(string: "https://apple.com")!

    // Create a background task to download the web page.
    let dataTask = URLSession.shared.dataTask(with: url) { (data, _, _) in

        // Make sure we downloaded some data.
        XCTAssertNotNil(data, "No data was downloaded.")

        // Fulfill the expectation to indicate that the background task has finished successfully.
        expectation.fulfill()

    }

    // Start the download task.
    dataTask.resume()

    // Wait until the expectation is fulfilled, with a timeout of 10 seconds.
    wait(for: [expectation], timeout: 10.0)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48903660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档