The previous article introduced the basic functions and general modules of autohotkey. This time, we will use a simple example to illustrate how to compile autohotkey scripts. Like popular automated testing tools, you can write scripts by recording them, or manually writing scripts. However, for GUI applications, recording operations are basically required to generate scripts for objects or coordinates that have traveled. Autohotkey is relatively simple, basically only records coordinates. A few window operations are represented by objects, which is equivalent to low-level recording in popular automated testing tools.
The following is how to use autoscriptwriter (recorder) recording in autohotkey to open the IE browser in the "quick launch" column on the desktop, enter www.google.com in the address bar, and press Enter, enter test in Google search and press Enter.
---------
Winwait ,,
Ifwinnotactive, winactivate ,,
Winwaitactive ,,
Mouseclick, left, 125, 22
Sleep 100
Winwait, blank page-Windows Internet Explorer,
Ifwinnotactive, blank page-Windows Internet Explorer, winactivate, blank page-Windows Internet Explorer,
Winwaitactive, blank page-Windows Internet Explorer,
Send, www.google.com {enter}
Winwait, Google-Windows Internet Explorer,
Ifwinnotactive, Google-Windows Internet Explorer, winactivate, Google-Windows Internet Explorer,
Winwaitactive, Google-Windows Internet Explorer,
Send, test {enter}
Winwait, test-Google search-Windows Internet Explorer,
Ifwinnotactive, test-Google search-Windows Internet Explorer, winactivate, test-Google search-Windows Internet Explorer,
Winwaitactive, test-Google search-Windows Internet Explorer,
Mouseclick, left, 1016, 13
Sleep 100
--------------
You can see that the input is represented by "send" and "<character>". Click "mouseclick", "Left", "<X >,< y>. Many ifwinnotactive statements are similar to the synchronization statements in the test tool. Because coordinates often change, if you can use keys, try to use keys to avoid coordinates. You can click the "Google Search" button to implement the above "enter", but it is better to use the "enter" key for the sake of insurance.
Autohotkey also provides a better scripting language, which is similar to VBScript. You can open autohotkey help file to search for it. After a simple modification, the script is as follows:
--------------
Run % a_programfiles %/Internet Explorer/iexplore.exe; start IE browser
Sleep 5000; wait launch IE browser
Send, www.google.com {enter}; input Google's website in Address Bar
Sleep 5000; wait open Google's home page
Send, test {enter}; input keyword in search field
Sleep 5000; wait search result
Mouseclick, left, 1016, 13; click X button to close ie window
--------------
In the above script, I used sleep for synchronization. In fact, there are many other methods. For example, the waitobject statement is to execute the next operation when the color of a certain area or point is the expected color, you can also wait for an image. Because objects are not provided, other methods are not too flexible and need to be used in many places, so they are not used. You can use the following method for some important items:
Loop, 5
{
Ifwinexist, save as; check whether Save As dialogbox exists
Break
Else
Sleep % shortsleeptime %
}
You can also use winclose and A to close the window of the current activity. There are many such statements. Just like writing code before, the only one is that no other scripts can be called and only valid in the current script. However, I was not using it right away.
If you are interested, you can discuss it together.