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
'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())
'Remove the frame
'Enable the event back so it will trigger for another appearance of the IE error window

'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

'Load the web page
StartBrowser("IE", "

 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
'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.
'Take a screenshot of the desktop
'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$
buffer$="Error line : "+str$(#LastErrorLine)
buffer$="Error Code : "+str$(#Errorcode)
buffer$="Error Function : "+#ErrorFunction$
buffer$="Error message : "+#ErrorMsg$
'Add here other lines for what to do in case of an error.
OnAction error
dosub Process_Error
'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