Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Xcode中的UI测试中检查是否存在从网络显示的静态文本?_Ios_Swift_Xctest_Xcode7_Xcode Ui Testing - Fatal编程技术网

Ios 如何在Xcode中的UI测试中检查是否存在从网络显示的静态文本?

Ios 如何在Xcode中的UI测试中检查是否存在从网络显示的静态文本?,ios,swift,xctest,xcode7,xcode-ui-testing,Ios,Swift,Xctest,Xcode7,Xcode Ui Testing,我正在使用Xcode 7 XCTest中引入的UI测试API。在我的屏幕上有一个从网络加载的文本 如果我只是用exists属性检查它,测试就会失败 XCTAssert(app.staticTexts["Text from the network"].exists) // fails 但是,如果我先将点击或任何其他事件发送到文本中,它确实会起作用,如下所示: app.staticTexts["Text from the network"].tap() XCTAssert(app.staticTe

我正在使用Xcode 7 XCTest中引入的UI测试API。在我的屏幕上有一个从网络加载的文本

如果我只是用
exists
属性检查它,测试就会失败

XCTAssert(app.staticTexts["Text from the network"].exists) // fails
但是,如果我先将点击或任何其他事件发送到文本中,它确实会起作用,如下所示:

app.staticTexts["Text from the network"].tap()
XCTAssert(app.staticTexts["Text from the network"].exists) // works
看起来,如果我只调用
exists
它会立即对其进行计算并失败,因为文本尚未从网络下载。但是我认为当我调用
tap()
方法时,它会等待文本出现

XCUIElement *label = self.app.staticTexts[@"Hello, world!"];
NSPredicate *exists = [NSPredicate predicateWithFormat:@"exists == 1"];

[self expectationForPredicate:exists evaluatedWithObject:label handler:nil];
[self waitForExpectationsWithTimeout:5 handler:nil];
有没有更好的方法来检查是否存在从网络发送的文本

类似(此代码不起作用):


Xcode 7 Beta 4增加了对异步事件的本机支持。下面是一个如何等待
UILabel
出现的快速示例

XCUIElement *label = self.app.staticTexts[@"Hello, world!"];
NSPredicate *exists = [NSPredicate predicateWithFormat:@"exists == 1"];

[self expectationForPredicate:exists evaluatedWithObject:label handler:nil];
[self waitForExpectationsWithTimeout:5 handler:nil];
首先创建一个查询,等待出现一个文本为“Hello,world!”的标签。当元素存在时,谓词匹配
(element.exists==YES)
。然后传入谓词并根据标签对其求值

如果在达到预期值之前经过五秒钟,则测试将失败。您还可以在中附加一个处理程序块,该块在预期失败或超时时被调用


如果您想了解有关UI测试的更多信息,请查看。

如果我正确理解您在检查目标文本是否存在时已经显示了目标文本,您可以尝试使用
可点击属性。

Swift 3:

XCTAssert(app.staticTexts["Text from the network"].exists) // fails
let predicate = NSPredicate(format: "exists == 1")
let query = app!.staticTexts["identifier"]
expectation(for: predicate, evaluatedWith: query, handler: nil)
waitForExpectations(timeout: 5, handler: nil)
它将连续5秒钟检查该文本是否显示

一旦在5秒内找到文本,它将执行进一步的代码。

XCode9有一个
xguielement

extension XCUIElement {
    // A method for tap element
    @discardableResult
    func waitAndTap() -> Bool {
        let _ = self.waitForExistence(timeout: 10)
        let b = self.exists && self.isHittable
        if (b) {
            self.tap()
        }
        return b
    }
}

// Ex:
if (btnConfig.waitAndTap() == true) {
    // Continue UI automation
} else {
    // `btnConfig` is not exist or not hittable.
}
但是我遇到了另一个问题,
元素
存在,但不可命中。所以我扩展了一个方法来等待一个元素可以命中

extension XCTestCase {
    /// Wait for XCUIElement is hittable.
    func waitHittable(element: XCUIElement, timeout: TimeInterval = 30) {
        let predicate = NSPredicate(format: "isHittable == 1")
        expectation(for: predicate, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: timeout, handler: nil)
    }
}

// Ex:
// waitHittable(element: btnConfig)

我更新了Xcode 7 Beta 4的答案。因为它彻底改变了被接受的答案,我想让你知道。