Useful functions for Web automation
Web automation is easy on HTML websites, you have just to use Recording mode and it generates the script for you. Internet Explorer 32 bits must be used, so if you have a x64 Windows, check that you use IE from Program files (x32).
If you want to create a Web automation script on a non-HTML Web site (a Flash site for instance), WinTask Recording mode object-oriented will not work. You can still use ClickOnTextOCR to click links embedded in an image, you can use the WinTask Navigate function to go directly to a page specified by its url, you can use Low-level Recording mode to record your actions at the mouse coordinates level.
The functions listed below can help in very rare circumstances when no usual synchronization method works (investigate first the help topic Advanced Web Synchronization).
Function wait_load_page() waits until the word Done is displayed in Internet Explorer status bar ; the wait is done until timeout seconds, after this timeout, an execution error is generated. As Internet Explorer is a low-integrity application under Vista or Windows 7, the capture of the status bar is not possible and so the wait_load_page function does not apply for Vista and Windows 7.
Function wait_url() waits until the specified word passed as parameter is part of the IE window title loaded in IE. It includes a timeout.
During Web navigation, it is possible to capture some text displayed in a Web page, use the Capture wizard (Start/Capture Wizard menu). The Capture_page_IE$ function listed below captures all the content of the IE window simulating a Select All (Ctrl+a) and a Copy (Ctrl+c). The copied data are then retrieved from Clipboard. The Capture_Area_IE$ function works the same way, but captures an area defined by mouse coordinates. ExtractBetween$ statement is used to extract a text between a beginning string and an end string.
The last function previous_page() simulates a click on Previous button in IE.
You can find the corresponding script file under Script_Examples, sub-directory of scripts.
'-----------------------------------------------
'Function waiting for Timeout seconds that a page is loaded.
'IMPORTANT : use this function only if none of the advanced synchronization tips work (Web automation help chapter,
' Web advanced synchronization topic).
'It does not verify that it's the proper page,
'it just waits until text defined in the global variable
'mes_end_load$ (Ex : "Done") is displayed in IE window status bar.
'Depending on the value of system variable #IgnoreErrors, if the page cannot be loaded,
'either an error message is displayed and the script is stopped, either
'a return code is sent back :
'0 page loaded
'1 page not loaded
'Insert the call to this function just after a Navigate statement.
'The function uses two global variables which must be defined in the main program :
'win_IE$="IEXPLORE.EXE|Internet Explorer_Server||1" Internet Explorer window name
'win_status_IE$="IEXPLORE.EXE|msctls_statusbar32||1" IE status bar window name
Function wait_load_page(Timeout)
'This function captures the text as displayed in the status bar.
'This capture is not allowed by Vista/Windows 7 because those versions of Windows start IE in low-integrity mode.
'So wait_load_page does not apply under Vista/Windows 7 or Windows 2008.local win_status$, quit, t, res
res=1
a$=""
quit=0
t=0
Usewindow(win_IE$)
pause 10 ticks
Movemouse(0,0,Absolute)
repeat
win_status$=capture$(win_status_IE$,0,0)
if instr(win_status$,mes_end_load$) > 0 then
quit=1
res=0
else
pause 1
t=t+1
endif
until quit=1 or t >=Timeout
If quit <> 1 then
if #IgnoreErrors=0 then
msgbox("Impossible to load the page",16,"Execution error")
stop
else
res=1
endif
endif
wait_load_page=res
EndFunction
'----------------------------------------------
'Function waiting for Timeout seconds that an URL containing the specified text is displayed.
'Depending on the value of system variable #IgnoreErrors, if the page cannot be loaded,
'either an error message is displayed and the script is stopped, either
'a return code is sent back :
'0 label$ has been found within the IE window title
'1 lablel has not been found
'Insert the call to this function just after the wait_load_page function.
'The function uses a global variable which must be defined in the main program :
'win_IE$="IEXPLORE.EXE|Internet Explorer_Server||1" Internet Explorer window name
function wait_url(label$,Timeout)
local t, quit, win$, res
res=1
t=0
quit=0
win$=""
usewindow(win_IE$)
repeat
win$=top$()
if instr(win$,label$) > 0 then
quit=1
res=0
else
pause 1
t=t+1
endif
until quit=1 or t >=Timeout
if quit=0 then
if #IgnoreErrors=0 then
msgbox(chr$(34)+label$+chr$(34)+"\n\not found within window title",16,"Execution error")
stop
else
res=1
endif
endif
wait_url=res
endfunction
'----------------------------------------------
'Function capturing the whole content of a Web page in a string.
'IMPORTANT : use this function only if GetPageSource$ or GetFrameSource$ WinTask function do not suit your needs.
'It uses Paste option in IE.
'The function uses a global variable which must be defined in the main program :
'win_IE$="IEXPLORE.EXE|Internet Explorer_Server||1" Internet Explorer window name
Function Capture_page_IE$()
Usewindow(win_IE$)
sendkeys("<Ctrl a>")
pause 5 ticks
sendkeys("<Ctrl c>")
clickmouse(left,Down,0,0)
clickmouse(left,Up,0,0)
Capture_page_IE$=getclipboard$()
EndFunction
'---------------------------------------------
'Function capturing the content of an area within a Web page ;
'this area is defined by x, y, height, width.
'You can find the coordinates of the area by using CaptureArea$ statement Wizard.
'This functions uses Paste option in IE.
'The function uses a global variable which must be defined in the main program :
'win_IE$="IEXPLORE.EXE|Internet Explorer_Server||1" Internet Explorer window name
Function capture_area_IE$(x,y,height,width)
local mes$, posx1, posx2, posy1,posy2 , res
posx1=x
posy1=y
posx2=x+width
posy2=y+height
Usewindow(win_IE$)
ClickMouse(left,down,posx1,posy1)
pause 5 ticks
MoveMouse(posx2,posy2)
pause 5 ticks
ClickMouse(left,up,posx2,posy2)
pause 5 ticks
sendkeys("<Ctrl c>",NoActivate)
pause 5 ticks
mes$=getclipboard$()
ClickMouse(left,down,0,0)
pause 1 ticks
ClickMouse(left,up,0,0)
capture_area_IE$=mes$
EndFunction
'----------------------------------------------
Function previous_page()
UseWindow(win_IE$)
sendkeys("<BackSpace>")
EndFunction
'------------------------------------------------
'EXAMPLE FOR MAIN PROGRAM
'VARIABLES INITIALIZATION
mes_end_load$="Done"
win_IE$="IEXPLORE.EXE|Internet Explorer_Server||1"
win_status_IE$="IEXPLORE.EXE|msctls_statusbar32||1"
Tmax=20
'open Wintask site
StartBrowser("IE","www.wintask.com")
'Note that this function wait_load_page does not work under Vista/Windows 7wait_load_page(Tmax)
wait_url("WinTask",Tmax)
'click hottest new version
Navigate("http://www.wintask.com/hottest_new_version.php")
'Note that this function wait_load_page does not work under Vista/Windows 7wait_load_page(Tmax)
wait_url("WinTask",Tmax)
pause 4
'capture an area
wintask_version$=capture_area_IE$(262,176,14,83)
msgbox(wintask_version$)
'go to buy now page
Navigate("http://www.wintask.com/buy_now.htm")
'Note that this function wait_load_page does not work under Vista/Windows 7wait_load_page(Tmax)
wait_url("WinTask",Tmax)
'capture all the page
page$=Capture_page_IE$()
'Extract between WinTask Full version : and CRLF
price_full$=ExtractBetween$(page$,"Full version :",excluded, chr$(13)+chr$(10),excluded)
'Extract between WinTask Lite version : and CRLF
price_lite$=ExtractBetween$(page$,"Lite version :",excluded, chr$(13)+chr$(10),excluded)
'previous page
previous_page()
'previous page
previous_page()
wait_url("WinTask",Tmax)
'capture all the page
page$=Capture_page_IE$()
'Extract between "Phone:" and " | E-mail:"
tel$=ExtractBetween$(page$,"Phone:",excluded, " | E-mail:",excluded)
'Extract between "E-mail:" and "| Privacy Policy"
Email$=ExtractBetween$(page$,"E-mail:",excluded, "| Privacy Policy",excluded)
'Close IE
CloseBrowser()
'display infos
mes$="PRODUCT VERSION\n\"+wintask_version$+"\n\\n\"+"PRICE\n\"+price_full$+"\n\"+price_lite$+"\n\\n\"+"CONTACT\n\"+tel$+"\n\"+Email$
msgbox(mes$,64,"WINTASK INFORMATIONS")