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?
-
2See also this answer for [delphi-gui-testing-and-modal-forms](http://stackoverflow.com/a/6574035/576719). – LU RD Mar 05 '12 at 07:03
-
2How accurate a simulation do you want? Should the mouse move? Should the button appear pressed on the screen? Should the button receive messages like `wm_LButtonDown`? – Rob Kennedy Mar 05 '12 at 14:47
-
@LU RD, thanks for the pointer! I just wondered how to test modal forms. This helps. – Arnold Mar 05 '12 at 20:47
-
@Rob Kennedy, I must confess that I hadn't thought thru the problem that far. As for now the answer of David is what I needed. Supopose I needed to go further, what would be your suggestion? – Arnold Mar 05 '12 at 20:49
-
I can't really recommend anything since I don't know what your needs are. – Rob Kennedy Mar 05 '12 at 22:09
5 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.

- 601,492
- 42
- 1,072
- 1,490
-
This is exactly what I wanted. I tested it and it works, making it easier for me to test forms. Thanks a lot! – Arnold Mar 05 '12 at 20:44
Calling the OnClick
event handler won't call the Delphi's default event handler, but just the one implemented by the user. I think you should create your own class derived from TCustomButton
, and implement a function that calls the Click
method(it is protected).

- 35,493
- 19
- 190
- 259

- 82
- 5
-
5
-
Yes, just call `Click` on a `TButton` instance. No need for derivation. – David Heffernan Mar 05 '12 at 08:06
-
It is public in VCL, and it is protected in Firemonkey. – naXa stands with Ukraine Jun 28 '14 at 20:22
A correction for TSpeedButton:
The behavior described by @David Heffernan does not quite hold true for speedbuttons in a group. Calling the Click method does not seem to affect the "Down" status of the buttons.
To solve this I used the following code:
MyButton.Click;
MyButton.Down := True;

- 39
- 4
It's better to use the PerformClick()
method of the Button =>
button1.PerfomClick()
If your Button is not in the right state to click (enabled false or not visible), it will not perform the click eventmethod.

- 16,456
- 10
- 90
- 156

- 7
- 1
-
4There is not `PerformClick` in the VCL. There is a `PerformClick` in WinForms. Perhaps that's what you are referring to. – David Heffernan Mar 05 '12 at 10:00