Home > Enhancing Scripts > Handling Events

Handling Events

An event is a specific action that occurs during the script execution. An example of such an event is the appearance of an unexpected window during the script execution. Another example is the appearance of an error message due to an execution error. An event is NOT a window which is displayed only in some circumstances, for this case, use an If  statement in cunjunction with an ExistW function.

WinTask x64 can handle events which occur at any time during script execution: OnAction statement handles events occurring in the application under automation, OnAction Error statement handles a script execution error. 

OnAction example for handling an unexpected browser window

On many web forms, if you fill a field with an incorrect value, a browser error window is displayed. The sample script below shows how OnAction can be used to click the OK button on an IE window as soon as it is displayed. Such an IE error window is simulated on our demo page www.wintask.com/demos/form.htm.

'Definition of the subroutine which is launched when the specified window appears
Sub clickOK()
'Disable the event processing to avoid an infinite loop if the same window is displayed when processing the error window
Disable(IEclose)
'Display a frame when the IE error window is detected
msgframe("IE error window detected",1)
'When the window is there, click its OK button to make it disappear
UseWindow("IEXPLORE.EXE|#32770|Message from webpage",topinstance())
 Click(Button,"OK")
'Remove the frame
removeframe(1)
'Enable the event back so it will trigger for another appearance of the IE error window
Enable(IEclose)
EndSub

'Definition of the OnAction, every time the specified window appears, it calls the subroutine
OnAction IEclose
'Every time the window named IEXPLORE.EXE|#32770|Message from webpage appears whatever its instance, the ClickOK Sub is launched
WinStatus(Exists, Exact)
inWindowAnyInstance("IEXPLORE.EXE|#32770|Message from webpage")
DoSub ClickOK
EndAction

'EXAMPLE
'Load the web page
StartBrowser("IE", "
www.wintask.com/demos/form.htm",3)

UsePage("Form")
 WriteHTML("INPUT TEXT[NAME= 'company']", "My company")
'Simulate an IE error window appearing by clicking the Popup window link
 ClickHTMLElement("A[INNERTEXT= 'Popup window']")
 
'By inserting a SLEEP statement, the script does not end and the OnAction stays active
sleep()
'And so close manually the .ROB to stop script execution.

OnAction Error example for handling a script execution error

The script below shows how to call an error procedure when a script execution error occurs. The error procedure takes a hardcopy of the screen. For running every x minutes the same script whatever happened at previous execution, see #ExecTimeout and #ScriptAfterTimeout$ System Variables.

'Definition of the proc to call if an error is detected.
Sub Process_Error()
local buffer$
 
'If within this proc, an error occurs too, it generates an infinite loop as
'the call to the error proc causes an error. To avoid that, disable OnAction Error within this proc.
disable(Error)
'Take a screenshot of the desktop
ret=Hardcopy("C:\wttest\error.jpg",1,1)
 
'Here a part for writing in a txt file where we were in the script, the error code, the error message when the error occurred.
buffer$="Error in : "+#ErrorScript$
write("C:\wttest\error.txt",buffer$,crlf)
buffer$="Error line : "+str$(#LastErrorLine)
write("C:\wttest\error.txt",buffer$,crlf)
buffer$="Error Code : "+str$(#Errorcode)
write("C:\wttest\error.txt",buffer$,crlf)
buffer$="Error Function : "+#ErrorFunction$
write("C:\wttest\error.txt",buffer$,crlf)
buffer$="Error message : "+#ErrorMsg$
write("C:\wttest\error.txt",buffer$,crlf)
 
'Add here other lines for what to do in case of an error.
'......
 
Endsub
 
OnAction error
dosub Process_Error
endaction
 
'EXAMPLE
StartBrowser("IE","www.wintask.com/demos",3)
'The UsePage returns an error, so OnAction Error triggers.
UsePage("Bla bla")

See also

Working with Excel
Working with Databases
Naming Objects, Spy Tool
Improving Object Recognition by using UI Automation Technology
Adding Dialog Boxes