【问题标题】:How to handle slow network connections with UIAutomation如何使用 UIAutomation 处理慢速网络连接
【发布时间】:2015-10-09 03:04:34
【问题描述】:

我注意到当网络速度较慢时,我的所有 UI 测试都会失败。例如,用户会尝试登录,然后下一个屏幕加载速度不够快,以使另一个 UIElement 出现在屏幕上。

如何在不使用 delay() 的情况下处理慢速网络连接?

【问题讨论】:

    标签: ios ui-automation ios-ui-automation


    【解决方案1】:

    您绝对应该看看多线程。在处理网络连接时,您应该在辅助线程中进行所有这些处理。否则,主线程将被阻塞,应用程序将无法响应用户。

    多线程是一个很大的课题。我建议您为此开始查看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.
    
        });
    });
    

    【讨论】:

    • 是的,我对 GCD 相当精通,我们确实在后台线程中实现了所有网络调用,但是,问题是我的自动化代码正在屏幕上寻找 UIElement,它目前还不可见。举个例子,假设你要加载一个充满文本的屏幕,但是你有一个微调器,直到文本加载。我希望我的自动化代码能够等待文本加载,但我不想使用 target.delay()
    • 可以用什么代替target.delay()?它似乎阻塞了应用程序的主线程。
    • @maremmle 我知道已经有一段时间了,但我刚刚发布了一个新答案,解释了我们如何在 UIAutomation 测试中等待网络/其他后台任务以防万一。
    【解决方案2】:

    我知道的唯一方法是使用延迟。从互联网加载内容时,我通常有一个活动指示器。所以我在显示活动指示器时添加了延迟

    while (activityIndicator.isVisible())
    {
        UIALogger.logMessage("Loading");
        UIATarget.localTarget().delay(1);
    } 
    

    【讨论】:

      【解决方案3】:

      查看UIATarget 中的pushTimeoutpopTimeout 方法。你可以找到文档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();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2017-11-15
        • 2011-02-27
        • 2012-04-09
        • 1970-01-01
        • 2010-11-08
        • 1970-01-01
        相关资源
        最近更新 更多