Sign in to follow this  
Followers 0
slayerbulia

Script behaves unpredictebly

12 posts in this topic

Hi all. I have a strange problem, I have wrote a script for Notepad automation, and mostly it works but from time to time it just hungs on some step, for example I'm calling ControlCommand("Open", "", "Edit1", "Editpaste", "1.txt") but the Edit1 box is left empty. Does enybody have a clue what is going on?

Share this post


Link to post
Share on other sites



Hi all. I have a strange problem, I have wrote a script for Notepad automation, and mostly it works but from time to time it just hungs on some step, for example I'm calling ControlCommand("Open", "", "Edit1", "Editpaste", "1.txt") but the Edit1 box is left empty. Does enybody have a clue what is going on?

Welcome to the forum.

Please show a few* lines of code from above the ControlCommand one that you posted. My guess is that the script is moving too fast... the edit box might not be ready to accept the input.

*Or better yet, maybe you could write a small script that does most of the things that you are doing with notepad and post that. Most of the time, there are better ways to do things than scripting notepad.


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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here goes some lines of code:

WinWaitActive("Open")

ControlFocus("Open", "", "Edit1")

ControlSetText("Open", "", "Edit1", "1.txt")

Sleep(500)

ControlClick("Open","","&Open")

WinWaitActive("1.txt - Notepad")

SendKeepActive("1.txt - Notepad")

Send("test")

I have replaced ControlCommand by ControlSetText but it still leaves editbox empty from time to time.

Edited by slayerbulia

Share this post


Link to post
Share on other sites

*Or better yet, maybe you could write a small script that does most of the things that you are doing with notepad and post that. Most of the time, there are better ways to do things than scripting notepad.

I'm evaluating AutoIt as a tool for automation UI testing, and for the first task I'm doing a simple testcase with notepad.

Share this post


Link to post
Share on other sites

I could not get a fail using this code:

Opt("WinWaitDelay", 1)

For $i = 1 To 100
    WinActivate("Untitled - ")
    WinWaitActive("Untitled - ")
    WinMenuSelectItem("Untitled - ", "", "&File", "&Open...")

    WinWaitActive("Open", "Cancel")
    ControlFocus("Open", "Cancel", "Edit1")
    ControlSetText("Open", "Cancel", "Edit1", "1.txt")
    Sleep(10)
    
    If ControlGetText("Open", "Cancel", "Edit1") <> "1.txt" Then MsgBox(0, "", "error")
    WinClose("Open", "Cancel")
    WinWaitClose("Open", "Cancel")
Next
You can add to the sleep if you want to see what is happening in the "Open" window.

You can also change this line:

If ControlGetText("Open", "Cancel", "Edit1") <> "1.txt" Then MsgBox(0, "", "error") to be

If ControlGetText("Open", "Cancel", "Edit1") <> "111.txt" Then MsgBox(0, "", "error")

...if you want to see it fail the check.

Do you see a fail/error when using the loop above?


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

Share this post


Link to post
Share on other sites

I could not get a fail using this code:

I don't think what the problem is related to this particular piece of code becouse my script sometimes freezes almoust at any step without any visible reason. It just stops from time to time and wont clicks the button, or don't set text, whatever...

Btw, I running this script on the virtual machine via remote desctop connection, if this could have some influence...

Share this post


Link to post
Share on other sites

... Btw, I running this script on the virtual machine via remote desctop connection, if this could have some influence...

If you are closing the RDC session and expecting the script to continue, then the VM is locked and this might apply: #571221

Put the line that Zedna mentioned near the top of your script.


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

Share this post


Link to post
Share on other sites

Do you see a fail/error when using the loop above?

Then I run this test case ten times it would fail 4-5 times.

Func RunTestCase()

;Start notepad and send some text

WriteToFile("C:\Abulat\log.txt","Notepad testcase started")

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

Opt("SendKeyDelay",0)

SendKeepActive("Untitled - Notepad")

Send("Text")

;File Open

WinMenuSelectItem("Untitled - Notepad", "", "&File", "&Open")

ConsoleWrite("Waiting for save dialog" & @CRLF)

IF WinWaitActive("Notepad", "",500) == 0 Then

WriteToFile("C:\Abulat\log.txt","Notepad testcase failed")

