We can automate Windows Presentation Foundation (WPF) applications using QTP with WPF-Addin. But is it possible to automate without addin?
Yes, it is possible. Microsoft launched System.Windows.Automation class with .Net 3.0 Framework. This is also called UI Automation class in .Net. Using this class we can automate WPF applications.We will see how to automate a WPF application using UI Automation class and QTP DOT Net Factory in the forthcoming articles.
First, we start by clicking a WPF button. I have used the below ExpenseIt Standalone WPF application for this implementation.
Code
'WPF_ClickButton function with two arguments 'Argument 1: inWindowHwnd - Window Handle 'Argument 2: strButtonName - Button Name (In this example, the button name is ‘Create Expense Report’) Function WPF_ClickButton(ByVal inWindowHwnd,ByVal strButtonName) Dim oTreeScope, oAutomationElement Dim oInvokePattern, oInptr, oPropertyCondition 'TreeScope object to specify scope of elements Set oTreeScope=DotNetFactory.CreateInstance("System.Windows.Automation.TreeScope","UIAutomationTypes") 'AutomationElement object is used to retrieve the element Set oAutomationElement=DotNetFactory.CreateInstance("System.Windows.Automation.AutomationElement","UIAutomationClient") 'PropertyCondition object to search the button with Name property, here it takes strButtonName argument as second constructor. Set oPropertyCondition=DotNetFactory.CreateInstance(" System.Windows.Automation.PropertyCondition","UIAutomationClient",oAutomationElement.NameProperty,strButtonName) 'Since Button control comes under InvokePattern, we need this object to invoke a button. Set oInvokePattern=DotNetFactory.CreateInstance("System.Windows.Automation.InvokePattern","UIAutomationClient") 'In .Net, Window Handle cannot be taken as Integer or String; it should be passed as IntPtr structure. That is why here inWindowHwnd argument is supplied as constructor for IntPtr object. Set oInptr=DotNetFactory.CreateInstance("System.IntPtr","",inWindowHwnd) 'Getting the WPF Form using FromHandle method. Set oAutomationElement=oAutomationElement.FromHandle(oInptr) 'Searching the Button with Name and assigning into oInvokePattern object Set oInvokePattern=oAutomationElement.FindAll(oTreeScope.Children,oPropertyCondition)(0).GetCurrentPattern(oInvokePattern.Pattern) 'Clicking the button with Invoke button oInvokePattern.Invoke() 'Killing the objects Set oTreeScope=Nothing Set oAutomationElement=Nothing Set oPropertyCondition=Nothing Set oInvokePattern=Nothing Set oInptr=Nothing End Function Dim inWindowHandle 'Getting Window handle property using GetRoProperty inWindowHandle=Window("text:=ExpenseIt Standalone").GetRoProperty("hwnd") 'Call WPF_ClickButton function with Window Handle and Button name WPF_ClickButton inWindowHandle,"Create Expense Report"
Synopsis
From this article you have learnt how to click a WPF button. Using Microsoft UI Automation API, we can automate WPF applications.
Note: This implementation can be done from .Net 3.0 Framework onward.
Comments(0)