¿cómo falsificar programáticamente un evento táctil a un UIButton?


Estoy escribiendo algunas pruebas unitarias y, debido a la naturaleza de esta aplicación en particular, es importante que consiga lo más alto posible en la cadena UI. Por lo tanto, lo que me gustaría hacer es activar programáticamente una pulsación de botón, como si el usuario hubiera presionado el botón en el GUI.

(Yes, yes I 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, sí mismo.)

¿Cuál es el método preferido para hacer esto?

Author: Rishil P., 2010-10-27

6 answers

Resulta que

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

Me dieron exactamente lo que necesitaba, en este caso.

EDIT: No se olvide de hacer esto en el hilo principal, para obtener resultados similares a una prensa de usuario.


Para Swift 3:

buttonObj.sendActions(for: .touchUpInside)

 419
Author: Olie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-30 22:07:56

Una actualización de esta respuesta para Swift

buttonObj.sendActionsForControlEvents(.TouchUpInside)

EDITAR: Actualizado para Swift 3

buttonObj.sendActions(for: .touchUpInside)
 47
Author: Hellojeffy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-07 15:32:46

Si quieres hacer este tipo de pruebas, te encantará la compatibilidad con UI Automation en iOS 4. Puede escribir JavaScript para simular pulsaciones de botones, etc. con bastante facilidad, aunque la documentación (especialmente la parte inicial) es un poco escasa.

 6
Author: Jeff Kelley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-27 00:27:03

En este caso, UIButton se deriva de UIControl. Esto funciona para el objeto derivado de UIControl.

Quería reutilizar la acción "UIBarButtonItem" en un caso de uso específico. Aquí, UIBarButtonItem no ofrece método sendActionsForControlEvents:

Pero afortunadamente, UIBarButtonItem tiene propiedades para el objetivo y la acción.

 if(notHappy){        
         SEL exit = self.navigationItem.rightBarButtonItem.action;
         id  world = self.navigationItem.rightBarButtonItem.target;
         [world performSelector:exit];
 }

Aquí, rightBarButtonItem es del tipo UIBarButtonItem.

 5
Author: Hitesh Savaliya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-17 12:56:56

Para Xamarin iOS

btnObj.SendActionForControlEvents(UIControlEvent.TouchUpInside);

Referencia

 3
Author: Durai Amuthan.H,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 10:31:31

Swift 3:

self.btn.sendActions(for: .touchUpInside)
 2
Author: Gobi M,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-15 06:41:28