Use values read in from a file as clickable text?

I am working on a variety of scenarios where it would be great to be able to read in a set of data from an external file and use it to select various items in the SUT. I have thrown together some scratch code to show what I mean. Is something like this possible? If it is, is there a better way to do this?
put zero into MachineCount

repeat with MachineNames = each line of file "c:\temp\MachineNames.txt"

add 1 to MachineCount

--------------------------
put item MachineCount of MachineNames into MachineNameToUse
or
set ( MachineNameToUse ) to item (MachineCount) of MachineNames
--------------------------

if ImageFound(text:"MachineNameToUse", textPlatform:"Generic") then
	log "MachineNameToUse " & "was found, so selecting it."
	click (text:"MachineNameToUse", textPlatform:"Generic")
else
	ScrollWheelDown 10
		if ImageFound(text:"MachineNameToUse", textPlatform:"Generic") then
			log "MachineNameToUse " & "was found, so selecting it."
			click (text:"MachineNameToUse", textPlatform:"Generic")
		else
			ScrollWheelDown 10
			if ImageFound(text:"MachineNameToUse", textPlatform:"Generic") then
				log ""MachineNameToUse " & "was found, so selecting it."
				click (text:"MachineNameToUse", textPlatform:"Generic")
			else
				logError "MachineNameToUse " & " not found."
			return
			end if
		end if
end if

keydown ControlKey

end repeat

Click "addbutton"

keyup ControlKey

Please be gentle... I am pretty new to this. :D

