Created a library of helper methods for integrating AutoIt into a C# application.
It's a little difficult to see what's happening in this YouTube video based on their resolution - but it give you the idea that the example application brings up NotePad, enters text automatically, moves the mouse around, can read the text and then closes NotePad - all being controlled in the code.
It has a great deal of flexibility - and being embedded into C# gives us the best of both languages.
Download: The "Automator" example code and AutoItHelper library ( Visual Studio 2008 )
Recently, I had the need to perform timing / response analysis for a C# WinForms application, just as if a user were entering data through the UI.
Found the best solution to be a freeware automation scripting language named AutoIt ( http://www.autoitscript.com/ ).
With AutoIt - you can write simple text scripts to accomplish many automation tasks - but - we had to do some deserialization of some particularly nasty legacy XML, so I looked into incorporating the AutoIt SDK into a C# project to further leverage the abilities of C#.
I was able to find a couple resources that pointed me in the correct direction - but nothing that did everything that I wanted it to, so I took what I could and from there - in the words of one of my fellow architects - I had to "roll my own"...
Wanted to get this up as a resource for everyone else that needs to do similar automation - or just want to play around with an excellent automation tool library incorporated into C#.
Here is an example C# Visual Studio 2008 project with the AutoItHelper library referenced - which as the video above shows, automates Notepad.
For more details of the code "under the hood" - here is the RunNotepad() method.
1: private void RunNotepad()
2: {
3: //----------------------------------------------------------------------
4: // Wait for initial window
5: //----------------------------------------------------------------------
6: AutoItX.WinWaitActiveWindow("Untitled - Notepad");
7:
8: //----------------------------------------------------------------------
9: // Move and Resize the window
10: //----------------------------------------------------------------------
11: NextActionDelay();
12: SetStatus("Move and Resize Window...");
13: AutoItX.WinMove("Untitled - Notepad", 100, 100, 500, 400);
14: NextActionDelay();
15: AutoItX.WinMove("Untitled - Notepad", 0, 100, 600, 500);
16:
17: //----------------------------------------------------------------------
18: // Now that the Notepad window is active type some text
19: //----------------------------------------------------------------------
20: SetStatus("Typing...");
21: SendKeys("This is a test of the AUTOMATOR.{ENTER}You will be AUTOMATED.{ENTER}");
22: NextActionDelay();
23: SendKeys("+{UP 2}");
24: NextActionDelay();
25:
26: //----------------------------------------------------------------------
27: // Get the text from the window and show tooltip
28: //----------------------------------------------------------------------
29: string sText = AutoItX.WinGetText("Untitled - Notepad");
30: AutoItX.Tooltip(string.Format("Text From Window: \"{0}\"",
sText.Replace("\r", "").Replace("\n", "")));
31: AutoItX.Sleep(3000);
32:
33: //----------------------------------------------------------------------
34: // Move the mouse around
35: //----------------------------------------------------------------------
36: SetStatus("Moving the Mouse...");
37: AutoItX.MouseMove(0, 0, 50);
38: AutoItX.MouseMove(300, 300, 50);
39: AutoItX.MouseMove(600, 300, 50);
40: NextActionDelay();
41:
42: //----------------------------------------------------------------------
43: // Set message
44: //----------------------------------------------------------------------
45: SetStatus("Closing Notepad...");
46:
47: //----------------------------------------------------------------------
48: // Now quit by pressing Alt-f and then x (File menu -> Exit)
49: //----------------------------------------------------------------------
50: NextActionDelay();
51: SendKeys("!f");
52: NextActionDelay();
53: SendKeys("x");
54:
55: //----------------------------------------------------------------------
56: // Now a screen will pop up and ask to save the changes, the window is
57: // called "Notepad" and has some text "Yes" and "No"
58: //----------------------------------------------------------------------
59: AutoItX.WinWaitActiveWindow("Notepad");
60: SendKeys("n");
61:
62: //----------------------------------------------------------------------
63: // Set message
64: //----------------------------------------------------------------------
65: SetStatus("Automation Complete!");
66: }
Further looking at the AutoItHelper library - you'll see that I've tied directly into the AutoItX3.dll as follows:
1: //AU3_API long WINAPI AU3_WinWaitActive(const char *szTitle, /*[in,defaultvalue("")]*/const
2: // char *szText, /*[in,defaultvalue(0)]*/long nTimeout);
3: [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
4: private static extern int AU3_WinWaitActive([MarshalAs(UnmanagedType.LPStr)]string Title
5: , [MarshalAs(UnmanagedType.LPStr)] string Text, int Timeout);
This was an idea from one of the resources that I found online, and I extended and incorporated it.
The big advantage of using AutoItX3 like this is that you don't have to register the dll with windows and it is easily included in the project solution - downside is that it's more work on that had to be done in the AutoItHelper library - but that's already done, so no downside. :)
You'll see below, that I then wrapped all the methods - streamlining where possible - and adding extensive comments from AutoIt's CHM help file - which I included in project for future reference.
1: ///----------------------------------------------------------------------
2: /// <summary>
3: /// Pauses execution of the script until the requested window is active.
4: /// </summary>
5: /// <param name="vsTitle">The title of the window to check.</param>
6: /// <param name="vsText">[optional] The text of the window to check.</param>
7: /// <param name="iTimeout">[optional] Timeout in seconds</param>
8: /// <returns>
9: /// Success: Returns 1.
10: /// Failure: Returns 0 if timeout occurred.
11: /// </returns>
12: ///----------------------------------------------------------------------
13: public static int WinWaitActive(string vsTitle, string vsText, int iTimeout)
14: {
15: return AU3_WinWaitActive(vsTitle, vsText, iTimeout);
16: }
There are a number of functions in AutoIt that I didn't need to utilize, so there is room for future enhancements - either by myself or others.
If anyone else does extends this - please send those updates back to me and I will continue to integrate those into this library for everyone to utilize.
Good coding!
...Lance Larsen