KIFTestStep for UIAlerts? GPS Notification enabling?

1,580 views
Skip to first unread message

James Nguyen

unread,
Feb 2, 2012, 2:44:48 PM2/2/12
to KIF iOS Automated Testing Framework
Hi--I'm testdriving KIF and I'm able to simulate tests with
Accessibility Labels, but how do you handle touches for UIAlerts,
since they don't have labels? Should I do :
stepToWaitfortimeInterval, and then do a +
(id)stepToTapScreenAtPoint?

I looked in the KIFTestStep class and didn't see a method that handled
alerts so wondering how the community handles this....Thanks!

Jim Puls

unread,
Feb 3, 2012, 2:57:05 PM2/3/12
to kif-fr...@googlegroups.com


This is a bit orthogonal to your question, but something we do when running KIF in the simulator is swizzle a few mocked-out methods on to CLLocationManager so that the alerts never appear and so that we get something meaningful or at least reliable back from Location Services. This may be useful.

-> jp

Drew Crawford

unread,
Feb 3, 2012, 3:05:07 PM2/3/12
to kif-fr...@googlegroups.com
Just a minor correction, although UIAlertViews in general have accessibility labels, Apple is doing something strange with the particular UIAlertViews that display the location permission prompt.  I believe it is a security thing (they don't want an app programmatically pushing "OK" on that dialog), and so there is some alternate view hierarchy that you literally cannot get access to from application code.

On the particular app I was working in, testing location as such was unimportant, and so I just disabled that stuff with TARGET_IPHONE_SIMULATOR.  On a project where this wasn't a feasible solution I might look into plist hacking the simulator stuff for CI (or you can obviously just manually click OK if you are running KIF locally).  Or, if you actually need to test particular lat/lon coordinates anyway, you could fully mock CLLocationManager.

Drew

Jim Puls

unread,
Jul 13, 2012, 3:50:41 AM7/13/12
to kif-fr...@googlegroups.com
It's really straightforward:

        [CLLocationManager swizzleInstanceSelector:@selector(startUpdatingLocationFake) toSelector:@selector(startUpdatingLocation)];
        [CLLocationManager swizzleInstanceSelector:@selector(locationFake) toSelector:@selector(location)];
        
        // One for class, one for (deprecated) instance method 
        [CLLocationManager swizzleInstanceSelector:@selector(locationServicesEnabledFake) toSelector:@selector(locationServicesEnabled)];
        [CLLocationManager swizzleClassSelector:@selector(locationServicesEnabledFake) toSelector:@selector(locationServicesEnabled)];

We just swizzle out a few instance and class methods on CLLocationManager right before the call to UIApplicationMain. The implementation of the swizzling is the simplest one that could possibly work:

+ (void)swizzleInstanceSelector:(SEL)firstSelector toSelector:(SEL)secondSelector;
{
    Method swizzleMethod = class_getInstanceMethod(self, firstSelector);
    Method method = class_getInstanceMethod(self, secondSelector);
    method_exchangeImplementations(method, swizzleMethod);
}

+ (void)swizzleClassSelector:(SEL)firstSelector toSelector:(SEL)secondSelector;
{
    Method swizzleMethod = class_getClassMethod(self, firstSelector);
    Method method = class_getClassMethod(self, secondSelector);
    method_exchangeImplementations(method, swizzleMethod);
}

Of course, as a reminder, don't do this in any code you're shipping. To quote our README:
KIF uses undocumented Apple APIs. This is true of most iOS testing frameworks, and is safe for testing purposes, but it's important that KIF does not make it into production code, as it will get your app submission denied by Apple. Follow the instructions below to ensure that KIF is configured correctly for your project.

-> jp

On Friday, July 13, 2012 at 12:38 AM, Karl Krukow wrote:

On Friday, February 3, 2012 8:57:05 PM UTC+1, Jim Puls wrote:

This is a bit orthogonal to your question, but something we do when running KIF in the simulator is swizzle a few mocked-out methods on to CLLocationManager so that the alerts never appear and so that we get something meaningful or at least reliable back from Location Services. This may be useful

Hi Jim,
Is it possible for you to share what you did? I'm looking for something similar - so I'd like to start with what you did.


- Karl 

Karl Krukow

unread,
Jul 13, 2012, 3:55:33 AM7/13/12
to kif-fr...@googlegroups.com
Thanks, Jim. Is it possible for me to see the *Fake method implementations too?

Does this approach work for MapView with user location tracking (i.e. is MKMapView calling out to CLLocationManager?)

view

unread,
Aug 26, 2012, 8:05:36 AM8/26/12
to kif-fr...@googlegroups.com
Hi, Jim:
     Would you please show us the example implementation of "startUpdatingLocationFake"?

Thanks a lot~

Youssef Francis

unread,
Dec 7, 2013, 11:03:49 PM12/7/13
to kif-fr...@googlegroups.com, jng...@phunware.com
I've found a solution that does not require swizzling, bypasses all of these alerts, and actually provides the test environment with access to location services, photos, contacts and the calendar, which I've outlined here:

Reply all
Reply to author
Forward
0 new messages