Jump to content

Help: exit if a window with error pops up.


Recommended Posts

Hello to all:

I am trying to get this script to run to do the following: I am running an AutoIt script that runs another program. If a window shows up with one of the following error messages, I want it to stop the script, close the program, and log the error window title with its information. Here is what I have:

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <ScreenCapture.au3>
#include <ProgressConstants.au3>
#include <SendMessage.au3>
#include <Array.au3>
#include <Debug.au3>

#include <../Include/ProgramInfo&Functions.au3>

Func ErrorCheck()
Local $EWin[19]
$EWin[0] = "Error"
$EWin[1] = "Input Error"
$EWin[2] = "Notice"
$EWin[3] = "Read Error"
$EWin[4] = "Save Error"
$EWin[5] = "Plot Error"
$EWin[6] = "File Read Error"
$EWin[7] = "Please make sure you download the latest Data archive."
$EWin[8] = "Warning"
$EWin[9] = "Help File Error"
$EWin[10] = "Missing 7z.dll"
$EWin[11] = "Date Error"
$EWin[12] = "Initialization Error"
$EWin[13] = "Data Format Error"
$EWin[14] = "Run Sequence Error"
$EWin[15] = "User Error"
$EWin[16] = "Display Error"
$EWin[17] = "TimeStep Error"
$EWin[18] = "Warning!"
Global $BWin = WinWaitActive($EWin,'')
If WinExists($BWin, '') Then
Global $BWinTitle = WinGetTitle($BWin,'')
Global $BWinText = WinGetText($BWin,'')
LogError($BWinTitle&":@CRLF"&$BWinText)
EndIf
EndFunc

Func OpenLog($sFileName, $sTestType, $sTestName)
    Global $hLog = FileOpen($sFileName, $FO_OVERWRITE) ; The log file
    Global $sTestId
    FileWriteLine($hLog, _
        $sTestType & " Test for Program" &@CRLF& _
    $sTestId & " - " & $sTestName &@CRLF& _
    @CRLF& _
    "ProgramVersion: " & $ProgramVersion &@CRLF& _
    "Program Binary:  " & $Program &@CRLF& _
    "AutoIt Binary:  " & @AutoItExe &@CRLF& _
    @CRLF& _
    "System:         " & @ComputerName)
EndFunc

Func LogError($sMessage)
    Global $sTestId
    FileWriteLine($hLog, "*** Error: " & $sMessage)
    FileWriteLine($hLog, "*** " & $sTestId & ": Failed ***")
    LogTime("Finished: ")
    CleanExit(1)
EndFunc

Func LogMessage($sMessage)
    FileWriteLine($hLog, $sMessage)
EndFunc

FindProgram()

RunProgram()

AdlibRegister(ErrorCheck())

ProgramRunTest()

LogMessage("Calculations finished")
LogTime()

LogMessage("Closing Program")

If $FAILMARKER > 0 Then
    LogMessage("--- Test Failed ---")
  
Program(Save)
LogMessage("Saving Files")
Sleep(2500)
LogTime("PASS: Save complete:    ")
Program(Close)
LogTime("Finished: ")
 MsgBox($MB_OK, "Test Result", "Test  Failed", 10)
ElseIf $FAILMARKER=0 Then
LogMessage("--- Test Passed ---")

Program(Save)
LogMessage("Saving Files")
Sleep(2500)
LogTime("PASS: Save complete:    ")
Program(Close)
LogTime("Finished: ")
MsgBox($MB_OK, "Test Result", "Test Passed", 10)
EndIf
CleanExit()

I run it and the first window of the program comes up and it gets stuck there. 

 

Any handles or text in here that is not defined is already done so in include/ProgramInfo&Functions.au3. Those work and for simplicity I am not going to include them. 

Edited by SoftWearInGinEar
Link to comment
Share on other sites

 

Local $EWin = WinList()
$EWin[0] = "Error"
$EWin[1] = "Input Error"

WinList ( ["title" [, "text"]] )
Returns an array of matching window titles and handles.
The array returned is two-dimensional and is made up as follows:
    $aArray[0][0] = Number of windows returned
    $aArray[1][0] = 1st window title
    $aArray[1][1] = 1st window handle (HWND)
    $aArray[2][0] = 2nd window title
    $aArray[2][1] = 2nd window handle (HWND)

 

why you assign
$EWin[0] = "Error"
$EWin[1] = "Input Error"

 

After all, what keeps the $EWin array,  window title and handle ,  or the  errors

 

Edit:
Then here say error in line 122
C:\Users\SoftWearinGinEar\Program\v2.2.2\Tests\test.au3" (122)

but in the Script you pass us it has only 103 line
what is line 122?

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <ScreenCapture.au3>
#include <ProgressConstants.au3>
#include <SendMessage.au3>
#include <Array.au3>
#include <Debug.au3>

#include <../Include/ProgramInfo&Functions.au3>

