Jump to content

AutoIt vs Expect


 Share

Recommended Posts

Hello everyone.

I'm in search of a tool to automate testing, and googling "GUI Expect for Windows" brought me here.

Before anything, I would first like to applaud the amazing work the developers have put together. I was planning on building a similar software(albeit mini-version) if I didn't find anything, and it's remarkable how much work is already done and provided for free of cost.

I've tried the software for a few days now, writing up some kiddy scripts to change my wallpaper and such. I say this because I want to avoid RTFA responses: I've read everything on this page(http://www.autoitscript.com/autoit3/docs/) except for the function descriptions(maybe I should read those too?).

I am wondering if someone could tell me if AutoIt has any Q&A+timeout+fallback mechanism that is similar to Expect. In a brief summary for those who aren't familiar with Expect, Expect is a command line automation tool that is focused on Q&A+timeout sets. You would Send() a command and Expect() some string on the screen with X seconds. Depending on if the result is Found() or not, different commands would be executed. The user can Expect() multiple options as well, so that the target program can be dynamically(yet finitely determined) managed. For example, I would Send("ssh web.site.tld") and Expect("Do you want to add this site to the permanent...") or Expect("login:"). Depending on what comes back(or timeout), I would Send() different things or terminate with an error message+log.

I see that AutoIt has functions like RunWait, but I can't seen to find an easy way for verifying the "success" of my previous command nor a configurable timeout feature should it fail. Can someone tell me if I'm overlooking something, or tell me that AutoIt isn't made for that purpose so there is no quick way of achieving that?

In example, let's say I want to

1. open up Windows Explorer

2. go to a folder

3. find a file

4. rename the file

5. create a new file with the original file's name

6. edit the file

7. close Windows Explorer

Every step of the way can fail by many different reasons, and the success must be verified before carrying out the next sequence of event. 1 can be verified by WinWait(), 2 can be verified by reading the address on Windows Explorer, 3 can be verified by reading the name of files(guessing), etc.. However, I can't seem to find a way to fail the process after a timeout, and stop the script gracefully. Can someone shed a light?

Wow this post became a lot bigger than I intended.. Thank you for your time.

Edited by CSP
Link to comment
Share on other sites

Welcome to the forum.

As far as I know, AutoIt can do all that you mentioned and more.

Below is the code to accomplish the example that you proposed.

I attempted to follow your steps.

; 1. open up Windows Explorer
;;; this is not needed unless you are making
;;; a tutorial for a human to watch

; 2. go to a folder
$FullPath = "c:\temp\testfile.txt"
$count = StringLen($FullPath) - StringInStr($FullPath, "\", 0, -1)
$Path = StringTrimRight($FullPath, $count)
$File = StringRight($FullPath, $count)

; 3. find a file
If FileExists($FullPath) Then
    ; 4. rename the file
    If Not FileMove($FullPath, $FullPath & ".old") Then
        MsgBox(0, "AutoIt", $FullPath & ".old already exists.")
        Exit
    EndIf
    ; check for the success of the file rename
    If FileExists($FullPath & ".old") Then
        MsgBox(0, "AutoIt", $File & " has been renamed.")
    EndIf
    ; 5. create a new file with the original file's name
    If FileClose(FileOpen($FullPath, 2)) Then
        MsgBox(0, "AutoIt", "A new file has been created.")
    Else
        MsgBox(0, "AutoIt", "A new file could not be created.")
        Exit
    EndIf
Else
    ; check for the reason that the file of interest was not found
    If FileChangeDir($Path) Then
        MsgBox(0, "AutoIt", $File & " does not exist.")
    Else
        MsgBox(0, "AutoIt", $Path & " does not exist.")
    EndIf
EndIf
You just happened to pick some of my weakest areas - file manipulation & error checking. I'm afraid that I'm just too much of an old DOS command line guy to work with files via AutoIt. Hopefully, the other forum members will [NOT] hurt themselves too badly as they laugh at my attempt to code your example.

If this is not what you wanted to see - if you really wanted to see AutoIt open the Windows File Explorer much like a human would - then that can be coded and checked for errors as well.

-MSP-

Edit: major typo :-(

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

...You would Send() a command and Expect() some string on the screen with X seconds. Depending on if the result is Found() or not, different commands would be executed.

The code below is not the best example of the comparison that you asked about, but it will have to do for now.

If you remove the ";" from in front of the Run function in the code below, then notepad will most likely start before the 1 second timeout in the "If" line of code. If you leave the Run line remarked out, you will see the results of the timeout:

;Run("notepad")

If Not WinWaitActive("Untitled - Notepad&quÝË ][ÝÉ][ÝËJH[ÙÐÞ
    ][ÝÉ][ÝË    ][ÝÛÝYYÝ7F'BâFÖVÇÖææW"gV÷C²¢W@¤VæD` ¥6VæBgV÷C²b333¶fgV÷C² ]¥¹]¥ÑÑ¥Ù ÅÕ½ÐíMÙÌÅÕ½Ðì¤)]¥¹5½Ù ÅÕ½ÐíMÙÌÅÕ½Ðì°ÅÕ½ÐìÅÕ½Ð, 0, 0)
If you decide to install AutoIt3, be sure and install the full (free) version of the SciTE4AutoIt3 editor. It is heavily customized for working with AutoIt3. http://www.autoitscript.com/autoit3/scite/downloads.php

-MSP-

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

One question to as might be:

Does it have to simulate the manual way(ie. the opening of explorer, and maneuver it and make it do all that stuff?), or would it be preferable to do it without trying to simulate the user interface?

Link to comment
Share on other sites

Thank you MSP. You just demonstrated that AutoIt meets my requirements beyond expectation. It seems like it's my limited knowledge of the APIs and the unfamiliar coding style that are going to be the bottleneck of my scripts. I'll be sure to try out the new SciTe editor.

Does it have to simulate the manual way(ie. the opening of explorer, and maneuver it and make it do all that stuff?), or would it be preferable to do it without trying to simulate the user interface?

Hello FreeFry,

I'm actually trying to decide that myself at this point :rolleyes:

My scripts are for automating trivial redundant tasks, and simulated UI has ups and downs:

Good

1. It's easier to debug failure cases because you can see where it fails and why

2. Although less relevant, the script won't be seen as "some magical script that does stuff and tells me that there is no error at the end" since it shows the users what's going on.

Bad

1. Slow

2. Prone to errors if users interrupt the execution by pressing random keys and such.

I think both ways are fine as long as they are properly managed. I think it's more of a design issue for me at this point, but thanks for the interesting insight.

Link to comment
Share on other sites

I will come back with some more insight on this when I get home from work. :rolleyes:

Edit:

Sorry, I'm too tired tonight, I'll see if I can manage to make an example or something tomorrow instead

Edited by FreeFry
Link to comment
Share on other sites

Oh no, I wasn't asking for your free work by any means.

I think I can manage for the time being. I appreciate your offer though.

I'll come up with some action plans later and bug you then maybe :rolleyes:

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...