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

Evg*_*nii 18 ios xctest swift xcode7 xcode-ui-testing

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

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

XCTAssert(app.staticTexts["Text from the network"].exists) // fails
Run Code Online (Sandbox Code Playgroud)

如果我首先将tap或任何其他事件发送到文本,它确实有效:

app.staticTexts["Text from the network"].tap()
XCTAssert(app.staticTexts["Text from the network"].exists) // works
Run Code Online (Sandbox Code Playgroud)

看起来如果我只是调用exists它立即评估它并失败,因为尚未从网络下载文本.但我认为当我调用该tap()方法时,它会等待文本出现.

有没有更好的方法来检查是否存在从网络传送的文本?

像(这段代码不起作用):

XCTAssert(app.staticTexts["Text from the network"].eventuallyExists)
Run Code Online (Sandbox Code Playgroud)

Joe*_*tti 29

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];
Run Code Online (Sandbox Code Playgroud)

首先创建一个查询,等待带有文本"Hello,world!"的标签.出现.谓词在元素存在时匹配(element.exists == YES).然后传递谓词并根据标签对其进行评估.

如果在满足预期之前经过五秒钟,则测试将失败.您还可以附加一个处理程序块,在期望失败或超时时调用该处理程序块.

如果您正在寻找有关UI测试的更多信息,请查看Xcode 7中的UI测试.