Func ErrorCheck()
Local $EWin0 = "Error"
Local $EWin1 = "Input Error"
Local $EWin2 = "Notice"
Local $EWin3 = "Read Error"
Local $EWin4 = "Save Error"
Local $EWin5 = "Plot Error"
Local $EWin6 = "File Read Error"
Local $EWin7 = "Please make sure you download the latest Data archive."
Local $EWin8 = "Warning"
Local $EWin9 = "Help File Error"
Local $EWin10 = "Missing 7z.dll"
Local $EWin11 = "Date Error"
Local $EWin12 = "Initialization Error"
Local $EWin13 = "Data Format Error"
Local $EWin14 = "Run Sequence Error"
Local $EWin15 = "User Error"
Local $EWin16 = "Display Error"
Local $EWin17 = "TimeStep Error"
Local $EWin18 = "Warning!"
        ; Retrieve the window title of the active window.
Global $AnyWindow = WinExists("[ACTIVE]",'')
If WinExists($AnyWindow,'') Then
Global $sTitle = WinGetTitle($AnyWindow,'')
Global $sText = WinGetText($AnyWindow,'')
If $sTitle = $EWin0 Or $sTitle = $EWin1 Or $sTitle = $EWin2 Or $sTitle = $EWin3 Or $sTitle = $EWin4 Or $sTitle = $EWin5 Or $sTitle = $EWin6 Or $sTitle = $EWin7 Or $sTitle = $EWin8 Then
Local $BWinTitle = WinGetTitle($AnyWindow,'')
Local $BWinText = WinGetText($AnyWindow,'')
LogError($BWinTitle&":"& @CRLF & $BWinText)
ElseIf $sTitle = $EWin9 Or $sTitle = $EWin10 Or $sTitle = $EWin11 Or $sTitle = $EWin14 Or $sTitle = $EWin15 Or $sTitle = $EWin16 Then
    Local $BWinTitle = WinGetTitle($AnyWindow,'')
Local $BWinText = WinGetText($AnyWindow,'')
LogError($BWinTitle&":"& @CRLF &$BWinText)
ElseIf $sTitle = $EWin17 Or $sTitle = $EWin18 Then
Local $BWinTitle = WinGetTitle($AnyWindow,'')
Local $BWinText = WinGetText($AnyWindow,'')
LogError($BWinTitle&":"& @CRLF &$BWinText)
EndIf
EndIf
EndFunc

Func OpenLog($sFileName, $sTestType, $sTestName)
    Global $hLog = FileOpen($sFileName, $FO_OVERWRITE) ; The log file
    Global $sTestId
    FileWriteLine($hLog, _
        $sTestType & " Test for Program" &@CRLF& _
    $sTestId & " - " & $sTestName &@CRLF& _
    @CRLF& _
    "ProgramVersion: " & $ProgramVersion &@CRLF& _
    "Program Binary:  " & $Program &@CRLF& _
    "AutoIt Binary:  " & @AutoItExe &@CRLF& _
    @CRLF& _
    "System:         " & @ComputerName)
EndFunc

Func LogError($sMessage)
    Global $sTestId
    FileWriteLine($hLog, "*** Error: " & $sMessage)
    FileWriteLine($hLog, "*** " & $sTestId & ": Failed ***")
    LogTime("Finished: ")
    CleanExit(1)
EndFunc

Func LogMessage($sMessage)
    FileWriteLine($hLog, $sMessage)
EndFunc

FindProgram()

RunProgram()

AdlibRegister(ErrorCheck())

ProgramRunTest()

LogMessage("Calculations finished")
LogTime()

LogMessage("Closing Program")

If $FAILMARKER > 0 Then
    LogMessage("--- Test Failed ---")
  
Program(Save)
LogMessage("Saving Files")
Sleep(2500)
LogTime("PASS: Save complete:    ")
Program(Close)
LogTime("Finished: ")
 MsgBox($MB_OK, "Test Result", "Test  Failed", 10)
ElseIf $FAILMARKER=0 Then
LogMessage("--- Test Passed ---")

Program(Save)
LogMessage("Saving Files")
Sleep(2500)
LogTime("PASS: Save complete:    ")
Program(Close)
LogTime("Finished: ")
MsgBox($MB_OK, "Test Result", "Test Passed", 10)
EndIf
CleanExit()

Update: This works and runs the whole program through, but it won't read the error windows when I force generate them. It is supposed to shut down and make a log of the error window when one does come up. I use ElseIfs btw because for some reason my editor will not read _ for the Or statements (the editor runs out of room). Otherwise I would just put it on one whole If Statement.

Any help or feedback would be appreciated!

Link to comment
Share on other sites

AdlibRegister ( "function" [, time = 250] )

Every 250 ms (or time ms) the specified "function" is called - note that the first call to the function is after the specified time period and not immediately the function is registered.
Typically the function is used to check for unforeseen errors. For example, you could use adlib in a script which causes an error window to pop up unpredictably.
The adlib function should be kept simple as it is executed often and during this time the main script is paused. Also, the time parameter should be used carefully to avoid CPU load.
You can not register a function using parameters.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You declare the variables every 250ms, while you simply have to update them. which means that you first declare them in Global scope, and in the AdlibRegister function you simply update them
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

