Simulate a Click
Small snippet to kinda simulate a click on a control
static public class ControlExtensions
{
static public void SimulateClick(this Control control)
{
if (control != null)
{
MethodInfo method = typeof(Control).GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
method.Invoke(control, new object[] { EventArgs.Empty });
}
}
}
Does the job for me – so far.
Advertisement
This is actually good for doing automated tests. Don’t you know if there is a way in webforms?
ivowiblo
February 18, 2010 at 11:15 am
yes, it may is. do you ask for way to automate tests on webforms?
esskar
February 18, 2010 at 11:20 am
I mean, if webforms has a kind of OnClick method to call.
ivowiblo
February 18, 2010 at 6:51 pm
Well, oviously it has (it’s in Control), but it’s not public. But one could write a wrapper around the Control/Form classes to call those On{Action} functions via Reflection calls.
esskar
February 19, 2010 at 6:51 am
Yeah, that’s what you’ve done with winforms.
ivowiblo
February 19, 2010 at 1:23 pm