Fun with UIAutomation and calc
Last night I got a cool idea how to show some basic ideas of UIAutomation. I did decide to-do a UIAutomation example with calc as my test subject. The idea is to give a mathematical expression to the program, which will then use calc to solve the problem and show you the result. The main thing is to show how UIAutomation is working and not to calculate.
The first thing that we need is a method that can find an AutomationElement for us. An AutomationElement is anything that you can see on the screen as buttons and input fields. The method take a root AutomationElement that define where to start to search, a name and a class name for the object that we are looking for. We then create two property conditions based on the name and class name. These two conditions are then combined in an AndCondition. We also have OrCondition and NotCondition to use. The last thing we do is to search for the first AutomationElement that match the condition.
private static AutomationElement GetElement(AutomationElement root, string name, string className) { PropertyCondition condName = new PropertyCondition(AutomationElement.NameProperty, name); PropertyCondition condClassName = new PropertyCondition(AutomationElement.ClassNameProperty, className); AndCondition cond = new AndCondition(condName, condClassName); return root.FindFirst(TreeScope.Descendants, cond); } |
Next method that we need is one that can return a invoke pattern. Invoke pattern is used for example to click on a button. This will be used to be able to click on the different buttons in calc
private static InvokePattern GetInvokePattern(AutomationElement element) { return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; } |
The last method that we will use is a method that will return the value for an example a text field.
private static string GetValue(AutomationElement element) { ValuePattern value = element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; return value.Current.Value; } |
So for our program we first need to-do is to start calc and create an AutomationElement for the program.
Process proc = new Process(); proc.StartInfo.FileName = "calc"; proc.Start(); proc.WaitForInputIdle(); AutomationElement calc = AutomationElement.FromHandle(proc.MainWindowHandle); |
The next thing is to search for all elements that we are going to use.
AutomationElement edit = GetElement(calc, "", "Edit"); AutomationElement plus = GetElement(calc, "+", "Button"); AutomationElement minus = GetElement(calc, "-", "Button"); AutomationElement div = GetElement(calc, "/", "Button"); AutomationElement mult = GetElement(calc, "*", "Button"); AutomationElement egual = GetElement(calc, "=", "Button"); Collection numberButtons = new Collection(); for (int i = 0; i < 10; i++) { numberButtons.Insert(i, GetElement(calc, i.ToString(), "Button")); } |
After that we loop through the input data and press on the buttons according to it. For example plus button GetInvokePattern(plus).Invoke().
The last thing we do is to press the equal button and read from calc the result of our calculation.
GetInvokePattern(egual).Invoke(); ValuePattern editValuePattern = edit.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; Console.WriteLine(argument + "=" + editValuePattern.Current.Value); proc.Kill(); |
Comnplete solution in VS 2008: Calculator.zip
I hope this have been giving you some ideas what is possible to-do with UIAutomation. Feel free to modifiy and use the code.
Comments
2 Responses to “Fun with UIAutomation and calc”Trackbacks
Check out what others are saying about this post...[…] week, I wrote about UIAutomation and calc. Now it’s time to revisit or little example program and replace the automatic start of calc and […]
[…] my earlier blogs (Fun with UIAutomation and calc, Fun with UIAutomation and calc 2 (event handling)) I have been talking about how to user […]