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.
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
'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
'Every time the window named IEXPLORE.EXE|#32770|Message from webpage appears whatever its instance, the ClickOK Sub is launched
inWindowAnyInstance("IEXPLORE.EXE|#32770|Message from webpage")
'Load the web page
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.
'Definition of the proc to call if an error is detected.
'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.
'The UsePage returns an error, so OnAction Error triggers.