Comments

  • EggplantMattEggplantMatt ForumAdmin admin
    Your code is mostly correct, but is somewhat dependent on what the contents of your file are. If each line of the file contains a machine name, then you don't need to use the counter (you actually rarely need a counter variable in eggPlant anyway -- there's one built in) -- your "repeat with each line" code is already returning individual machine names. From what I can deduce from your code, it should work if rewritten like this:
    Log "Read in all of the Machine Names in c:\temp\MachineNames.txt"
    
    repeat with MachineName = each line of file "c:\temp\MachineNames.txt"
    
          repeat while not ImageFound(text:MachineName) and not ImageFound("Bottom_of_page_indicator_image")
             ScrollWheelDown 10
          end repeat
    
          if ImageFound(text:MachineName) then
             log MachineName && "was found, so selecting it."
             click foundImageLocation()
          else
              logError MachineName && "not found."
          end if
    
          keydown ControlKey
    
          Click "addbutton"
    
          keyup ControlKey
    
    end repeat
    
    If you're using the OCR feature, you'll actually want to constrain the search a bit if you can by identifying the area of the screen that you expect to find the machine name in. Since you are scrolling, that may be difficult, unless you are scrolling something like a dropdown list. You can constrain the region to search by specifying two images that represent the upper-left corner and lower-right corner of the area to search:
    if imageFound(text:MachineName, searchRectangle:("UL_image", "LR_image"))
    

    The OCR search is not very fast when searching the entire screen -- it has to look at every pixel and determine whether that pixel is part of a character and then whether that character is part of a word, using context and dictionary lookups to try to determine exactly what that word should be. It's a very processor intensive search, so if you can narrow the area it is looking at, it will be quicker.

    As I mentioned, you rarely need to declare a counter variable when working with a loop in SenseTalk. There is a built-in variable called the repeatIndex which can be accessed in either of two ways:
    repeat with each item of someList
         put the repeatIndex
         put repeatIndex()
    end repeat
    
    Either the variable reference or the function call will return the current iteration of the loop, and since both the repeatIndex and the list indexing start at 1, the repeatIndex always corresponds to the item number.
  • Thanks for the speedy reply and advice Matt.

    When I try to use the following:
    repeat while not ImageFound(text:MachineName, searchRectangle:("upper_left_boundary", "lower_right_boundary"))
    		ScrollWheelDown 10
    	end repeat
    

    I get:

    Selection Failed in 0:00:02 Invalid Image Image has no searchable pixels

    I verified that the upper_left_boundary & lower_right_boundary images are not the problem.

    Then, I went back and ran just the following code to see what was being read in:
    repeat with MachineName = each line of file "c:\temp\MachineNames.txt"
    	Put MachineName
    end repeat
    

    and (roughly) got:
    machine1 ┌ control-M ┘
    machine2 ┌ control-M ┘
    machine3 ┌ control-M ┘
    machine4 ┌ control-M ┘
    machine5
    

    Any ideas?
  • By the way, based on your code above, I think this is basically what I was looking to do:
    keydown ControlKey
    repeat with MachineName = each line of file "c:\temp\MachineNames.txt"
    	repeat while not ImageFound(text:MachineName, searchRectangle:("upper_left_boundary", "lower_right_boundary"))
    		ScrollWheelDown 10
    	end repeat
    	if imageFound(text:MachineName) then
    		log MachineName && "was found, so selecting it."
    		click foundImageLocation()
    	else
    		logError MachineName && "not found."
    	end if
    end repeat
    Click "addbutton"
    keyup ControlKey
    

    Now if it just worked! :(
  • EggplantMattEggplantMatt ForumAdmin admin
    There is a bit of code that will get rid of those line endings, but the latest version of eggPlant (v11.02) will actually strip those out as it reads your text file, so I'd recommend that you start by downloading and installing that. Then make sure that your default text platform (under Eggplant > Preferences > Text) is set to the Generic OCR platform. Then try running the code again and if you're still having issues, contact us by e-mail at the support address with a screenshot of the UI you are running against and copies of the images you are using to set your search rectangle.
  • EggplantMattEggplantMatt ForumAdmin admin
    I don't think you want to do what this code is doing. Having the control key held down the whole time that you are doing these other actions is likely to have some unwanted side effects. I'm not sure what using the scroll wheel will do when the control key is held down. When do you actually want the control key held down (what does it do for you)? And when do you want to click the Add button? Can you just describe the process in plain english without using code? That usually makes it more obvious what you need the script to do. The code you posted holds the control key down the whole time, only clicks the Add button once after you've gone through the whole list of machine names, and then releases the control key.

    If you're trying to select all of these items in a list and then add them with one button click, I'd rework the code like this:
    repeat with MachineName = each line of file "c:\temp\MachineNames.txt"
       repeat while not ImageFound(text:MachineName, searchRectangle:("upper_left_boundary", "lower_right_boundary"))
          ScrollWheelDown 10
       end repeat
       if imageFound(text:MachineName) then
          log MachineName && "was found, so selecting it."
          keydown ControlKey
          click foundImageLocation()
          keyup ControlKey
       else
          logError MachineName && "not found."
       end if
    end repeat
    Click "addbutton"
    
  • You guessed correctly.

    I want to select the whole set of machines from the TXT file in a wizard window before using the button that includes only those machines in the set that actions will be executed against. Your reworking of the code looks like it does exactly what I intended. Thanks!

    I am downloading the newest version of eggPlant now and will let you know how it works out.
  • The new version of eggPlant did help as expected.

    However, I had considerable problems getting the code to work as written above. For some reason the mouse scroll command would not execute on the SUT. I also could not get it to keep all of the selected machines between loops (Via holding down the CTRL key when clicking on items.). It would lose the first one when it clicked on the second one for example.

    I also wanted to be able to search both up and down the available list, so I wrote a new version that I will include below. It may not be the final version, but it does work as expected! It reads in the first machine from the file, looks for it, clicks on it if it finds it, if it does not find it it scrolls down if it can, looks for it, clicks on it if it finds it. Well, hopefully you can see from the code:
    repeat with MachineName = each line of file "c:\temp\MachineNames.txt"
    	Put MachineName
    	if ImageFound(text:MachineName, searchRectangle:("upper_left_boundary", "lower_right_boundary")) then
    		log MachineName && "was found, so selecting it."
    		click foundImageLocation()
    		Click "addbutton"
    	else
    		if ImageFound("downarrow") then
    			Click foundImageLocation()
    			if ImageFound(text:MachineName, searchRectangle:("upper_left_boundary", "lower_right_boundary")) then
    				log MachineName && "was found, so selecting it."
    				click foundImageLocation()
    				Click "addbutton"
    			else
    				logError MachineName && "not found."
    			end if
    		else
    			if ImageFound("uparrow") then
    				Click foundImageLocation()
    				if ImageFound(text:MachineName, searchRectangle:("upper_left_boundary", "lower_right_boundary")) then
    					log MachineName && "was found, so selecting it."
    					click foundImageLocation()
    					Click "addbutton"
    				else
    					logError MachineName && "not found."
    				end if
    			else
    				logError MachineName && "not found."
    			end if
    		end if
    	end if
    end repeat
    

    By the way, my new code runs considerably quicker than the version that was doing two OCR checks (one to see if the image was there and another find and to click on it). It also allowed checking through the entire list of available machines by "scrolling" up or down as needed depending on the current state of the list per value read from the TXT file.

    I would love to hear what you think.
  • EggplantMattEggplantMatt ForumAdmin admin
    Its important to be aware that the scrollwheel (even when working directly on a machine) only works if the mouse cursor is within the area that you want to scroll. So usually your script needs to move the cursor into that area before you send the scroll wheel command.
  • Its important to be aware that the scrollwheel (even when working directly on a machine) only works if the mouse cursor is within the area that you want to scroll. So usually your script needs to move the cursor into that area before you send the scroll wheel command.

    Ah... I was only making sure focus was on the window I was using, not the specifc area where the choices were beign presented. Thanks for the info.
Sign In or Register to comment.