Analysis of automated UI Testing

Source: Internet
Author: User

---How to build your own lightweightUITest Tool

Note: This article is only for discussion and mainly serves as an example. I hope you can correct the mistakes.

Currently, there are many popular automated testing tools, such as wr, qtp, robot, robotj, TC, and rft, which may not be available at once and have powerful functions, have you ever thought about a test tool of your own? It should be interesting.
Starting from the basic principles of such testing tools, we will show you how the automated testing tool works, finally, we will discuss how to build our own testing tools.

IPrinciples of automated testing tools

An automated testing tool should have three modules: Recording, playback, and Object Library. These three modules support the entire automated testing tool. We will discuss the three modules respectively:

Download(12.74 KB)

Recorded)
Recording is to record the operation process. There are three main points to be recorded in this process: mouse events, keyboard events, and objects in the event response process. An object is a form (in windows, a control is a special form, so this article does not distinguish between forms and controls, all called forms ), these objects will be stored in a place called the object library. Of course, attributes related to the objects will also be recorded, such as class, caption, and enabled, and the objects will be stored in the object library together, and assign a unique ID for identification.
The technology used in this process is something called "hook". After you click recording, the test tool loads related hooks, such as mouse hooks, keyboard hooks are the most common two types. Windows hook functions can be considered as one of the main features of windows. They allow you to capture events that occur in your own processes or other processes. With "hook-up", you can give Windows a callback function to process or filter events. This function is also called "hook function". When an event you are interested in occurs, windows will call this function. There are two types of hooks: local and remote. Local hooks only hook events of your own processes. Remote hooks can also hook events that occur in other processes. When you create a hook, Windows will first create a data structure in the memory, which contains information about the hook, then add the struct to an existing hook linked list. The new hook is added to the front of the old one. Note that if it is a remote Hook, the system must insert the hook function into the address space of other processes. To do so, the hook function must be in a dynamic link library, if you want to use a remote Hook, you must put the hook function in the dynamic link library.

Playback)
Let's start with a simple example. When using the test tool, we can see that all operations are automated. How does one click a button in the system? In Windows, each form is uniquely identified as a handle (the form handle involved in this article). The handle is a 32-bit unsigned integer corresponding to the object. An object can be mapped to a unique handle or a unique object. Among the many windows APIs, how does one obtain the coordinates of the form related to the handle through the handle, and move the mouse to this position, after a mouse click event is generated, the playback of one click is completed. How does one get the handle? It is good to get the handle in the playback process, which is the most important thing to mention the object library and find the correct form through the relevant attributes of each object recorded in the object library, this is what needs to be done in the recording process (or in the process of adding an object.

Object Map)
The object library is a data structure defined by the test tool developer. Each form is saved as an object in the object library, and the object contains many attributes. The purpose is to "reproduce" the operating environment by telling the program what controls should be operated during playback.

Second, do your own testing tools

Next, let's take a look at how to use our function regression testing tool. The design framework is as follows:

Download(11.24 KB)

This framework is roughly divided into four parts: spy ++ is used to manage, add, delete, and modify objects. Object map is a custom pseudo object library, and run the operation process, the multi-thread running mechanism should be used for the running process, which is roughly three threads:
1)
Responsible for running the process.
2)
Process the pop-up of an unexpected form during running.
3)
Records logs and generates reports

The next thing to do is to first define the "script" structure of the Object Library and runtime. Just take a few attributes. The object library is defined as a win. OBJ file. The structure is as follows:

Download(78.02 KB)

 

It is actually an XML file, where the ID is used to uniquely identify this object, so that it is not found incorrect or similar forms during playback. In this way, you can use setjavaswhookex to load the mouse hook and keyboard hook to record the operation process during the recording process. For example, you can know when the mouse generates an event in the mouse hook, such as one click, then, you can click the lparam parameter of the message to retrieve the coordinates clicked by the mouse. Then, you can use getwindowfrompoint to get the handle of the form under the STOP coordinate, and then use getclassname, getwindowtext, getwindowrect and other related functions get the corresponding attribute values and store them in the object library.