a good tactic is to declare all public variables / constants, above, in the main program outside the function. in the function you simply call them
and the functions should be at the bottom

script structure

ScriptStrcture4.png ScriptStrcture6.png

 

in the example I brought things as I imagined, since pieces of code were missing, but the idea is similar

:)  I hope it works for you

;~ #include <MsgBoxConstants.au3>
;~ #include <AutoItConstants.au3>
;~ #include <FileConstants.au3>
;~ #include <ScreenCapture.au3>
;~ #include <ProgressConstants.au3>
;~ #include <SendMessage.au3>
#include <Array.au3>
;~ #include <Debug.au3>
;~ #include <../Include/ProgramInfo&Functions.au3>

Global $AnyWindow, $aErr, $FAILMARKER
Global $BWinTitle, $BWinText
Global Const $sLog = StringTrimRight(@ScriptFullPath, 4) & ".log"

; Returns an array, with Errors
$aErr = "Error;Input;Error;Notice;Read Error;Save Error;Plot Error;File Read Error;Date Error;" & _
        "Please make sure you download the latest Data archive.;Warning;Help File Error;Missing 7z.dll;" & _
        "Initialization Error;Data Format Error;Run Sequence Error;User Error;Display Error;TimeStep Error;Notepad" ;Warning!  * <<-
$aErr = StringSplit($aErr, ";")

AdlibRegister("ErrorCheck")

;~ FindProgram()
RunProgram()
;~ ProgramRunTest()
;~ LogMessage("Calculations finished")
;~ LogTime()
;~ LogMessage("Closing Program")
;~ ...


CleanExit()

;----------------------------------------------------------------------------------------
Func RunProgram()

    ; Run Notepad
    Local $hWin_1 = WinWait("[CLASS:Notepad]", "", 1)
    If Not WinExists($hWin_1) Then Run("notepad.exe")
    $hWin_1 = WinWait("[CLASS:Notepad]")
    ConsoleWrite("- $hWin_1=" & $hWin_1 & @CRLF)

    Local $sTxt = "close this window to display the Save dialog" & @CRLF
    WinActivate($hWin_1)
    Send($sTxt)

    While WinExists($hWin_1)

        Sleep(100)
    WEnd

EndFunc   ;==>RunProgram
;----------------------------------------------------------------------------------------
Func ErrorCheck()
    Local $ActiveWindow, $sTitle

    $ActiveWindow = WinGetHandle("[ACTIVE]")

    If $AnyWindow <> $ActiveWindow Then
        $AnyWindow = $ActiveWindow
        $sTitle = WinGetTitle($ActiveWindow)
        ;ConsoleWrite("$sTitle=" & $sTitle & @CRLF)

        For $i = 1 To $aErr[0]
            If $sTitle = $aErr[$i] Then
                $BWinTitle = $sTitle
                $BWinText = WinGetText($ActiveWindow)
                LogError($BWinTitle & ":" & @CRLF & $BWinText)
                CleanExit($i)
            EndIf
        Next
    EndIf

EndFunc   ;==>ErrorCheck
;----------------------------------------------------------------------------------------
Func LogError($sMessage)
    Local Static $iErrCnt = 0
    Local $sTestId = "??"
    FileWriteLine($sLog, "*** Error: " & $sMessage)
    FileWriteLine($sLog, "*** " & $sTestId & ": Failed ***")
;~  LogTime("Finished: ")
EndFunc   ;==>LogError
;----------------------------------------------------------------------------------------
Func LogMessage($sMessage)
    FileWriteLine($sLog, $sMessage)
EndFunc   ;==>LogMessage
;----------------------------------------------------------------------------------------
Func CleanExit($iExitCode = 0)
    If $iExitCode > 0 Then
        ConsoleWrite("!*** Dirty Exit:" & $iExitCode & @CRLF)
        _ArrayDisplay($aErr, "$aErr")
    Else
        ConsoleWrite("*** Clean Exit:" & $iExitCode & @CRLF)
    EndIf

    Exit
EndFunc   ;==>CleanExit
;----------------------------------------------------------------------------------------

 

Edited by ioa747
UpDate

I know that I know nothing

Link to comment
Share on other sites

 

14 minutes ago, SoftWearInGinEar said:

It seems to work on the first window that shows up (the window of the program)

which program? What is the title? When  the title is in $aErr array,  then yes,  in first catch go to exit
As the title says   exit if a window with error pops up.
if you have something else in mind you should give more information

I know that I know nothing

Link to comment
Share on other sites

I apologize because I know this will make this harder, but I can't really talk about the program because it is related to my work (company propriety information). I can say what I am looking for however is that I want the function to check to titles and texts of all the windows. And if it finds a match (or even partial) then to log and exit. I am now looking into StringRegExp. Think that could help?

Link to comment
Share on other sites

The problem is that you have to decide what the program will do after it find/match first window from $aErr

  • in case  go to exit,   as in the example above,
    when the program finds the first match it will close the program, so it will NOT find/match another window
     

You have to think about the work flow , and what actions should be taken, after finding the first window from $aErr

 

I know that I know nothing

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...