【发布时间】:2017-03-02 13:24:03
【问题描述】:
我正在使用 XCTest 为我的应用程序编写 UITest 案例。应用程序在主屏幕中进行多次服务器调用。我无法导航到下一个屏幕。自动化通常会保持空闲 1 分钟甚至更长的时间
等待应用空闲
或
无法监控事件循环
有没有办法让应用程序执行我的测试用例打破这个???
【问题讨论】:
标签: ios xcode xctest xctestcase
我正在使用 XCTest 为我的应用程序编写 UITest 案例。应用程序在主屏幕中进行多次服务器调用。我无法导航到下一个屏幕。自动化通常会保持空闲 1 分钟甚至更长的时间
等待应用空闲
或
无法监控事件循环
有没有办法让应用程序执行我的测试用例打破这个???
【问题讨论】:
标签: ios xcode xctest xctestcase
我在 UI 测试类中设置了参数
let app = XCUIApplication()
app.launchArguments = ["NoAnimations"]
app.launch()
在我的 Appdelegate 的 didFinishLaunchingWithOptions 方法中,我进行了检查
NSArray *args = [NSProcessInfo processInfo].arguments;
for (NSString *arg in args){
if ([arg isEqualToString:@"NoAnimations"]){
[UIView setAnimationsEnabled:false];
}
}
所以现在我的应用程序中不会有任何动画,并且我的应用程序不再被阻止。这将我的自动化时间从 25 分钟缩短到 2 分钟。
【讨论】:
我的建议是帮助您使用以下这两种方法之一。第一个等待一个元素出现在屏幕上。第二个元素正在等待命中。但无论如何这些方法对你有帮助,也许你可以使用方法 sleep(param)。喜欢sleep(5)。等待 5 秒
import XCTest
class BaseTestCase: XCTestCase {
func waitForElementToAppear(element: XCUIElement, timeout: NSTimeInterval = 60, file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "exists == true")
expectationForPredicate(existsPredicate,
evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(timeout) { (error) -> Void in
if (error != nil) {
let message = "Failed to find \(element) after \(timeout) seconds."
self.recordFailureWithDescription(message, inFile: file, atLine: line, expected: true)
}
}
}
func waitForHittable(element: XCUIElement, timeout: NSTimeInterval = 70, file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "hittable == 1")
expectationForPredicate(existsPredicate, evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(timeout) { (error) -> Void in
if (error != nil) {
let message = "Failed to find \(element) after \(timeout) seconds."
self.recordFailureWithDescription(message,
inFile: file, atLine: line, expected: true)
}
}
}
}
我希望能在某些方面有所帮助
【讨论】: