Xcode UI 测试用例中的延迟/等待

问题描述 投票:0回答:15

我正在尝试使用 Xcode 7 beta 2 中提供的新 UI 测试编写一个测试用例。该应用程序有一个登录屏幕,可以调用服务器进行登录。由于它是异步操作,因此存在与此相关的延迟。

有没有办法在继续下一步之前在 XCTestCase 中引起延迟或等待机制?

没有适当的文档可用,我浏览了类的头文件。找不到与此相关的任何内容。

有什么想法/建议吗?

ios ios9 xcode-ui-testing xcode7-beta2 xctwaiter
15个回答
257
投票

另外,你可以直接睡觉:

sleep(10)

由于 UITests 在另一个进程中运行,所以这是可行的。我不知道这有多可取,但它确实有效。


193
投票

异步 UI 测试是在 Xcode 7 Beta 4 中引入的。等待带有文本“Hello, world!”的标签。要出现,您可以执行以下操作:

let app = XCUIApplication()
app.launch()

let label = app.staticTexts["Hello, world!"]
let exists = NSPredicate(format: "exists == 1")

expectationForPredicate(exists, evaluatedWithObject: label, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)

更多有关 UI 测试的详细信息可以在我的博客上找到。


106
投票

iOS 11 / Xcode 9

<#yourElement#>.waitForExistence(timeout: 5)

这是本网站上所有自定义实现的绝佳替代品!

请务必在这里查看我的答案:https://stackoverflow.com/a/48937714/971329。在那里,我描述了等待请求的替代方法,这将大大减少测试运行的时间!


79
投票

Xcode 9XCTWaiter

引入了新技巧

测试用例显式等待

wait(for: [documentExpectation], timeout: 10)

Waiter 实例委托测试

XCTWaiter(delegate: self).wait(for: [documentExpectation], timeout: 10)

Waiter 类返回结果

let result = XCTWaiter.wait(for: [documentExpectation], timeout: 10)
switch(result) {
case .completed:
    //all expectations were fulfilled before timeout!
case .timedOut:
    //timed out before all of its expectations were fulfilled
case .incorrectOrder:
    //expectations were not fulfilled in the required order
case .invertedFulfillment:
    //an inverted expectation was fulfilled
case .interrupted:
    //waiter was interrupted before completed or timedOut
}

使用示例

Xcode 9 之前

目标C

- (void)waitForElementToAppear:(XCUIElement *)element withTimeout:(NSTimeInterval)timeout
{
    NSUInteger line = __LINE__;
    NSString *file = [NSString stringWithUTF8String:__FILE__];
    NSPredicate *existsPredicate = [NSPredicate predicateWithFormat:@"exists == true"];

    [self expectationForPredicate:existsPredicate evaluatedWithObject:element handler:nil];

    [self waitForExpectationsWithTimeout:timeout handler:^(NSError * _Nullable error) {
        if (error != nil) {
            NSString *message = [NSString stringWithFormat:@"Failed to find %@ after %f seconds",element,timeout];
            [self recordFailureWithDescription:message inFile:file atLine:line expected:YES];
        }
    }];
}

使用

XCUIElement *element = app.staticTexts["Name of your element"];
[self waitForElementToAppear:element withTimeout:5];

斯威夫特

func waitForElementToAppear(element: XCUIElement, timeout: NSTimeInterval = 5,  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)
            }
        }
    }

使用

let element = app.staticTexts["Name of your element"]
self.waitForElementToAppear(element)

let element = app.staticTexts["Name of your element"]
self.waitForElementToAppear(element, timeout: 10)

来源


39
投票

从 Xcode 8.3 开始,我们可以使用

XCTWaiter
http://masilotti.com/xctest-waiting/

func waitForElementToAppear(_ element: XCUIElement) -> Bool {
    let predicate = NSPredicate(format: "exists == true")
    let expectation = expectation(for: predicate, evaluatedWith: element, 
                                  handler: nil)

    let result = XCTWaiter().wait(for: [expectation], timeout: 5)
    return result == .completed
}

另一个技巧是编写一个

wait
函数,感谢 John Sundell 向我展示了它

extension XCTestCase {

  func wait(for duration: TimeInterval) {
    let waitExpectation = expectation(description: "Waiting")

    let when = DispatchTime.now() + duration
    DispatchQueue.main.asyncAfter(deadline: when) {
      waitExpectation.fulfill()
    }

    // We use a buffer here to avoid flakiness with Timer on CI
    waitForExpectations(timeout: duration + 0.5)
  }
}

并像这样使用它

func testOpenLink() {
  let delegate = UIApplication.shared.delegate as! AppDelegate
  let route = RouteMock()
  UIApplication.shared.open(linkUrl, options: [:], completionHandler: nil)

  wait(for: 1)

  XCTAssertNotNil(route.location)
}

22
投票

这将产生延迟,而不会让线程进入睡眠状态或在超时时抛出错误:

let delayExpectation = XCTestExpectation()
delayExpectation.isInverted = true
wait(for: [delayExpectation], timeout: 5)

因为期望反转,所以会悄悄超时。


14
投票

根据@Ted的回答,我使用了这个扩展:

extension XCTestCase {

    // Based on https://stackoverflow.com/a/33855219
    func waitFor<T>(object: T, timeout: TimeInterval = 5, file: String = #file, line: UInt = #line, expectationPredicate: @escaping (T) -> Bool) {
        let predicate = NSPredicate { obj, _ in
            expectationPredicate(obj as! T)
        }
        expectation(for: predicate, evaluatedWith: object, handler: nil)

        waitForExpectations(timeout: timeout) { error in
            if (error != nil) {
                let message = "Failed to fulful expectation block for \(object) after \(timeout) seconds."
                let location = XCTSourceCodeLocation(filePath: file, lineNumber: line)
                let issue = XCTIssue(type: .assertionFailure, compactDescription: message, detailedDescription: nil, sourceCodeContext: .init(location: location), associatedError: nil, attachments: [])
                self.record(issue)
            }
        }
    }

}

你可以这样使用它

let element = app.staticTexts["Name of your element"]
waitFor(object: element) { $0.exists }

它还允许等待元素消失,或任何其他属性更改(通过使用适当的块)

waitFor(object: element) { !$0.exists } // Wait for it to disappear

13
投票

Xcode 测试等待

就我而言,

sleep
产生了副作用,所以我使用了
wait

let _ = XCTWaiter.wait(for: [XCTestExpectation(description: "Hello World!")], timeout: 2.0)

10
投票

编辑:

我实际上刚刚想到,在 Xcode 7b4 中,UI 测试现在有了

expectationForPredicate:evaluatedWithObject:handler:

原文:

另一种方法是旋转运行循环一段设定的时间。 仅当您知道需要等待多少(估计)时间时才真正有用

对象-C:

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow: <<time to wait in seconds>>]]

斯威夫特:

NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: <<time to wait in seconds>>))

如果您需要测试某些条件才能继续测试,这并不是非常有用。 要运行条件检查,请使用

while
循环。


9
投票

我们在当前公司的做法是创建一个 XCUIElement 表达式期望(以创建通用的等待方法)。我们按照以下方式进行操作,以确保它是可维护的(有很多期望变化,并且不想创建大量方法/特定谓词来做到这一点。

斯威夫特5

基本方法

表达式用于形成动态谓词值。我们可以从谓词创建

XCTNSPredicateExpectation
,然后将其传递到
XCTWaiter
以显式等待。如果结果不是
completed
,那么我们会失败并显示一条可选消息。

@discardableResult
func wait(
    until expression: @escaping (XCUIElement) -> Bool,
    timeout: TimeInterval = 15,
    message: @autoclosure () -> String = "",
    file: StaticString = #file,
    line: UInt = #line
) -> Self {
    if expression(self) {
        return self
    }

    let predicate = NSPredicate { _, _ in
        expression(self)
    }

    let expectation = XCTNSPredicateExpectation(predicate: predicate, object: nil)

    let result = XCTWaiter().wait(for: [expectation], timeout: timeout)

    if result != .completed {
        XCTFail(
            message().isEmpty ? "expectation not matched after waiting" : message(),
            file: file,
            line: line
        )
    }

    return self
}

用法

app.buttons["my_button"].wait(until: { $0.exists })
app.buttons["my_button"].wait(until: { $0.isHittable })

关键路径

然后我们将其包装在一个方法中,其中 keyPath 和

match
值构成表达式。

@discardableResult
func wait<Value: Equatable>(
    until keyPath: KeyPath<XCUIElement, Value>,
    matches match: Value,
    timeout: TimeInterval = 15,
    message: @autoclosure () -> String = "",
    file: StaticString = #file,
    line: UInt = #line
) -> Self {
    wait(
        until: { $0[keyPath: keyPath] == match },
        timeout: timeout,
        message: message,
        file: file,
        line: line
    )
}

用法

app.buttons["my_button"].wait(until: \.exists, matches: true)
app.buttons["my_button"].wait(until: \.isHittable, matches: false)

然后您可以包装该方法,其中对于我发现最常见的用例,

match
值始终为
true

用法

app.buttons["my_button"].wait(until: \.exists)
app.buttons["my_button"].wait(until: \.isHittable)

我写了一篇关于它的文章,并在那里获取了完整的扩展文件:https://sourcediving.com/clean-waiting-in-xcuitest-43bab495230f


4
投票

以下代码仅适用于 Objective C。

- (void)wait:(NSUInteger)interval {

    XCTestExpectation *expectation = [self expectationWithDescription:@"wait"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [expectation fulfill];
    });
    [self waitForExpectationsWithTimeout:interval handler:nil];
}

只需调用此函数,如下所示。

[self wait: 10];

3
投票
   let app = XCUIApplication()
    app.launch()

     //Find the button in the UI 
    let SettingsButton =
        app.navigationBars["HomeView"].buttons["Settings"]
    XCTAssertTrue(settingButton.waitForExistence(timeout: 10))

2
投票

睡眠会阻塞线程

“当线程被阻塞时,不会发生运行循环处理。”

你可以使用waitForExistence

let app = XCUIApplication()
app.launch()

if let label = app.staticTexts["Hello, world!"] {
label.waitForExistence(timeout: 5)
}

0
投票

根据 XCUIElement 的 API

.exists
可用于检查查询是否存在,因此以下语法在某些情况下可能很有用!

let app = XCUIApplication()
app.launch()

let label = app.staticTexts["Hello, world!"]
while !label.exists {
    sleep(1)
}

如果您确信最终会满足您的期望,您可以尝试运行此程序。应该注意的是,如果等待时间太长,崩溃可能会更好,在这种情况下,应该使用@Joe Masilotti 帖子中的

waitForExpectationsWithTimeout(_,handler:_)


0
投票

下面的内容与原始问题无关,而是等待特定的元素。 我们注意到从Xcode9.0开始,有一个XCUIElement

的API
func waitForExistence(timeout: TimeInterval) -> Bool
© www.soinside.com 2019 - 2024. All rights reserved.