Return 1

EndIf

ConsoleWrite("Clicking on save" & @CRLF)

SendKeepActive("Notepad")

;ControlClick("Notepad","","&Save")

Send("!s")

;Save As

;WinWaitActive("Save As", "Save")

IF WinWaitActive("Save As", "",500) == 0 Then

WriteToFile("C:\Abulat\log.txt","Notepad testcase failed")

Return 1

EndIf

ControlCommand("Save As", "Save", "Edit1", "Editpaste", "abtest.txt")

;Sleep(500)

ControlClick("Save As", "Save","&Save")

Sleep(1000)

;If file already exists overwrite

If WinActive("Confirm Save As","") Then

ConsoleWrite("Confirm Save As" & @CRLF)

ControlClick("Confirm Save As", "","&Yes")

EndIf

ConsoleWrite("Confirm Save As passed" & @CRLF)

;Open File

ConsoleWrite(" waiting for open window" & @CRLF)

IF WinWaitActive("Open", "",500) == 0 Then

WriteToFile("C:\Abulat\log.txt","Notepad testcase failed")

Return 1

EndIf

;WinWaitActive("Open")

ControlFocus("Open", "", "Edit1")

ControlSetText("Open", "", "Edit1", "abtest.txt")

Sleep(10)

;ControlCommand("Open", "", "Edit1", "Editpaste", "abtest.txt")

If ControlGetText("Open", "", "Edit1") <> "abtest.txt" Then

ConsoleWrite(" closing process" & @CRLF)

WriteToFile("C:\Abulat\log.txt","Notepad testcase failed")

WinClose("Open", "Cancel")

WinWaitClose("Open", "Cancel")

ProcessClose("notepad.exe")

Return 1

EndIf

ConsoleWrite(" file name passed" & @CRLF)

ControlClick("Open","","&Open")

ConsoleWrite(" waiting for a file to open" & @CRLF)

IF WinWaitActive("abtest.txt - Notepad", "",500) == 0 Then

WriteToFile("C:\Abulat\log.txt","Notepad testcase failed")

Return 1

EndIf

;WinWaitActive("abtest.txt - Notepad")

SendKeepActive("abtest.txt - Notepad")

Send("test")

;File | New | Save? Yes

Send("^n")

IF WinWaitActive("Notepad", "",500) == 0 Then

WriteToFile("C:\Abulat\log.txt","Notepad testcase failed")

Return 1

EndIf

;WinWaitActive("Notepad","",1000)

Send("!s")

;Exit Notepad

ProcessClose("notepad.exe")

WriteToFile("C:\Abulat\log.txt","Notepad testcase passed")

Return 0

EndFunc

#EndRegion TestCase

Share this post


Link to post
Share on other sites

I'm evaluating AutoIt as a tool for automation UI testing, and for the first task I'm doing a simple testcase with notepad.

Basic UI manipulation can be learned using the tutorial in the helpfile. Have you taken a look at the section for installing Winzip? I referenced that to write my first script for application removal and I didn't have a clue about scripting. Some would say I still dont... :)


Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Share this post


Link to post
Share on other sites

Then I run this test case ten times it would fail 4-5 times. ~~~

Ya could a told me it was for Vista :-)

Below is some code that worked for me - without changing too much of your original structure. I ended up hard coding some paths because of a problem that I had with my Vista Business Virtual Machine locking up while I was testing the code with only 57MB of RAM for Vista. There is nothing special about 57MB, that is just where the slider landed. I did not test it via RDC to the VM... you can try that... but you have to keep the RDC session going while the script is running.

;for Vista
Opt("SendKeyDelay", 0)
Opt("TrayIconDebug", 1)

$FileHandle = FileOpen("c:\temp\log.txt", 2)

