"Video tutorial" using UIAutomation development software plug-in

Source: Internet
Author: User
Tags win32 window

UIAutomation is the "Interface Automation test" technology that was provided after. Net 3.5, which was originally intended for testers, but UIAutomation is also a technology for automatic interface operation, which is more than the direct use of keybd_event, GetWindowText and other Win32 API interface simulation operation is much simpler, so you can also use uiautomation to do software "plug-in."

I happen to have such a demand, such as Peng Network has an internal use of a tool (purchased third-party software) to calculate the "play password" based on the student's machine code, this tool only provides a graphical interface:

After entering the machine code, click the "Create Play password" button to generate the play password.

such as the second phase of the development of the learning assistance system needs to develop the "automatic generation of Play password" function, that is, students in the browser input his machine code, the website automatically calculate his play password.

Since this tool only provides a graphical interface, does not provide the API, so I think of using analog click Method to "Automation", the direct use of Win32 is too troublesome, autoit use also to register components, so I think of uiautomation.

The finished results are as follows:

Share the main technologies below.

Before you learn uiautomation, be sure that the program interface elements in Windows are composed of "Windows" (except for directui, etc.), that buttons, text boxes, and so on are windows, and that there is a parent-child relationship between windows. Windows desktop is the root window of all windows.

UIAutomation Support Common Win32 program (not VC + +,. NET development is also supported, because it is essentially a WIN32 program) and a WPF program, but does not support ordinary directui windows (such as QQ, browser).

Add a reference to the Uiautomationclient, Uiautomationprovider, uiautomationtypes three assemblies before using UIAutomation. All interface elements are composed of automationelement, each of which is a automationelement, so there is a parent-child structure before automationelement.

You can use Automationelement.rootelement to get the root element of the desktop; use Automationelement.fromhandle (INTPTR hwnd) Gets the AutomationElement object from the Win32 window handle.

Getting a automationelement usually goes through his child elements. It is necessary to understand the concept of "traversal condition" before traversing the child elements, and the traversal condition is to search for the child elements according to what conditions. All the conditions inherit from the condition class, and the main subclasses of the condition class are propertycondition, Andcondition, Notcondition, orcondition, which can be combined in a composite To form a variety of complex traversal conditions.

PropertyCondition is filtered based on the name and value of the attribute. The first parameter of the constructor is the name of the property, and all the supported properties are in the static members of the AutomationElement ***property; the second parameter of the constructor is the value being compared. You can also use Andcondition, notcondition, and orcondition to make complex logical combinations of various conditions. For example, the following conditionBtn9 is the "class named Button and the name is 9" condition:

Condition conditionBtn9 = new Andcondition (                new PropertyCondition (Automationelement.classnameproperty, "button") ,                new PropertyCondition (Automationelement.nameproperty, "9")                );

  

The Condition class has a fixed value of two, Condition. Truecondition represents a condition that is always true, Condition. Falsecondition represents a condition that is always false (should seldom be used)

We can use AutomationElement's FindAll or FindFirst method to iterate through the elements. FindAll is to get all automationelement that conform to the traversal condition, so it is the return automationelementcollection collection, Instead, FindFirst returns the first automationelement that conforms to the traversal condition, and therefore returns AutomationElement.

The first parameter of FindFirst, FindAll, represents the scope of the search, The most common is that treescope.children and treescope.descendants,treescope.children are searched for in the direct child nodes, while the treescope.descendants represents recursion in all descendant nodes. The second parameter of FindFirst, FindAll, represents the search condition.

After you navigate to the automationelement you want to manipulate, you can simulate clicks (such as buttons) or read and write values (such as input boxes). For example, in the following code, element points to a button, the following code is the analog click on this button:

var Clickpattern = (Invokepattern) element. Getcurrentpattern (Invokepattern.pattern); Clickpattern.invoke ();

  

For example, in the following code, element points to a text box, and the following code fills the input box with a string:

ValuePattern ValuePattern = (valuepattern) element. Getcurrentpattern (Valuepattern.pattern); Valuepattern.setvalue ("such as Peng Network");

  

Here is an analog click Calculator to calculate the number of two-digit multiplication:

AutomationElement Desktop = Automationelement.rootelement;var calcFrame1 = Desktop. FindFirst (treescope.children,new propertycondition (Automationelement.classnameproperty, "calcframe")); Clickcalcbutton (CalcFrame1, "3"); Clickcalcbutton (calcFrame1, "6"); Clickcalcbutton (CalcFrame1, "5"); Clickcalcbutton (calcFrame1, "*"); Clickcalcbutton (calcFrame1, "1"); Clickcalcbutton (CalcFrame1, "2"); Clickcalcbutton (calcFrame1, "=");

Where Clickcalcbutton is a method of my encapsulation:

private static void Invokebutton (AutomationElement e) {    Invokepattern invoke = (Invokepattern) E.getcurrentpattern ( Invokepattern.pattern);    Invoke. Invoke ();} private static void Clickcalcbutton (AutomationElement calcFrame1, string name) {    Condition conditionbtnplus = new Andcondition (       new PropertyCondition (Automationelement.classnameproperty, "button"),       new propertycondition (Automationelement.nameproperty, name)       );    var btn = Calcframe1.findfirst (treescope.descendants, conditionbtnplus);    if (BTN = = null)    {throw new Exception ("Cannot find the calculator button named" +name+ ");    }    Invokebutton (BTN);}

  

Article space is limited, especially for some no Win32 foundation of friends, light look above the text will not be easy to understand, so I recorded a set of about 90 video tutorial, from the introduction to the practical explanation of the use of uiautomation, interested friends can see.

The video tutorial address is as follows http://www.rupeng.com/Courses/Chapter/298

"Video tutorial" using UIAutomation development software plug-in

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.