ios - 在 iOS 上使用 UI 自动化测试屏幕跟踪

标签 ios objective-c testing google-analytics ios-ui-automation

所以我有了这个想法,使用 UI 自动化测试我的应用程序上的屏幕跟踪(使用 Google Analytics)的实现。

最初的想法是构建一个 UI 脚本来遍历屏幕,同时检查是否相应地发送了跟踪事件。我需要这个,因为有时我无法从 View Controller 中组合所有内容,或者事件未按预期顺序转发。不管怎样,我也应该测试我的应用程序的这一方面,我认为 UI 自动化就是答案。

我已经使用 UI 自动化工具实现了一个脚本来浏览屏幕,并且它工作正常。我什至使用了tuneup js。使代码更精简,更容易理解。 我期待有类似的东西(一般来说,语法只是一种简化):

Being on screen X
    Tap button A
Expect screen Y and tracking event for the screen Y

但是,据我所知,使用 UI 自动化无法测试屏幕跟踪。 还是我遗漏了什么?

我想创建一个位于所有 View 层次结构顶部的不可见 View ,并在每次加载新屏幕时更改其名称,以便我可以使用 UI 自动化对其进行测试,但这个想法听起来有点过头了。 .

你们有什么建议?寻找另一个 UI 自动化工具?用单元测试代替吗?

在此先感谢您的帮助

最佳答案

您可以使用 UIAlertView 并检查这些警报。您可以弹出警报,而不是发送分析事件,以便您可以在 UIAutomation 中检查它。

分析抽象框架,如 AnalyticsKit提供一种更改分析提供程序的简单方法。和 AnalyticsKit甚至有一个例子(看看 AnalyticsKitDebugProvider 类)。因此对您的生产代码的更改是最小的。

您可以使用构建配置,在其中设置构建变量来控制分析的初始化

id<AnalyticsKitProvider> provider
#ifdef USE_UI_AUTOMATION_ANALYTICS
provider = [[TestAutomationProvider alloc] init];
#else
provider = [[RealProvider alloc] initWithApiKey:API_KEY];
#endif

[AnalyticsKit initializeLoggers:@[provider]];

在 UIAutomation 中,您可以测试即将出现的警报。您可以利用 tuneup.js 包中的 assertions.js 来编写这样的函数

function checkForAlert()
{
    var alert = null;
    retry( function() {
          log("wait until alert appaers");
          alert = UIATarget.localTarget().frontMostApp().alert();
          assertNotNull(alert, "No alert found");
          assertTrue("The name you can choose for the alert" == alert.name()); 
          }, 5, 1.0);
    return alert;
};

这结合了等待警报和测试它是否最终出现。如果未出现警报,则测试将失败。

在您的测试中,您按以下方式使用它:

var analyticAlert = checkForAlert() // if alert appears it will be in the var, otherwise the test fails at this point.
analyticAlert.buttons()["OK"].tap(); // dismiss the alert

要完成这项工作,您还需要设置一个 onAlert 处理程序。否则 UIAutomation 将尝试立即解除您的警报。这必须在您的测试代码之前完成。 UIAutomation 文档中解释了警报处理。

function MyOnAlertHandler(alert)
{
   if("The name you choose"==alert.name()) // filter all alerts created by analytics provider
   {
      return true; // handle alert in your test
   }

   return false // automaticly dismiss all other 
}

UIATarget.onAlert = MyOnAlertHandler; // set the alert handler

关于ios - 在 iOS 上使用 UI 自动化测试屏幕跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911649/

相关文章:

ios - Controller 之间的导航 - 黑屏

ios - didReceiveRemoteNotification 将用户带到正确的 View

objective-c - 如何在 Xcode 4 中切换 .h 和 .m

ruby-on-rails - Rails Minitest 没有运行测试类

java - CDI @Alternative - 根据测试用例选择备选方案

java - 如何根据 JUnit 中的测试大小对测试进行分类

ios - 如何从远程通知操作按钮打开应用程序

html - 防止滚动拉动超出 iOS 元素的范围

ios - 扩展 UIApplicationDelegate 协议(protocol)

ios - 如何在没有 StoryBoard 的情况下以编程方式将单元格添加到 CollectionView