Can I create a function to replace if/then/else code?

I find I am writing lots of code that looks like:

if ImageFound(15.0,"dir/image") then
	log "Expected image was found, so clicking: " & "dir/image"
	click "dir/image"
else
	logError "Expected image not found: " & "dir/image"
	return
end if

Is there a way to create a function that I could pass three variables to that would allow me to reduce the amount of code being generated? I am thinking that the time, the image, and the action (click in this example) could all be the variables I pass in. Although not necessarily based on anything real, here is what I am envisioning as a replacement calling a function:

IfThenElse (15, dir/image, click)

or

IfThenElse (30.0, dir, DoubleClick)

Is something like this possible? If so, can you help me understand how to implement it?

Hello,

You can do this by creating a separate script. Call this new script “IfThenElse”. In the new script, you will have the below code:

Params ImageTime, ImageName, Action

if imagefound (ImageTime, ImageName) 
then
	log "Expected image was found, so clicking: " & ImageName
	do action && ImageName
else
	LogError "Expected Image Not Found: " & ImageName
end if 

Then, in your main script, you will call IfThenElse.script with code like this:

//Do Something
IfThenElse 15.0, "Image1", click
//Do Something
IfThenElse 30.0, "Image2", doubleClick

Make sure that the new script is named “IfThenElse”, or the code above will not call it properly!

The advantage of creating a separate script is that then you can use that bit of code from any other script in the suite (and in other suites, as long as the suite that holds the IfThenElse script is added as a helper or into the InitialSuites).

If (for whatever reason) you would rather, his can also be done within a single script. To do this, you would just call the code in the same way in the main script, but instead of having a separate script, you would have a handler. Your code would then look like this:

//Do Something
IfThenElse 15.0, "Image1", click
//Do Something
IfThenElse 30.0, "Image2", doubleClick

//This is the handler being called (at bottom of script):
to IFThenElse ImageTime, ImageName, Action
	if imagefound (ImageTime, ImageName) 
	then
		log "Expected image was found, so clicking: " & ImageName
		do action && ImageName
	else
		LogError "Expected Image Not Found: " & ImageName
	end if 
end IfThenElse

I hope that helps!

-Elizabeth

Thanks for the speedy reply Elizabeth. :smiley:

Actually, I replied too soon. After trying this out a bit I am finding that I get an error consistently. Here is the test code in question. The first script has nothing in it but:

IfThenElse 30, "monitor/tm_monitor", click

The IfThenElse.script file contains:

Params ImageTime, ImageName, Action 

if imagefound (ImageTime, ImageName) 
then 
	log "Expected image was found, so clicking: " & ImageName 
	do action && ImageName 
else 
	LogError "Expected Image Not Found: " & ImageName 
end if

When I run the first script, I get the following:

18-May-12 18:49:07	START		Running 1.script
18-May-12 18:49:09	imagefound	monitor/tm_monitor	found at (445, 146)
18-May-12 18:49:09	log		Expected image was found, so clicking: monitor/tm_monitor
18-May-12 18:49:10	FAILURE		STInvalidNumberException Value is not a number: 'monitor'
Execution Time 0:00:02 1.script

What is not working as expected here?

This code will work as written when the image name is a single word. But for your case (and in general) the do command should be changed slightly, either to put quotes around the image name, like this:

    do action && quote & ImageName & quote

or (even better) to use the ImageName variable itself (rather than its value) in the command that will be executed by ‘do’:

    do action && "ImageName"

Doing it this way will also work if the image that is passed in is an image property list or a text property list as well as an image name.

Thanks Doug. I will give it a shot.

One final thought: Since you’re doing a successful image search before the action (with the imageFound() function), you could further simplify and save eggPlant from searching the screen for the same image a second time by using the foundImageLocation() function, like this:

  do action && "foundImageLocation()"

Good luck! :slight_smile:

Makes perfect sense. I will give it a whirl! Thanks again.

what if ImageName whould be Text: “some text”
how can i pass it?

That should work fine, too. Any place that you can use an image name to do a search, you can use an image property list or text property list like (Text: “some text”) instead.

In the case of your called script, the ImageName parameter will be a name or a property list (whichever is passed in by the calling script). It should all just work, as long as you use one of these forms of the do command recommended earlier – either:

do action && "ImageName"

That will directly pass along whatever ImageName value was received to the action command.

Or of course this form will work fine too:

do action && "foundImageLocation()"

I thought it would be worth posting where this all finally ended up for us.

  • We recently added the “MoveTo (1,1)” because we found a few examples in our UI where we needed to make sure the mouse was not in the lower right hand (default) corner of the screen because that sometimes caused a sliding dialog to appear when it was not wanted. This was an easy way to make sure whatever we were looking for was not accidentally hidden.

  • We added the Waitfor because we wanted the Image Doctor to be triggered if an image was not found for some reason.


Params ImageTime, ImageName, Action 

MoveTo (1,1)
if imagefound (ImageTime, ImageName) 
then 
	log "Expected image was found, so using: " & ImageName 
	do Action && "foundImageLocation()"
	MoveTo (1,1)
else 
	LogError "Expected Image Not Found: " & ImageName
	WaitFor "1.0", ImageName
	return
end if

We use this little script a LOT!