How to play back? Playback requires a script like a commercial test tool to be used for playback. This is complicated and the simplest method is used, you can also customize a "script" to implement the playback function. The script file is run. script. The format is as follows:

Download(65.56 KB)

In fact, it is still XML. In this file, specify the object library required for running. Each run represents an action, and the ID corresponds to the run in the object library, that is, at runtime, first, use the ID in the object library for matching. After matching, take out the relevant attributes of the object, such as class and caption, and then use findwindow and find1_wex to obtain the form handle with the same attributes.

After the above definition, we began to design the class design in the Code, as follows:

Download(24.34 KB)

It can be divided into four main categories. guitesobj encapsulates some common object operations, such as clicking, double-clicking, and closing the activation form, to inherit from it to complete operations on various forms, wininfo stores the relevant properties of objects. buildmap encapsulates the process of generating object libraries, and autorun runs the process.

 

The following points must be explained during code writing:

1) Positioning form

 

You cannot directly use Windows APIs. For example, the findwindow function completes the operations on the form in an instant. However, commercial test tools can search for a form for a period of time, therefore, the following encapsulation can be performed:

 


Download(11.24 KB)

The method for searching second-level forms is also encapsulated as above. Another important question is what if there are two identical forms on the interface? There is still no caption. For example, what is the difference between the user name and password input box on the login interface? In Windows, each form is constructed in order for all the forms it contains. This order is called index, and the index of the form itself is always 0, the first constructed form object is 1, which is sorted in sequence.

Download(11.7 KB)

2) Operation

After obtaining a form handle, you can perform related operations on it. Specifically, you can obtain the coordinates of the form through the handle, and then move the mouse to this position, A mouse event, keyboard event, or other operations occur. Take Mouse clicking as an example:

Download(21.76 KB)

Note that a setfocus is used at the beginning of the function to activate the target form and then perform event operations to prevent the target form from being blocked by other forms.


3) generate an object library

The process of generating an object library is to record the properties of a form that has been operated into the object library according to the previously defined format after loading the "hook, the format of the Object Library is a data type in a tree structure. It is written to the object library completely according to the hierarchical relationship of the form object. Each object must have a unique ID for identification, to prevent object location errors during playback. Functions needed here include enumwindowsproc, enumchildproc, and getwindow and functions used to obtain object attributes: getclassname, getwindowtext, and getwindowrect.

4) run the "script"

When parsing our own script structure, we need to consider the Operation Cohesion, correct matching of the Object ID in the two files, and control playback, because this process is the most important hub of the entire tool, it is responsible for connecting everything. This part of the code is written into the autorun class.

5) fault tolerance handling

During the script playback process, there are usually abnormal pop-up forms or the target object is not displayed. There may be many unexpected events that affect the correctness and robustness of the script playback, because the running process starts another thread for fault tolerance Processing, and some abnormal forms are usually closed to prevent script interruption.

After all the above work is done, call the following in the main function:

Download(5.98 KB)


Add this sentence:
# Pragma comment (linker, "/subsystem:/" Windows/"/entry:/" maincrtstartup /"")
Hide the process (it looks more professional ^_^ ).

All the work has been done here. Let's review the entire process of using the tool, add the object to the object library, and then edit the "script" as shown in the "script" diagram attached above, in this example, the Windows Calculator is operated to complete 1 + 2 +... + 9 + 10 calculation, and then write the string "Running ended" to the output box of the calculator to mark the completion of the operation. The result will be displayed immediately after the operation is completed, follow the result of running the script as shown in the following figure:

Download(16.55 KB)

Download(17.82 KB)

The previous one is after running to click 7 and +, and the later one is after running. This is the entire process of clicking, and other operations are similar. The only difference is the script editing interface, which provides a simple function regression testing tool.







[Postscript]: This article briefly discusses the implementation principle of automated testing tools and the establishment of a lightweight automated testing tool that cannot be simplified, it is far from enough to develop a testing tool that can be used. Therefore, we hope that this article will give you a different understanding of popular commercial tools, better understanding, understanding, and use of these tools to serve daily work.


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.