Question

So I had this idea to test the implementation of my screen tracking (with Google Analytics) on my app using UI automation.

The original idea was to build a UI script to go through the screens while checking if the tracking events are being sent accordingly. I need this as sometimes I'm not able to compose everything out of view controllers or the events are not forwarded in the expected order. Regardless of that, I should test this aspect of my app as well and I thought that UI automation was the answer.

I have implemented a script to go through the screens using the UI automation instrument and this is working correctly. I even went so far as using tuneup js to make the code more streamlined and easier to follow. I was expecting to have something like (in general terms, the syntax is only a simplification):

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

However, as far as I was able to check, testing the screen tracking is something that is not possible with the UI automation. Or am I missing something?

I thought of creating an invisible view that stays on top of all the view hierarchy and changing its name every time a new screen is loaded to allow me to test it with UI automation but the idea sounded a little over the top...

What do you people suggest? Look for another UI automation tool? Do it with unit testing instead?

Thanks in advance for any help

Was it helpful?

Solution

You could use a UIAlertView and inspect those alerts. Instead of sending the analytics events you can pop up the alert so you can check on it in UIAutomation.

Analytics abstraction frameworks like AnalyticsKit provide an easy way to change the analytics provider. And AnalyticsKit even has an example for that (take a look at the AnalyticsKitDebugProvider class). So the changes to your production code are minimal.

You could use a build configuration where you set a build variable to control the initialization of your analytics

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

[AnalyticsKit initializeLoggers:@[provider]];

In UIAutomation you can test for the alert coming up. You can utilize assertions.js out of the tuneup.js package to write a function like this

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;
};

This combines waiting for the alert and testing if it finally appear. If the alert not appears, the test will fail.

In your test you use this in the following way:

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

To make this work you also need to set an onAlert handler. Otherwise UIAutomation would try to dismiss your alert immediately. This has to be done before your tests code. Alert handling is explained in the UIAutomation docs.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top