Repeating Steps with Different Images

I'm writing a script at the moment to test several buttons in a dialog box, to ensure that they call the correct dialog box. Right now I have a repeat loop with an incrementing variable, ie if testNum=1, then click button x, if testNum=2 then click button Y, etc.

What I have thus far looks like this:
repeat while testNum is less than or equal to 14
	if testNum = 1 then
		set whatControl to "button1"
		clickany("XP_Button_button1")
		if anyimagefound("XP/XP_button1Verified") then
			set isTestSucessful to "YES"
		else
			set isTestSucessful to "NO"
		end if
	end if
       //if testNum=2 then...
	run WriteResults(whatControl,isTestSucessful)
	add 1 to testNum

if I continue with this method I'll have an "if testNum=X" then click (imageY) loop for each button. I'm fine with that, but I'm interested to know if there's an easier or more efficient way of doing it.

Thanks a lot, all

Allen

Comments

  • EggplantMattEggplantMatt ForumAdmin admin
    Hi, Allen:

    If you've already got buttons named "XP_Button_button1", "XP_Button_button2", ... "XP_Button_buttonx" and the corresponding "XP/XP_button1Verified", etc., then you could rewrite your code like this:
    repeat 14 times  // I'm assuming you have 14 tests
        put repeatIndex() into count
        clickany"XP_Button_button" & count
        if anyimagefound("XP/XP_button" & count & "Verified") then
            WriteResults("button" & count, "YES") 
        else
            WriteResults("button" & count, "NO") 
        end if
    end repeat
    

    So, you can build image names in the command/function calls by concatenating the number onto the basename of the images. I'm using the repeatIndex() function -- which is an automatically incremented counter that's available in all SenseTalk repeat loops -- as a shortcut.

    If you had a similar test, but you had to use images that weren't named using numbers, you could do the same sort of thing by first putting the image names into one or more lists:
    put ( "Edit button", "Save button" , "Delete button" ) into testList
    put ( "Edit panel", "Save panel", "Delete panel" ) into verifyList
    
    repeat with each button of testList 
        click button
        if imagefound( item repeatCount() of verifyList ) then
            WriteResults(button, "YES") 
        else
            WriteResults(button, "NO") 
        end if
    end repeat
    

    Let me know if you have any questions.

    - Matt
  • Aha! I'd forgotten about RepeatIndex()

    Thanks, Matt

    Allen
  • Looking at your post closer, I do have a question or two:

    put ( "Edit button", "Save button" , "Delete button" ) into testList
    put ( "Edit panel", "Save panel", "Delete panel" ) into verifyList
    
    repeat with each button of testList
    

    every example of repeat with each that I've seen always uses item. So I assume you can simply substitute item with a variable?
        click button
        if imagefound( item repeatCount() of verifyList ) then
            WriteResults(button, "YES") 
        else
            WriteResults(button, "NO") 
        end if
    end repeat
    

    I see you changed my sub-script into a function. I'm having trouble getting my head around functions (my only programming experience has been a Pascal class about 7 years ago). So, sorry for the stupid newbie questions ahead...

    First, what is the advantage to passing the data off to a function as opposed to another script? Judging by the documentation, they do nearly the same thing.

    If I got this right, I declare a function by saying
    function ReallyNifty (value1,value2,value3)
               do stuff
               do this stuff
               do that stuff
    end function
    

    To call said function, all I have to do is
    ReallyNifty(1,2,"Buckle My Shoe")
    

    If what I have above is correct, then it's the code snippets in the docu that are throwing me for a loop. Most all of them use the word ON. I've read the section for ON, but I don't understand what that actually does. could you give me a quick tutorial?

    Thanks again, for all your help, Matt

    Allen
  • EggplantMattEggplantMatt ForumAdmin admin
    FHQWHGADS wrote:
    Looking at your post closer, I do have a question or two:

    every example of repeat with each that I've seen always uses item. So I assume you can simply substitute item with a variable?

    Yes. That was a change made in Eggplant 1.5. Prior to that change, if you wanted to specify a variable for the iterator, you would do it like this:
    repeat with button = each item of testList
        put button
    end repeat
    

    If you don't specify a variable, Eggplant uses the default iterator "it":
    repeat with each item of myList
        put it
    end repeat
    
    FHQWHGADS wrote:
        click button
        if imagefound( item repeatCount() of verifyList ) then
            WriteResults(button, "YES") 
        else
            WriteResults(button, "NO") 
        end if
    end repeat
    

    I see you changed my sub-script into a function.

    Well, not exactly. I made a mistake when I edited your original script; if WriteResults is a script, then the calls to WriteResults shouldn't have parentheses around the arguments. All I really intended to do there was just remove the "Run" commands -- they're not needed when your script is local to the current suite and there aren't any spaces in the script name.
    FHQWHGADS wrote:
    I'm having trouble getting my head around functions (my only programming experience has been a Pascal class about 7 years ago). So, sorry for the stupid newbie questions ahead...

    First, what is the advantage to passing the data off to a function as opposed to another script? Judging by the documentation, they do nearly the same thing.

    They are largely interchangeable, but there are some differences. The biggest difference is that while both a command and a function *can* return a value, it is always assumed that a function does have a return value, and that value must be dealt with. So, for example, you can't have imageFound() on a line by itself; it's return value must be handled, even if it's only with a put statement.

    A script (which is really just a self-contained command handler) can also return a value, but whereas you can just treat a function like it is it's return value, e.g.:
    put 3 + theSumOfAllFears() into myVar
    

    there's an extra step needed to get the return value of a command:
    aCommandThatReturnsAValue
             put the result into myVar
    
    FHQWHGADS wrote:
    If I got this right, I declare a function by saying
    function ReallyNifty (value1,value2,value3)
               do stuff
               do this stuff
               do that stuff
    end function
    

    To call said function, all I have to do is
    ReallyNifty(1,2,"Buckle My Shoe")
    

    If what I have above is correct, then it's the code snippets in the docu that are throwing me for a loop. Most all of them use the word ON. I've read the section for ON, but I don't understand what that actually does. could you give me a quick tutorial?

    ON is the keyword that indicates a command declaration. The code in a script that appears before a command or function declaration is called an initial handler, and it's really just a special ON handler.

    I tend to use scripts and command (aka ON) handlers for actions that don't need to return a value and functions for actions that do. But the next release of Eggplant will actually have a new handler declaration that can be called as either a command or a function.
    FHQWHGADS wrote:
    Thanks again, for all your help, Matt

    Allen

    You're welcome. Thanks for the interesting questions.
  • Hola again, Matt --

    I have a follow-up question about this chunk of code:
    If you had a similar test, but you had to use images that weren't named using numbers, you could do the same sort of thing by first putting the image names into one or more lists:
    put ( "Edit button", "Save button" , "Delete button" ) into testList
    put ( "Edit panel", "Save panel", "Delete panel" ) into verifyList
    
    repeat with each button of testList 
        click button
        if imagefound( item repeatCount() of verifyList ) then
            WriteResults(button, "YES") 
        else
            WriteResults(button, "NO") 
        end if
    end repeat
    

    I can't seem to get this to work. Eggplant barfs on
    if imagefound( item repeatCount() of verifyList ) then..
    

    with this error:
    Eggplant wrote:
    No Handler Found: function repeatcount Execution Time 0:00:06 MyScript.script

    I suspected it wouldn't work, because "item" and "of" never got their syntax coloring. Doesn't seem to matter whether I use repeatindex() or repeatcount().

    Thanks again,

    Allen (who feels like the only one talking on the forum... :oops: )
  • EggplantMattEggplantMatt ForumAdmin admin
    Oops. Sorry about that. RepeatCount() should be RepeatIndex(). I always want to call it RepeatCount(), to the point that I actually created RepeatCount() helper function that just calls RepeatIndex(). I caught myself doing the same thing on another post earlier today.
Sign In or Register to comment.