Let's describe how to use AutoIt to complete the automated testing of a standalone program. Using AutoIt to complete the automated testing of the desktop application, the most important thing is to find a way to identify the GUI object, and then call the AutoIt function to manipulate it or read its property values and compare it to the correct result. AutoIt has built-in a number of ways to manipulate Windows standard controls to meet the testing needs of general applications . We can also extend the functionality of AutoIt by invoking a dynamic library or COM extension.
1. How to identify Windows
One of the first things to solve when writing an automated script is how to identify the target window accurately (in many windows). In general, we often use the title of the window as its identification method. But sometimes it is not enough to give the window title, but also to give the title and file (text). It is easy to get the title of a window (most Windows can be seen directly) and can be crawled using the window information tool provided by AUTOIT (AutoIt window info) . The title of most Windows is obvious, such as the system's own Notepad program (Notedad.exe), and its default title is "Untitled-notepad". The window caption and its text are case sensitive, including punctuation, and we have to make sure that they match exactly.
Most of AutoIt's window functions have window captions and text parameters, such as the Winwaitactive function below. The function is to suspend execution of the script and wait until the specified window appears and activates.
Winwaitactive ("window caption", ["Window text"],[time-out])
where window caption is a parameter that must be specified, window text and time-out are optional parameters. However, some functions of the window text parameters must be specified, if you want to omit this parameter, you only need to specify an empty string ("") as a parameter. Specifying an empty string in the Parameters window text does not even have a value (NULL), which is equivalent to telling AutoIt that any text is valid.
The following is an example of a Notepad window that illustrates two uses of the functions mentioned above:
Winwaitactive ("Untitled-Notepad") or Winwaitactive ("Untitled-Notepad", "" ")
2. Window handle
Variables in AutoIt can be used to store window handles (Windows Handles). The so-called window handle refers to the special value that Windows automatically assigns to each newly created window. Window handles can be used instead of window caption parameters. The advantage of using a window handle instead of a window caption is to be able to identify the window more precisely, for example, sometimes we open multiple copies of the same application at the same time, which have exactly the same window caption and window text, and can use the uniqueness of the window handle to accurately identify the specified window.
Many functions, such as Wingethandle, Winlist, and Guicreate, will return a window handle, as an example:
$handle =wingethandle ("Untitled-Notepad") Winclose ($handle)
Note : The window handle is always available regardless of whether the current Wintitlematchmode is set to a mode .
3. Methods for identifying controls
AutoIt provides the ability to manipulate controls directly. What you can see on the window is mostly one of the following controls: Buttons, list boxes, text edit boxes, static text, and so on. For example, the main window of the Notepad program that comes with the system is just a relatively large edit box control. Because AutoIt provides the ability to manipulate controls directly, we no longer have to use low-level methods such as analog keystrokes to manipulate windows, which makes it more reliable to implement scripts that operate on Windows.
AutoIt primarily supports standard microosft controls. Some applications use a large number of custom controls, much like a standard Ms control, but cannot be identified by a script program, which requires a special solution.
in Use control ... () function, some control descriptions must provide ControlID. The controls can be correctly identified by these descriptions. These descriptions include:
- ID, ID of the internal control;
- text, control texts, such as the "Next" button;
- class, the name of the inner control's classes, such as "Edit" or "button";
- INSTANCE, enumeration;
- classnn, category name, such as "Edit1";
The properties above can be used alone or in combination. The specific type of attribute used is based on what kind of information you like and what you get from the AutoIt window Information tool. In general, the best way to do this is to use the control ID, but if the control ID is not available or the control ID is not sufficient to ensure that the target control is recognized, then you need to use a different property, or a combination of properties.
For example, send the text to the 1th edit control in Notepad:
Controlsend ("Untitled-Notepad", "", "[Class:edit;instance:1]", "This is some text")
Or
Controlsend ("Untitled-Notepad", "", "[Classnn:edit1]", "This is some text")
Or
Controlsend ("Untitled-Notepad", "", "Edit1", "This is some text")
By clicking on the control in my window and getting the control ID 254, you can use the ID directly:
Controlclick ("My Window", "" "," [id:254] ")
Or
Controlclick ("My Window", "", 254)
For example, if you click the 2nd button that contains the finish text, you use the Combine method:
Controlclick ("My Window", "", "[Class:button; TEXT: "Done"; Instance:2] ")
If you want to get a handle to a control, you can use the Controlgethandle function. A control handle is a unique identifier that Windows assigns to a control, that is, each created control has a different handle. Examples are as follows:
$handle =controlgethandle ("Untitled-notepad", "" "," Edit1 ")
4. Manipulating Windows and controls
After identifying the window and control methods, we can use the functions provided by AutoIt to complete the operation of the windows and controls. The usual functions are as follows.
Activates the specified window (which sets the focus to the window, making it the active window) winactive (window caption [, window text]) closes the specified window winclose (window caption [, window text]) moves the specified window or resizes the window Winmove (" Window caption "," window text "],x coordinate, y coordinate [, width, [, height [, speed]]]) to display, hide, minimize, maximize, or restore a window winsetstate (Window caption, window text, flag) Sends a mouse click command to the specified control: Controlclick ("window caption", "window text", Control id[, key [, click Count [, x coordinate [, y]]]) to send a command to the specified control: Controlcommand (Window caption, window text, The control ID, command [, Options]) sets the input focus to a control on the specified window: Controlfocus (Window caption, window text, control ID) sends a string to the specified control: controlsend (Window caption, window text, control ID, "String" [, Flag]) modifies the text of the specified control: Controlsettext ("window caption", "window text", Control ID, "new text", flag) sends a simulated keystroke action to the activation window: Send ("key" [, Flag]) Perform mouse click action: MouseClick ("button" [, x-coordinate, y-coordinate [, click [, Speed]]) to perform mouse drag: Mouseclickdrag ("button", X1 coordinate, Y1 coordinate, X2 coordinate, Y2 coordinate [, speed])
5. Verification
After completing the procedure of the test case, the black box test method mainly verifies the results of the test by checking and comparing the output of the software (including interface, file, data, etc.) to determine whether the software is defective. Software interface inspection is mainly to check the Software window and control of various status, title, text and pictures and other information, and to write the results of the final test report, for analysis.
5.1 Common methods for validating window, control state
Gets the coordinate position and size of the window control, etc.: Wingetpos ("window caption" [, "Window Text"]) ; for window controlgetpos ("window caption", "window text", control ID) ; Used for the control to get the state of the window control (including whether it is visible, active, maximized, minimized, etc.): wingetsate (Window caption [, window text]), for Window controlcommand (Window caption, window text, control ID, options) ; for the control to check whether the window exists winexists (window caption [, window text])
5.2 Common methods for validating Windows, control text
Gets the full title name of the window: wingettitle (Window caption [, window text]) Gets the text in the window: wingettext (Window caption [, window text]) Gets the text on the control: Controlgettext ("window caption", " Window text ", control ID)
5.3 Common ways to verify a picture or display effect
AutoIt does not provide an image comparison function and needs to develop its own function. If you are not ready to develop this feature, only the images or effects that will be inspected are saved with a screenshot and attached to the test report, allowing the tester to manually analyze it afterwards.
To intercept the entire screen or a specified area:
_screencapture_capture ("C:\Image1.jpg") or _screencapture_capture ("C:\Image1.jpg", 0,0,796,596) or _screencapture_ Capturewnd ("C:\Image1.jpg", Window handle)
Case:
#include <ScreenCapture.au3>; the path and file name used to save the image $file= "C:\ScreenCapture" & @MON & @MDAY & @HOUR & @MIN & Amp @SEC & ". jpg" _screencapture_capture ($file) and save to file
5.4 Common methods for validating files
Check if file exists: FileExists ("path") Get File Size: filegetsize ("path") Get file basic properties (including read-only, hidden, etc.): Filegetattrib ("path")
5.5 Other validations
Returns the current mouse pointer shape's id:mousegetcursor () gets the current mouse coordinate position: Mousegetpos ([dimension])
6. Example
The following script example demonstrates how to open a calculator, find a calculator window, perform a "1+2" calculation and validation of the operation calculator, and write the results of the check into a test report.
The script is as follows:
Run ("Calc.exe") winwaitactive ("calculator") Controlclick ("Calculator", "", "1") Controlclick ("Calculator", "", "+") Controlclick ("Calculator", "", " 2 ") Controlclick (" Calculator "," "," = ") $Result =controlgettext (" Calculator "," ", 403) if $Result = =" 3. Then filewriteline ("C:\result.txt", "correct: consistent with expected results 3") Else filewriteline ("C:\result.txt", "error: inconsistent with expected result 3, The actual result is "& $Result) Endifwinclose (" calculator ")
How to use AutoIt to complete a standalone test