I created a new repository on GitHub called BitCollectors.UIAutomationLib which contains an XML driven .NET library for automating keystrokes and mouse clicks on just about any UI. I’ve used this on several applications, including apps running through Terminal Services and Citrix. It’s quite powerful, but I’m not claiming it’s the best solution for native Win32 apps (although it might be the easiest to use). If you’re interfacing with a native Win32 app and you don’t mind writing a little code, you might want to look in to using Microsoft’s UI Automation framework. Their framework lets you get a handle on a control and populate a text box or simulate a button press directly on the control. My library simulates key strokes and mouse clicks, so it doesn’t really work at the control level.
Implementing the library in your code is extremely easy. All you need to do is reference BitCollectors.UIAutomationLib.dll and add the following code to your project (this example includes a macro, which is optional):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using BitCollectors.UIAutomationLib.Entities; using BitCollectors.UIAutomationLib.Helpers; using System.Collections.Specialized; ... StringDictionary macros = new StringDictionary(); macros.Add("{USERTEXT}", textBox1.Text); UIAutomation automationConfig = XmlHelper.ProcessXmlFile("C:TempMyAutomationScript.xml", macros); if (automationConfig != null) { Win32Helper win32 = new Win32Helper(); win32.ExecuteAutomation(automationConfig); } |
You can either pass in a path to an XML file or a string containing the XML itself. The XML that is used to automate the input looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?xml version="1.0" encoding="utf-8" ?> <Automation> <!-- Find the first open Notepad window --> <FormHandle TitleBarRegex="^(.*?) - Notepad$" /> <InputActions> <InputBatch Timeout="150"> <!-- Click in the window --> <MouseClick Type="Click" RelativeTo="Window" X="80" Y="80" /> </InputBatch> <InputBatch Timeout="200"> <!-- Press CTRL+A to select all text, and then press DELETE to clear out the window --> <KeyStroke Type="downup" Value="{CTRL}+A" /> <KeyStroke Type="downup" Value="{DEL}" /> </InputBatch> <InputBatch Timeout="10"> <!-- Send in the {USERTEXT} macro. This will simulate keystrokes for whatever you define {USERTEXT} to be --> <KeyStroke Type="downup" Value="{USERTEXT}" /> </InputBatch> <InputBatch Timeout="10"> <!-- Press the ENTER key to start a new line --> <KeyStroke Type="downup" Value="{ENTER}" /> </InputBatch> <InputBatch Timeout="10"> <!-- This will simulate uppercase letters vs lowercase letters by holding the SHIFT key down --> <!-- Hold down the SHIFT key and type C A P S, release the SHIFT key, type a SPACE followed by the letters L O W E R --> <KeyStroke Type="down" Value="{SHIFT}" /> <KeyStroke Type="downup" Value="C" /> <KeyStroke Type="downup" Value="A" /> <KeyStroke Type="downup" Value="P" /> <KeyStroke Type="downup" Value="S" /> <KeyStroke Type="up" Value="{SHIFT}" /> <KeyStroke Type="downup" Value=" " /> <KeyStroke Type="downup" Value="l" /> <KeyStroke Type="downup" Value="o" /> <KeyStroke Type="downup" Value="w" /> <KeyStroke Type="downup" Value="e" /> <KeyStroke Type="downup" Value="r" /> </InputBatch> </InputActions> <Messages> <Message Type="WindowHandleFailed" Value="Could not find the application running. Please make sure application is running." /> </Messages> </Automation> |
Anyone interested in contributing to this project, please drop me a line.
For more information check out the README file.
Or grab the code from the repository at: https://github.com/aplocher/BitCollectors.UIAutomationLib
One thought on “UIAutomationLib on GitHub”