For $i = 1 To 500
    ;Start notepad and send some text
    FileWriteLine($FileHandle, $i & " Notepad testcase started")
    Run("notepad.exe")
    WinWaitActive("Untitled - Notepad")
    SendKeepActive("Untitled - Notepad")
    Send("Text")
    WinWait("Untitled - Notepad", "Text")

    ;File Open
    WinMenuSelectItem("Untitled - Notepad", "", "&File", "&Open")
    ConsoleWrite("Waiting for save dialog" & @CRLF)

    If Not WinWaitActive("Notepad", "&Save", 30) Then
        FileWriteLine($FileHandle, $i & " Notepad testcase failed - Waiting for save dialog")
        Exit
    EndIf
    ConsoleWrite("Clicking on save" & @CRLF)
    SendKeepActive("Notepad", "&Save")
    ;ControlClick("Notepad","","&Save")
    Send("!s")

    ;Save As
    If Not WinWaitActive("Save As", "&Save", 30) Then
        FileWriteLine($FileHandle, $i & " Notepad testcase failed - Save As")
        Exit
    EndIf
    ControlCommand("Save As", "Save", "Edit1", "Editpaste", "abtest.txt")
    WinWait("Save As", "abtest.txt")
    ControlClick("Save As", "Save", "&Save")

    ;If file already exists overwrite
    If FileExists("c:\temp\abtest.txt") Then
        WinWait("Confirm Save As", "")
        ConsoleWrite("Confirm Save As" & @CRLF)
        For $ii = 1 To 1000
            Sleep(99)
            If ControlCommand("Confirm Save As", "", "&Yes", "IsEnabled", "") Then ExitLoop
            If $ii = 999 Then
                ConsoleWrite(" Confirm Save As - 'Yes' did not 'enable' in time" & @CRLF)
                Exit
            EndIf
        Next
        For $ii = 1 To 1000
            Sleep(99)
            If ControlFocus("Confirm Save As", "", "&Yes") Then ExitLoop
            If $ii = 999 Then
                ConsoleWrite(" Confirm Save As - 'Yes' did not 'focus' in time" & @CRLF)
                Exit
            EndIf
        Next
        ControlClick("Confirm Save As", "", "&Yes")
        ConsoleWrite("Confirm Save As passed" & @CRLF)
    EndIf

    ;Open File
    ConsoleWrite(" waiting for open window" & @CRLF)
    If Not WinWaitActive("Open", "", 30) Then
        FileWriteLine($FileHandle, $i & " Notepad testcase failed -  waiting for open window")
        Exit
    EndIf
    ;WinWaitActive("Open")
    For $ii = 1 To 1000
        Sleep(99)
        If ControlCommand("Open", "", "Edit1", "IsEnabled", "") Then ExitLoop
        If $ii = 999 Then
            ConsoleWrite(" Confirm Save As - 'Yes' did not 'enable' in time" & @CRLF)
            Exit
        EndIf
    Next
    $CFr = ControlFocus("Open", "", "Edit1")
    $CSTr = ControlSetText("Open", "", "Edit1", "abtest.txt")
    ConsoleWrite("  " & $CFr & " < return from ControlFocus" & @CRLF)
    ConsoleWrite("  " & $CSTr & " < return from ControlSetText" & @CRLF)
    WinWait("Open", "abtest.txt")

    If ControlGetText("Open", "", "Edit1") <> "abtest.txt" Then
        ConsoleWrite(" closing process" & @CRLF)
        FileWriteLine($FileHandle, $i & " Notepad testcase failed - ControlSetText")
        WinClose("Open", "Cancel")
        WinWaitClose("Open", "Cancel")
        ProcessClose("notepad.exe")
        Exit
    EndIf

    ConsoleWrite(" file name passed" & @CRLF)
    ControlClick("Open", "", "&Open")

    ConsoleWrite(" waiting for a file to open" & @CRLF)
    If Not WinWaitActive("abtest.txt - Notepad", "", 30) Then
        FileWriteLine($FileHandle, $i & " Notepad testcase failed -  waiting for a file to open")
        Exit
    EndIf
    ;WinWaitActive("abtest.txt - Notepad")
    SendKeepActive("abtest.txt - Notepad")
    Send("test")
    WinWait("abtest.txt - Notepad", "testText")

    ;File | New | Save? Yes
    Send("^n")

    If Not WinWaitActive("Notepad", "", 30) Then
        FileWriteLine($FileHandle, $i & " Notepad testcase failed - save/yes/no")
        Exit
    EndIf
    Send("!s")

    ;Exit Notepad
    ProcessClose("notepad.exe")
    FileWriteLine($FileHandle, $i & " Notepad testcase passed")
Next

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

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  
Followers 0