iphoneiostestingsentestingkit

SenTesting for iOS programmatically press button?


So in some of the apple sample code I see things like this for application testing:

[viewController press:[calcView viewWithTag: 6]];  // 6

However when I try to do that with my own viewcontrollers/views all I am getting is "No visible interface declares press"... where is the documentation for application SenTesting on iOS, and in particular how do you go about doing UI testing (programmatically press a button, etc) within iOS?


Solution

  • Without seeing the sample code, I would guess that the view controller has a UIAction -press:. It should expect the nib to wire button presses to this action.

    Instead of simulating the event, the test directly calls the action touch event would call.


    After looking a bit, I think you may want how to programmatically fake a touch event to a UIButton?.

    First you need to get a reference to the UIButton. If it's already set as an outlet in the view controller, you can use that. So, I'll assume your viewController has an IBOutlet for that UIButton named 'button'

    [viewController.button sendActionsForControlEvents:UIControlEventTouchUpInside];
    

    For the Swift 2.0 solution

    viewController.button.sendActionsForControlEvents(.TouchUpInside)