Jump to content
Sign in to follow this  
prunesquallor

Using AutoIt for automated testing

Recommended Posts

prunesquallor

I'm trying to see if I can use AutoIt as a way to automate the testing of executables, and am hitting a few problems. I am assuming that AutoIt should be rather good at this, so would appreciate any help.

I've created a Codegear C++ executable called ATI1.exe, which contains exactly 1 TEdit Password (ie masked) field. It has no other controls, and actually does nothing: the field simply accepts whatever is typed into it. It resides in the same folder as the AutoIt script.

I then want to test that I can read what I've put into a field, so the script:

1. Opens a log file.

2. Starts the executable.

3. Writes and then reads the same string 100 times into the field.

4. Closes the log file and exits. It actually leaves the executable running, but this is a cut-down example.

The script:

#cs ----------------------------------------------------------------------------
 Tests the response of a TEdit Password field to AutoIT.

 Toytown programming: no error handling. Needs executable in same
 directory called AIT1.exe with a single TEdit password field. Window
 name of executable is "AutoItPassTest1"
#ce ----------------------------------------------------------------------------

Global $iLineCount = 0 ; Always updated by the OutputLogLine routine
Global $iErrorCount = 0 ; Always updated by any test routine

;Open log file, write first line
Global $fLogFile = FileOpen(@ScriptDir & "\TestTEditLogFile.txt", 2)
OutputLogLine("Started UAT Test TEdit at " & GetNow(), 1)

;Start test executable
Run(@ScriptDir & "\AIT1.exe")
Global $hWHandle = WinWaitActive("AutoItPassTest1", "", 2)

BlockInput(1) ; Doesn't do much

;Run test 100 times checking input into field
For $iI = 1 to 100
  $sPasswordText1 = "ufVNn{{"""
  SendTextToControl($sPasswordText1)
  OutputLogLine("Password field given password: """ & $sPasswordText1)
  CheckTextFromControl($sPasswordText1)
  OutputBlankLine()
  Sleep(100)
Next

BlockInput(0)

;Write last line, close out log file
OutputLogLine("Run completed at: " & GetNow() & " with "& $iErrorCount & " errors.")
FileClose($fLogFile)
Exit

;Send text string to control
Func SendTextToControl(Const $sText)
  Local $iSendTextCharRaw = 1
  ControlFocus("", "", "[CLASS:TEdit; INSTANCE:1]")
  ControlSend("", "", "[CLASS:TEdit; INSTANCE:1]", "{HOME}+{END}{DEL}")
  ControlSend("", "", "[CLASS:TEdit; INSTANCE:1]", $sText, $iSendTextCharRaw)
EndFunc

;Check that control has test text string
Func CheckTextFromControl(Const $sText)
  Local $sCheckText = ControlGetText("", "", "[CLASS:TEdit; INSTANCE:1]")
  If $sCheckText == $sText Then
    OutputLogLine("Text in " & "[CLASS:TEdit; INSTANCE:1]" & " is: " & $sCheckText & " - and is correct.")
  Else
    OutputLogLine("Text in " & "[CLASS:TEdit; INSTANCE:1]" & " is: " & $sCheckText & " - and is INCORRECT. +++ERROR+++")
    $iErrorCount += 1
  EndIf
EndFunc

;Output line to log file, with trailing blank lines if required
Func OutputLogLine(Const $sOutLine, Const $iExLines = 0)
  $iLineCount += 1
  $sOutput = StringFormat("%06u", $iLineCount) & " " & $sOutLine
  FileWriteLine($fLogFile, $sOutput)
  For $iCount = 1 To $iExLines
    FileWriteLine($fLogFile, @CRLF)
  Next
EndFunc

;Output blank line to log file
Func OutputBlankLine()
  FileWriteLine($fLogFile, @CRLF)
EndFunc

;Get formatted date and time ISO 8601
Func GetNow()
    return @YEAR & "-" & @MON & "-" & @MDAY & "T" & @HOUR & ":" & @MIN & ":" & @SEC
EndFunc

On runnning, the input into the field will match what is read, as long as I don't click anywhere on the executable or switch windows to another window. The comparisons will fail in this case: generally, use of mouse or keyboard and changing windows focus interferes with the comparisons.

BlockInput doesn't seem to work (?), and doesn't stop me switching away from the test program window to another window. I can't attach a working version of the executable due to the combination of Codegear code bloat and my upload quota!

So I guess the question is: can I get AutoIt to completely lock out all inputs and stop all window switching while the test is running?

I'm new at this, so all help gratefully received!

Prune

Share this post


Link to post
Share on other sites
prunesquallor

Answering myself, a lot of the problems go away if #RequireAdmin is added (under Vista)!

Not all though, but the main errors caused by mouse and key clicks definitely go away.

Prune.

Share this post


Link to post
Share on other sites
jaberwacky

You could insert some WinActive statements.

If WinActive("Window Name") then 

    ; code to execute

Else

    Do
 
    sleep(1000)

    Until WinActive("WIndow Name")

Endif
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
prunesquallor

Thanks for the reply. This is to make sure that the test Window is ready for input I presume? Thanks - I'll give it a whirl.

ATB

Prune

Share this post


Link to post
Share on other sites
Makaule

or you could simply use this:

WinWait($App)
;Some action

It would use sleep mode till application would be launched.

Share this post


Link to post
Share on other sites
prunesquallor

Thanks Jaberwocky6669/Makaule. I'm now doing a WinWaitActive at the start, to ensure that the target program comes up, then before I do anything with the fields I do:

;Make sure our windows's active with pause
Func PauseMakeSureWindowIsReady()
  Do
    Sleep(100)
  Until WinActive("Window Name")
EndFunc

This has very nearly solved all the problems: still under test.

ATB

Prune

Share this post


Link to post
Share on other sites
jaberwacky

;Make sure our windows's active with pause
Func PauseMakeSureWindowIsReady()
 Do
 Sleep(100)
 Until WinActive("Window Name")
EndFunc
;Make sure our windows's active with pause
Func PauseMakeSureWindowIsReady()
 WinWaitActive("Window Name")
EndFunc

This should work too.

Share this post


Link to post
Share on other sites
prunesquallor

Thanks. I'm using the explicit Sleep statement to make sure there is a pause - there's something a bit odd going on and I want to make sure it's not a timing issue. I may be re-raising this as there may be a specific issue with ControlSend/ControlGetText. But it needs more testing.

ATB

Prune

Share this post


Link to post
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
Sign in to follow this  

×