Testing screen tracking with UI automation on iOS
Asked Answered
G

1

13

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

Gretagretal answered 7/4, 2014 at 11:52 Comment(5)
Would it be possible to do manual test of Google Analytics? Maybe you can test three devices three different times each; the way my partner and I did was to analyze data from the first one or two days download, and asked friends what pages they had visited.Bally
reference that GoogleAnalytics can be utilized in simulator: https://mcmap.net/q/909122/-is-it-possible-to-track-the-app-with-google-analytics-in-ios-simulatorBally
I want to make this automatic so I can run it in a server as regression tests. I wanted to include this in a sort of workflow test for checking that nothing is broken.Jameljamerson
Can you update the question with what you're hoping to accomplish? I.E. what are your success criteria for this? (I am guessing "when i tap X, event X is sent to GA").Hedgehog
@quellish, exactly. I've updated my question with that.Jameljamerson
T
2

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
Therapeutics answered 13/4, 2014 at 20:26 Comment(2)
Thanks for the answer and for suggesting AnalyticsKit, it seems interesting however is lacking some documentation. Have to check their comments again later. The idea of having a UIAlertView is very similar with mine of having an hidden view and may seem more interesting and more easily achieved than mine. However I'd have to distinguish between these alerts and the ones I use to communicate failures or info to the user. Could you weigh on this?Jameljamerson
I tried to more outline how your tests would look like. HTHTherapeutics

© 2022 - 2024 — McMap. All rights reserved.