This is the last text that I plan to enter to share with you my experience. It describes the structure of my scripts.
Code:
The content of a script
General considerations
a) When I was at school (during the mainframe era) it was generally admitted that the user needed 2 to 5 seconds between the reception of the application screen and the key that send it back to the application having filled the various fields. Now the processor are smaller and more powerful but the users remain the same... So I add a fixed sleep before each actions except the last one that has a random sleep to make simulation more realistic. Total of sleeps is in the low part of the 2 to 5 s. range because I don't take into account processing time.
b) Ahk is fast (order of magnitude few ms.), FF and its Javascript functions is slow (order of magnitude few 1/10 of a s.). You need to synchronize them : sometimes it is not needed (e.g. you change the size of the screen), in the functions of the library it is built in (using the content of the clipboard), but when the page call a Javascript function during or at the end of loading or when you have clicked a button, I don't know a method to verify that the function has finished so I put a long sleep (order of magnitude 1 s.) : this is included in the range of a) .
c) the servers and the programming of an application have a cost. It is paid by your country, a foundation, a company that uses it as a service to its customers, or by advertisement. In this case, either the advertisement use a part of each screen and you don't take it into account or the application forces (before going to its next screen) a choice between some banners that bring you to an advertisement screen that you have to close in the processing of your script (see below).
The structure of a script is always the same : start part, N screens, closing part. You can see that in the distributed scripts.
1) start part
a) Call to the application site using e.g. :
Run, www.autohotkey.com/forum/login.php,Max ; i.e. any URL can be launched.
If there is a disconnect/logoff link or function it is often good to use it here to be sure to start in a clean situation. You are redirected to a more classic screen.
b) initialization of parameters of the script
c) wait till the page has finished loading using :
rc := fini()
2) for each screen
a) verify that we are on a right screen. In fact there can be several right screens according the circumstances e.g. nothing new since your last access, something new, loop finished, etc. and the processing may change according this. This is the reason why I have put this here and not at the end of the previous item.
To verify this you have different methods and functions already written, if they are not sufficient code your own and propose it for inclusion in the library if you use it several scripts.
You can verify on Title, URL, Source or a combination or a part of them.
There are basic functions : AHK WinGetTitle command, retURL() RETurns URL (in fact the content of the address bar that with some systems include the true URL and parameters generally separated by a ? ), sdom() returns Source of DOM.
There are also elaborate functions that do verification, exit from the script if this fail (be cautious if you have to accept several screens) and set global the verified variable (Title, vURL, source) : vertit(x) VERifies that TITle contains x, verURL(x) VERifies that the URL is x or URLcont(x) verifies that the URL CONTains x, versourc(x) VERify that the SOURCe of DOM contains x.
b) do the processing of the screen : fill fields, click on buttons, extract some informations or a combination of these with the programming that goes around, eventually log some results. The last action is generally to click somewhere (a link or a function) to go to the next screen of the application.
c) special case : the application has forced (before going to its next screen) a choice between some banners that bring you to an advertisement screen. I close it with :
SetTitleMatchMode 2
;WinClose [, WinTitle, WinText, SecondsToWait, ExcludeTitle, ExcludeText]
WinClose , Firefox, ,0 , The_application_screen_I_wait_for_Title,
d) wait till the page has finished loading using :
rc := fini()
3) Closing part
If there is a disconnect/logoff button click it (to have a clean end of the application) and wait : This may also be considered as a particular case of 2) .
I close (at the end of the script) the instance of FFX opened by run with :
SetTitleMatchMode 2
WinClose , Firefox, ,0
Then you may do some log recording e.g. using Dur(x) DURation of script to write the last record of the log before
exit ; end of script
or any other cleaning process.