views:

48

answers:

2

I'm writing some unit tests and, because of the nature of this particular app, it's important that I get as high up the UI chain as possible. So, what I'd like to do is programmatically trigger a button-press, as if the user had pressed the button in the GUI.

(Yes, yes -- I could just call the IBAction selector but, again, the nature of this particular app makes it important that I fake the actual button press, such that the IBAction be called from the button, itself.)

What's the preferred method of doing this?

+3  A: 

If you want to do this kind of testing, you’ll love the UI Automation support in iOS 4. You can write JavaScript to simulate button presses, etc. fairly easily, though the documentation (especially the getting-started part) is a bit sparse.

Jeff Kelley
Yeah, that's cool stuff, but I'm trying to do it from within the app, not via Instruments (if that's the method you mean.)
Olie
Instruments runs with your app. The app is still running, Instruments just simulates the touching.
Jeff Kelley
I understand Instruments, Jeff -- what I'm saying is: I want my unit-tests to run in the device in my pocket, without a laptop or XCode or Instruments handy. Or do you know the secret of running instruments on the device? ;)
Olie
+2  A: 

It turns out that

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

got me exactly what I needed, in this case.

Olie
Which is no different to calling `[myController myActionMethod:myButton1];`
Jasarien