【发布时间】:2015-10-09 03:04:34
【问题描述】:
我注意到当网络速度较慢时,我的所有 UI 测试都会失败。例如,用户会尝试登录,然后下一个屏幕加载速度不够快,以使另一个 UIElement 出现在屏幕上。
如何在不使用 delay() 的情况下处理慢速网络连接?
【问题讨论】:
标签: ios ui-automation ios-ui-automation
我注意到当网络速度较慢时,我的所有 UI 测试都会失败。例如,用户会尝试登录,然后下一个屏幕加载速度不够快,以使另一个 UIElement 出现在屏幕上。
如何在不使用 delay() 的情况下处理慢速网络连接?
【问题讨论】:
标签: ios ui-automation ios-ui-automation
您绝对应该看看多线程。在处理网络连接时,您应该在辅助线程中进行所有这些处理。否则,主线程将被阻塞,应用程序将无法响应用户。
多线程是一个很大的课题。我建议您为此开始查看Apple's reference。也可以参考a great course on iTunes U(第11讲)。
如果您只是想试一试,这里是您需要的实际代码(类似):
dispatch_queue_t newQueue = dispatch_queue_create("networkQueue", NULL);
dispatch_async(newQueue, ^{
// here you need to call the networking processes
dispatch_async(dispatch_get_main_queue(), ^{
// if you need to update your UI, you need to get back to the main queue.
// This block will be executed in your main queue.
});
});
【讨论】:
我知道的唯一方法是使用延迟。从互联网加载内容时,我通常有一个活动指示器。所以我在显示活动指示器时添加了延迟
while (activityIndicator.isVisible())
{
UIALogger.logMessage("Loading");
UIATarget.localTarget().delay(1);
}
【讨论】:
查看UIATarget 中的pushTimeout 和popTimeout 方法。你可以找到文档here。
这是我们 iOS 应用 UIAutomation 测试中的一个代码示例:
// Tap "Post" button, which starts a network request
mainWindow.buttons()["post.button.post"].tap();
// Wait for maximum of 30 seconds to "OKAY" button to be valid
target.pushTimeout(30);
// Tap the button which is shown from the network request success callback
mainWindow.buttons()["dialog.button.okay"].tap();
// End the wait scope
target.popTimeout();
【讨论】: