drew.d.lenhart

programming, software, technology, anything on my mind...

Why AutoIT Matters – Part 2

2015/02/17

In the last article, I quickly ran through how to do a simple check of a website. I left the script kind of open ended and kind of useless. After a little more testing the email function I included wasn't really useful and actually didn't work. So I came up with another solution, as well as made some slight adjustments to the script. Read up on the last article before going further!


Notifications

The script currently has pop up messages for success/failures. This is nice, but what if this script is running several times a day? Having to sit and watch the progress of a automated script kind of defeats the purpose. Lets fire off an email when the script is a success/failure.

To accomplish this, I used and external program to send mail. The built in _INetSMTPMail function isn't very useful when most smtp services these days require a username/password/port yada yada. Download mailsend.exe by muquit! With this program, we can execute the program with parameters in our AutoIT script!

mailsend

**The code example below is using a Gmail account to send status messages! I use this line in several of my personal scripts! Thank you for free smtp services GMAIL!!

Run('mailsend.exe -to youremail@gmail.com -from from-email@gmail.com -name "Your Name" -ssl -smtp smtp.gmail.com -port 465 -sub "Results for website - FAILURE Detected" +cc +bc -q -auth-plain -user "gmail username" -pass "gmail pass" -msg-body "results.txt"')

Full Source

;Author:      Drew D. Lenhart
;Website:       http://www.drewlenhart.com
;Description:   Simple automated website functionality script
;Date:          Feb 2015

#include <IE.au3>
#include <MsgBoxConstants.au3>

Global $fail_count = 0

Func Run_script()
   Local $oIE = _IECreate("http://www.drewlenhart.com")

   _IELoadWait($oIE)

   ;Read the text within the <body></body> tags
   $read_home = _IEBodyReadText($oIE)

   ;Check for the text "Home" ------------------
   If StringInStr($read_home, "Home") = 0 Then
      MsgBox($MB_SYSTEMMODAL, "Text Not found", "Could not find the perameter 'home'!", 5)
      $fail_count = 1
      $stat = "FAILED - Home screen!"
         create_report($stat)
      _IEQuit($oIE) ; Quit IE browser and ends script -----------
      Return
   Else
      ;Click the Downloads link, and check body for "Downloads" text ------------
      $get_link = _IELinkClickByText($oIE, "Downloads")
      Sleep(5000)

      $read_down = _IEBodyReadText($oIE)
      Sleep(2000)
      If StringInStr($read_down, "Downloads") = 0 Then
         MsgBox($MB_SYSTEMMODAL, "Text Not Found", "Could not find the peramter 'downloads'!", 5)
         $fail_count = $fail_count + 1
         $stat = "FAILED - Download screen!"
         create_report($stat)
         _IEQuit($oIE)
         Return
      Else
         ;All success, now display success message. ------------------
         MsgBox($MB_SYSTEMMODAL, "Success!", "All script perameters were found.", 5)
         $stat = "PASSED"
         create_report($stat)
      EndIf

   EndIf

Sleep(3000)
_IEQuit($oIE)

EndFunc

Func send_report()
    If $fail_count >= 1 Then
        ;Use Run() to execute mail program
        Run('mailsend.exe -to youremail@gmail.com -from from-email@gmail.com -name "Your Name" -ssl -smtp smtp.gmail.com -port 465 -sub "Results for website - FAILURE Detected" +cc +bc -q -auth-plain -user "gmail username" -pass "gmail pass" -msg-body "results.txt"')
    Else
        ;Use Run() to execute mail program
        ;Uncomment bottom line if you want passed emailes:
        Run('mailsend.exe -to youremail@gmail.com -from from-email@gmail.com -name "Your Name" -ssl -smtp smtp.gmail.com -port 465 -sub "Results for website - PASSED" +cc +bc -q -auth-plain -user "gmail username" -pass "gmail pass" -msg-body "results.txt"')
    EndIf
 EndFunc

 ; Create a file.
Func FileCreate($sFilePath, $sString)
    ;time stamp for when the script started.
    $datetimestamp = "" &@YEAR& "-" & @MON & "-" & @MDAY & "  " & @HOUR & ":" & @MIN & ":" & @SEC & ""
    Local $bReturn = True ; Create a variable to store a boolean value.
    If FileExists($sFilePath) = 0 Then $bReturn = FileWriteLine($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False.
    FileWriteLine($sFilePath, $datetimestamp)
    FileWriteLine($sFilePath, " ")
    Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite.
EndFunc

Func create_report($status)
    Local $stat2 = "STATUS: [ " & $status & " ]"
    Local $line = "-------------"
    $file = FileOpen("results.txt",1)
    FileWriteLine($file,$stat2)
    FileWriteLine($file,$line)

    FileClose($file)

EndFunc

;Delete results file before each run
FileDelete("results.txt")
Sleep(1000)
FileCreate("results.txt", "Results of my website checkout:")
Sleep(200)
Run_script()
Sleep(100)
send_report()

Download full sourcecode.

Thanks for reading! -Drew