drew.d.lenhart

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

Why AutoIT Matters - A Windows Automation Scripting Language

2014/09/05

AutoIT is a superb scripting language that can be used for automating daily tasks. AutoIT is a language similar to BASIC that not only can be used for scripting, but can be used to make GUI programs as well. Another great point to make is that the scripts you create can be compiled into executables! If you are a Mac user like I am, you can think of it as the AppleScript of Windows. Yes AutoIT is limited to Windows platforms only.

* * * * *

So lets say you are the IT guy for an organization in charge of multiple applications that your company uses. Instead of getting multiple phone calls, emails, or text messages that a important web application the company uses is down, why not create your own scripts to test the applications functionality?

AutoIT can do this, and do it well. In this tutorial, I will create a basic AutoIT script that opens a web browser, navigates to a web page, and checks to make sure content is on the page. This script will satisfy the need that the website is online and content is available to users.

Before we start, be sure to download the latest version of AutoIT. There are a number of different installers available. Grab the version with the ScITE editor. We will use this program to edit our scripts and compile when the script is complete.


First, add the librarys we need:

#include <ie.au3>
#include <MsgBoxConstants.au3>

The _IE functions are perfect for creating scripts that open in IE. Check out the reference library for complete list of functions.

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

_IELoadWait($oIE)

$read_home = _IEBodyReadText($oIE)

Here I am using three _IE functions. First create a new window with _IECreate.  _IELoadWait pauses the script and allows the page to load fully.  _IEBodyReadText($oIE) reads all the text within the <body> tags.


If StringInStr($read_home, "Home") = 0 Then
   MsgBox($MB_SYSTEMMODAL, "Text Not found", "Could not find the string 'home'!", 5)
   _IEQuit($oIE) ; Quit IE browser and ends script -----------
   Return
Else

Compare whats read using _IEBodyReadText with "Home" ( I'm basically searching my homepage for the "Home" link and verifying it exists. ).  If "Home" is not found, the script ends in error.


$get_link = _IELinkClickByText($oIE, "Downloads")
   Sleep(5000)

   $read_down = _IEBodyReadText($oIE)
   If StringInStr($read_down, "Downloads") = 0 Then
      MsgBox($MB_SYSTEMMODAL, "Text Not Found", "Could not find the string 'downloads'!", 5)
      _IEQuit($oIE)
      Return
   Else

If the script found "home", then use _IELinkClickByText to click the "Downloads" link in my navigation bar.  The next part should look familiar.  Its checking the text in the body and comparing in the StringInStr function.


     MsgBox($MB_SYSTEMMODAL, "Success!", "All script checks were found.", 5)

   EndIf

EndIf

Sleep(3000)
_IEQuit($oIE)

Lastly, if all search strings were found, end the script with a MsgBox popup confirmation.   Notice at the end, _IEQuit($oIE) is used to close down the IE window.

 

To test the code go to Tools >> Go, or press F5 and watch it run. ( Replace the web address and search strings to check your own website! )

*Tools >> Compile , will compile the script in .exe format!


The example above is a very simple example of what you can do with Auto IT.  Ideally you wouldn't want to use Message Boxes as the status notification.  You could configure the script to email you the results, or post the results to a website/dashboard!  Fire up Windows Task scheduler and schedule the script to run hourly/daily/weekly!

Download the script example above, I've also included code to email for success/failure.

Links:

AutoIT Reference Manual

Tutorials Wiki


-Drew