【问题标题】:IOS UI Testing - Waiting for the network callsIOS UI 测试 - 等待网络调用
【发布时间】:2018-07-31 21:37:54
【问题描述】:

我正在使用 XCTest 框架为 IOS 编写测试用例。一旦网络调用完成,我们如何等待网络调用并开始执行。 目前,我正在使用 sleep() 等待。但是,这不是最好的方法。
那么,有没有其他办法呢?

【问题讨论】:

    标签: ios xctest ios-ui-automation xctestcase


    【解决方案1】:

    阅读文档here。

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

    【讨论】:

    • 如何让它用于 UI 测试,因为 UI 测试是在不考虑内部代码和 api url 的情况下完成的。上述解决方案是否适用于 UI 测试用例。如果没有,还有其他方法吗?
    • 要等待 UI 元素出现,您可以使用 func expectation(for predicate: NSPredicate, evaluatedWith object: Any, handler: XCTNSPredicateExpectation.Handler? = nil) -> XCTestExpectation,它会返回如上述示例代码中所等待的期望值。
    • 是的!正确的。但问题是,如何检测加载器是启动还是停止。您的帮助将不胜感激。提前致谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2015-09-19
    相关资源
    最近更新 更多