gpt4 book ai didi

ios - UITests 在组中运行时失败但在独立运行时成功

转载 作者:搜寻专家 更新时间:2023-11-01 06:28:32 26 4
gpt4 key购买 nike

我目前正在处理一个包含许多异步代码的项目,并且我正在为这个项目编写 UITest。在开发过程中,我一个一个地运行它们,但从不成组运行。所以我认为测试是成功的。但是当一起测试它们时,大多数都失败了。我正确地实现了设置和拆卸。我研究了这种行为的原因,但找不到解决此问题的任何好的答案。这就像等待期望没有正常工作......

这是产生大部分问题的方法。每次我想等待一个元素出现时,我都会调用它。

/*
* This method will wait for an element (10 seconds default) afterwards it will assert it existence
*
* - parameter toAppear:The condition we are waiting for.
* - parameter element: The element to be expected
* - parameter timeout: The MAX time that the function will wait for the element, 10 seconds if none is given
* - parameter file: The file where the error will be displayed, the current file will be used in case none is provided
* - parameter line: The code line where the error will be displayed, the current line will be used in case none is provided
*/
static func assertForElement(toAppear: Bool, _ element: XCUIElement?, timeout: TimeInterval = 10, file: String = #file, line: Int = #line) {

guard let currentTestCase = BaseXCTestCase.CurrentTestCase else {
return
}

guard let element = element else {
let message = "Element cannot be empty"

currentTestCase.recordFailure(withDescription: message, inFile: file, atLine: line, expected: true)

return
}

let existsPredicate = NSPredicate(format: "exists == \(toAppear)")

currentTestCase.expectation(for: existsPredicate, evaluatedWith: element, handler: nil)
currentTestCase.waitForExpectations(timeout: timeout) { [weak currentTestCase] (error: Error?) in

if (error != nil) {
let appearMessage = "Failed to find \(String(describing: element)) after \(timeout) seconds."
let disappearMessage = "Failed to see \(String(describing: element)) disappear after \(timeout) seconds."

currentTestCase?.recordFailure(withDescription: ((toAppear == false) ? disappearMessage : appearMessage), inFile: file, atLine: line, expected: true)
}
}
}

有没有人有好的 UITest 异步代码方法?

非常感谢您!

更新

这是我收到的错误警告。

大多数时候我只是得到:异步等待失败:超过 10 秒的超时,未满足的期望:“Expect predicateexists == 1for object but the元素在模拟器中可见且可点击。另一方面,我得到:捕获“NSInternalInconsistencyException”,“API violation - creating expectations while already in waiting mode.”...

最佳答案

您的错误消息暗示您在调用 waitForExpectations 之后正在创建额外的期望。

首先创建所有期望,然后调用 waitForExpectations 作为测试中的最后一行代码。

关于ios - UITests 在组中运行时失败但在独立运行时成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50564548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com