Solved

Generate clicks for winform app

Posted on 2012-03-25
7
513 Views
Last Modified: 2012-03-27
I have a winform app (C# 4.0). i'm testing a race condition and need to click repeatedly very fast on a form panel, faster than I can click on my mouse. Is there a way to do this?

This click utility should be external to the program itself, since I'll need to run it on the current production version as well as the corrected development version. If the solution involves writing a separate program, would prefer C#.
0
Comment
Question by:BlearyEye
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 37762902
Have you heard of AutoIT? I just tested it out for your specific scenario. It would be one less thing you have to write (well, you'd only have to write a quick script for the clicking).

To simulate your example, I wrote the following in AutoIT's editor:

For $i = 0 To 10000
  ControlClick("Form1", "", "[ID:329122]")
Next

Open in new window


In my test, my form had the title (Text property) of "Form1". You would change this to exactly match the text of your form. The ID was attained by using the Window Info tool which comes with AutoIT:

Window Info Tool
Within this tool, there is a small target icon. If you've ever used Spy++, then you should have an idea of what this feature does. If you haven't, then you can actually click and drag the little target to any form on your desktop. As you mouse over a form, the border of that form will be highlighted. This will be your active target. Further, you can select specific controls from a form. Likewise to the form, a control's border will highlight when you mouse over it. When you let go of your mouse button, the currently highlighted control's information will be populated within the Window Info window.

The "Title" field is the same text as what I mentioned above for the form, so you could also use this tool to grab that value as well. If you switch to the "Control" tab, as I have in the screenshot above, you will get access to the ID property for the control you selected. This value is what you would put in the script above.

The above script is going to execute "faster than [you] can click with a mouse," and if you need to slow it down a bit, you can include a Sleep call in your script.

e.g.

For $i = 0 To 10
  ControlClick("Form1", "", "[ID:656868]")
  Sleep(1000) ' in milliseconds
Next

Open in new window


Realize that the scripting language is VB/VBA-esque. The tool has decent documentation, and since it's similar to VB, you can adapt a lot of VB code to be used within the tool. The tool itself is pretty popular, so even if you get stuck, you should be able to find help with any problems you encounter.

At least it's an option  = )
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 37762917
If you want to do it manually, then you'll need to obtain a handle the panel first.  Do you know how to that?

Assuming so, then you'd use SendMessage() to send a mouse down message (and possibly a follow-up mouse up message depending on what you're doing):

        private const int WM_LBUTTONDOWN = 0x201;

        [DllImport("User32.DLL")]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private void button1_Click(object sender, EventArgs e)
        {
            SendMessage(panel1.Handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
        }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37762934
For Idle_Mind's method, I believe you would also need FindWindow (maybe FindWindowEx) since you mention the click utility's "external" requirement.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 40
ID: 37763553
If this apply to your situation, typically if no other mouse events are involved, you can call the click event from inside the application itself.

Simply add a button or menu on the form to start your test, something that is visible only by an administrator or through a back door if you do not want the users to see it.

Prepare a loop, maybe with a short Sleep instruction to be closer to a reall life situation, and call your event handler in every loop:

Button1_Click (null, null);
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 37771808
Am checking into AutoIT ... JamesBurger: can't add a button to the app since I need to test a deployed version.
0
 
LVL 40
ID: 37771852
As I told you, the button can be hidden by a backdoor so that only you have access to it for your tests.

A nice thing about that feature is that you can still test at the users station after the application is deployed if you find problems on some of the computers. The user will never see the button. And a hidden button or menu with the simple code that the test would require does not add a significant overhead to the application.
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 37775187
After a bit of effort, AutoIT did the trick. Nice tool. I had to use MouseClick rather than ControlClick, for some reason that I did not quite divine; the control in question has a MouseDown event that changes the focus, as well as a Click event.

Idle_Mind's solution would also have worked, tho it would have required a C# app which is a bit more complex.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not showing page correctly 3 29
Entity Framework 7 27
C#.NET and microsoft certification. 3 25
Where to download and how to install sqldmo.dll 5 27
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now