加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 等待所有HTTP请求在XCode UI测试中完成?

发布时间:2020-12-14 19:25:57 所属栏目:百科 来源:网络整理
导读:在XCode中测试UI时是否有办法等待所有网络请求完成? 我有一个应用程序发送HTTP请求以从服务器获取一些数据,并且在UI测试中,我想等待在继续之前检索这些数据.目前我正在使用睡眠(1)但这种方法似乎不可靠. 解决方法 最好的办法是等待一些UI元素出现或消失.想
在XCode中测试UI时是否有办法等待所有网络请求完成?

我有一个应用程序发送HTTP请求以从服务器获取一些数据,并且在UI测试中,我想等待在继续之前检索这些数据.目前我正在使用睡眠(1)但这种方法似乎不可靠.

解决方法

最好的办法是等待一些UI元素出现或消失.想一想:

The framework acts like a user. It doesn’t care what code is running under the hood. The only thing that matters is what is visible on the screen.

也就是说,您可以在UI测试中使用wait for a label titled “Go!” to appear.

let app = XCUIApplication()
let goLabel = self.app.staticTexts["Go!"]
XCTAssertFalse(goLabel.exists)

let exists = NSPredicate(format: "exists == true")
expectationForPredicate(exists,evaluatedWithObject: goLabel,handler: nil)

app.buttons["Ready,set..."].tap()
waitForExpectationsWithTimeout(5,handler: nil)
XCTAssert(goLabel.exists)

你也可以extract that into a helper method.如果你使用一些Swift编译器魔法,你甚至可以在调用该方法的行上发生失败消息.

private fund waitForElementToAppear(element: XCUIElement,file: String = #file,line: UInt = #line) {
    let existsPredicate = NSPredicate(format: "exists == true")
    expectationForPredicate(existsPredicate,evaluatedWithObject: element,handler: nil)

    waitForExpectationsWithTimeout(5) { (error) -> Void in
        if (error != nil) {
            let message = "Failed to find (element) after 5 seconds."
            self.recordFailureWithDescription(message,inFile: file,atLine: line,expected: true)
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读