Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simulate the press of a button?

I want to test some forms. Is there a way to simulate the press of an Ok (or Cancel) button so that the button is pressed and fires the event handlers that are associated with it?

like image 596
Arnold Avatar asked Mar 05 '12 06:03

Arnold


2 Answers

The cleanest approach is to call the Click method of the button. This is better than the alternatives for these reasons:

  • You could read the OnClick property, check that it was not nil, and then call the method. But that seems rather pointless since the Click method already does just that. There's no point duplicating this code.
  • And you could call the event handler directly, but that would require your code to have knowledge of it. That implies an undesirable level of coupling to the implementation details.
  • Calling Click replicates what actually happens when the user clicks. It is what happens when the user presses the button. It deals with any actions that are associated with the button, for example. It sets the forms ModalResult property. And so on.
like image 99
David Heffernan Avatar answered Oct 24 '22 06:10

David Heffernan


btn_ok.click or btn_okClick(sender);

like image 22
bejarun Avatar answered Oct 24 '22 04:10

bejarun