[eggPlant Functional] Useful SenseTalk Functions

A little fun here today, its Friday after all.

10.5.x Leopard has a much improved and understandable Text-To-Speech system and a new voice called Alex. Alex can and will say stupid (and useful) things to you and others if you just command it. If you dare, man page the ‘say’ command in the Terminal/iTerm.app vt100 environment.

Here we have a not so big challenge, announcing the time and suggesting doing something else for a bit is a good thing for those of us that sit at the computer far far far too long at spells. With the healthy problems computer professionals have, RSS, pooling of blood in the extremities (particularly the legs), shoulder problems and even eye strain issues this little ditty does the job well to remind me to shower :). I laugh, and think about how it is a good thing its just a computer telling me to take a rest and not some nagging impotent boss from “The Office” or “The Drew Carey Show”.

get the time

if the hour is zero then  -- announce for first hour of day what day it is.
 shell "say Today is " & the long date
 put "00" into chars 1 to 2 of it  -- correct a bug with say command
end if

shell "say The" && \
  any item in ( "bleeping", "freaking", "", "darn", "lovely" ) && \
  "time is" && it && \
  any item in ( ", so", ", " ) && \
  any item in ( "wake up", "make a change" ) && \
  any item in ( "and", "or", "exclusively or", "in a boolean fashion and" ) && \
  any item in ("possibly", "maybe", "definitely") && \
  " you" && \
  any item in ("should", "would", "could") && \
  any item in ( "think about", "contemplate, to consider, to evaluate, to decide on", "deciding on" ) && \
  any item in ( "taking", "doing", "exiting and doing" ) && \
  any item in ( "a cup of" & \
  any item in ( "java", "coffee", "tea", "soda", "water" ), "a walk", "some sleep", "a bit of rest", "a" && \
  any item in ( "hot", "cold", "warm", "boiling", "freezing", "" ) && \
  "shower", "dinner", "lunch", "breakfast", "a meal" )

You could include this in an .st or .script file, and direct either your embedded ST application to run it, or even talk to Thoughtful Software (http://www.thougthful.com) about other means of executing SenseTalk (http://www.sensetalk.com) scripts from the CLI. I run it as an .st script, and let Cronnix (http://www.abstracture.de/projects-en/cronnix) do the scheduling.

Either way this is a good and fun way to remind us that we need to take a few mins every 30 to 60 mins and stretch and exercise something other than our brains.

Try this on for size:

shell "say super calla fragia listic expee alla docious"

And finally you can leverage all this for announcements that change due to failures or success in your scripts and environment.

try to do Something
sayRunStatus the exception

try to do "Something(goAheadAndFail)"
sayRunStatus the exception

to sayRunStatus  e
 if e.name is empty then
  shell "say" && "SUCCESS, your scripts works fine!"
 else
  Log  e.name && "-" && e.reason
  shell "say" && "Uh oh, you have encountered a problem."
  shell "say The Exception information follows"
  wait 1 second
  shell "say" && e.name && e.reason
 end if
end sayRunStatus

to something  trigger  -- do nothing but throw an exception
 if trigger is not empty then
  Throw "Some Type of Exception", \
    "Please place exception information here, which should be detailed."
 end if
 return "goodness"
end something

Enjoy, and have a great time with your SenseTalk’ing (literally)

I do all kinds of stuff generating temp files instead of using dynamic variable creation. These serve purposes like storing intermediate variable or conditions/results that I want to track. This can be done with Log, LogWarning or even LogError and exception handling… However, often my needs are for template files that will be merged later (being generated programmatically rather than statically). This leads to the need of temporary file names and generating those names.

Once again, a simple but powerful solution is presented. You’ll notice also a clean and simple comment header that should be employed whenever you are moving scripts and handlers into your libraries. These headers are not only a best practice in software engineering, but also in automation and functional programming/scripting. You can use the Doug Simons radioactive EggDoc to run against this which will generate a nice clean html file for your documentation efforts, you are doing documentation of your ‘agile’ efforts I hope.

(**
  Returns a unique file name that is almost uarenteed not exist.
  Will try up to 10000 times to create a filename that is unique
  with a large spread of 90 million possibilities.  Chances of this
  routine not creating a unique file name are very remote

@param  dir     optional directory, defaults to "/tmp/"
@param  prefix  the front, defaults to "TempFile"
@param  suffix  end which is often left empty.
@param  ext     the file name extension, defaults to empty.
@returns        a unique full path name
**)

params  dir, prefix, suffix, ext

set numOfAttempts to 10000
set defaultDir to "/tmp/"

if there is not a folder dir then
 LogWarning  "Passed parameter 'dir' invalid, passed as '" & \
   dir & "'... doesn't exist."
 set dir to defaultDir
 LogWarning "The parameter is now defaulted to '" & dir & "'"
else if dir is empty then
 set dir to "/tmp/"
end if

if last char of dir is not "/" then put "/" after dir
if prefix is empty then set prefix to "TempFile"
if ext is not empty and first char of ext is not "." then \
  set ext to "." & ext

repeat numOfAttempts times
 set path to dir & prefix & \
   random(10000000, 99999999) & suffix & ext
 if there is not a file path then return path -- exit when unique
end repeat

-- as a last resort, we'll toss an exception if we are unable
-- to generate a unique filename numOfAttempts times
Throw  "Unique File Name Creation Exception", \
  "Was not able to create a file name that was unique trying" && \
  numOfAttempts && "times."

Some example calls to this and returned values follow…

put UniqueFileName("/tmp")
/tmp/TempFile78212738
put UniqueFileName("/tmp")
/tmp/TempFile61775655
put UniqueFileName("/tmp", Todd)
/tmp/Todd26460678
put UniqueFileName("/tmp", Todd, Nathan)
/tmp/Todd18014018Nathan
put UniqueFileName("/tmp", Todd, Nathan, abc123)
/tmp/Todd24873800Nathan.abc123
put UniqueFileName("/tmp", , , rtf)
/tmp/TempFile52695153.rtf

And of course for those folks that want to use the underpinnings of the hosting operating system, in my case currently OS X, you can use the ‘secure’ means of mkdir and mktmp using the bash shell $RANDOM generator. To read more on this, you can turn to http://wooledge.org:8000/BashFAQ/062 which has a terse but good example set.

You could easily make the above SenseTalk handler deal with a unique folder name generated by changing the

if there is not a file path then

to the equivalent for a folder

if there is not a folder path then

and that effort to make it work is left up to the astute SenseTalker. Good luck, and happy scripting.

And of course a final note. If you want to avoid looping for uniqueness you could also use milliseconds, or a date-time combination to stamp each file as unique. This technique works close to 100% of the time since time is a continuum which if using small enough time stamp increments (as in milliseconds) you will not likely have a single command execute in less than that time to execute in SenseTalk (at least not until we get to 20-30Ghz mega count core computers, and we’ll worry about that later.)

Just for fun exercise today. I was reading along on metrics in C++ and C for hysterically historic meloncholic reasons today. And what did I find in the SQE PowerPass access (www.sqe.com) area a little article on the right way to handle checking for items in a iterator before iterating over the iterator (what a mouthful). I found this almost self folding, solving itself as stated which is often a hint of unnecessary complexity.

For our example, lets start with some variable setting for the simple elapsed time testing…

set aList to ()
set iterations to 10 ^ 5

Then we’ll execute a simple repeat loop with some time wrapper script around it for reporting purposes.

set sTime to now  -- the really wrong way to do things.
repeat iterations times
	if the number of items in aList is not zero then
		repeat with each item in aList
			do somethingWonderful
		end repeat
	end if
end repeat
put "Elapsed Time: " & now - sTime

and then execute a simple repeat loop with some time wrapper code around it for reporting. What we find when we alter the object checking (either for existance, or contents of items, or emptiness) is significantly different timings. Some cases are useful to note…

set sTime to now  -- the possibly right way to do things.
repeat iterations times
	if aList is not an empty list then
		-- [[ ... loop removed for brevity ... ]]
	end if
end repeat
put "Elapsed Time: " & now - sTime && seconds


set sTime to now  -- the maybe right way to do things
repeat iterations times
	if aList is not () then
		-- [[ ... loop removed for brevity ... ]]
	end if
end repeat
put "Elapsed Time: " & now - sTime && seconds


set sTime to now  -- the right way to do things
repeat iterations times
	-- [[ ... loop removed for brevity ... ]]
end repeat
put "Elapsed Time: " & now - sTime && seconds

The point of this exercise is not to point out the flaws, weakeness or particular right or wrong way to do things… But more of a method of understanding your needs. In this case, we don’t need to check for the object being empty or void of items at all, due to the nature of the repeat loop inherintly doing that for us by default. A loop over any chuck in a container which has non of those chunk types in it will result in the fastest execution with no checking for that chuck type at all.

Here are the numbers I got on an iMac with 4 gigs of RAM, 2.0 ghz system running SenseTalk in a hosted application called Eggplant. The numbers are clear on the winning approach.

Thu, 5/8/08 9:56:21 PM	START		Running BenchmarkContainerChecking.script
Elapsed Time: 6.357546 seconds
Elapsed Time: 5.063503 seconds
Elapsed Time: 2.60024 seconds
Elapsed Time: 0.5716 seconds
Thu, 5/8/08 9:56:35 PM	SUCCESS		Execution Time 0:00:14 BenchmarkContainerChecking.script

Experiment, write your own metric and timing scripts. Just like a master mechanic learns the tools and language of the trade, it is in your best interest to do the same. Read at least once a month the entire SenseTalk Reference Manual, there are so many tidbits of goodness tucked away in it that I still find fun to discover weekly.

The need arose last week to click on all instances of a image element, as they become visible as a result of elements moving out of the way. The power of recursion (a technique where a function calls itself with possibly terminal values for call depth control) is what first came to mind. Many problems can be solved recursively.

params  image

if image is empty then set image to "close_window"
repeat with each loc in EveryImageLocation(image)
	Click  loc
end repeat

if ImageFound(1, image) then CloseAll

return

You also have to be careful that this can loop forever if the elements eventually don’t go away from the screen. For intance, if you were to supply an image element of the zoom button on a window title bar, this loop would go forever until either the system ran out of memory and Eggplant or SenseTalk would exceed the default recursion count limit.

You can take a look at how to tweek the call depth limit in Eggplant.

http://www.redstonesoftware.com/phpBB2/viewtopic.php?p=1931

Developing vocabulary in scripting system for automation is often part of what we all do, eventually establishing some standards in our organization to facilitate effective communications of ideas, quality and easy of maintainance of scripts. Externalizing script creation and management, decoupling if you will, is often part of the keyword driven approach in test automation.

Eggplant has build in functions and vocabs, which form the foundation to a much richer custom solution for any organization. In the search to describe a limiting search region, I have come up with a fairly flexible “LimitSearchTo regionName, additionalRegions” handler, with its own help/usage system (for you unix heads)…

properties
 help: "Usage: LimitSearchTo  [ Menus | Dashboard | Content | Help ], optionalProperties"
end properties

params  regionByName, optionalProperties

if regionByName is "help" then
 put my help
 return
end if

set regionProperties to ( "All": the remoteScreenRectangle, "Menus": Menus, "Dashboard": Dashboard, "Content": Content )
if optionalProperties is a property list then \
  add properties optionalProperties to regionProperties

if regionByName is in the keys of regionProperties then
 get regionProperties.(regionByName)
 if it is not a rectangle then get everyImageLocation(it)
 set the searchRectangle to it  -- should be two image locations
end if

Without belaboring the details, fundamentally this handler embraces a LOT of SenseTalk and objects. You have initial properties of which is a static help string property by name ‘help’. This allows you to quickly ask the object/script for some help in using itself. This string is internalized, so if you change the script’s behaviour you also will need to be in the habit of changing/updating both the help property and the EggDoc comments. I encourage as a standard in house to use EggDoc comments for automatic documentation creation along with test runs being part of your organizations build cycles (daily or more often).

You’ll also notice this handler does some fun stuff with a mix of data types. We dynamically get the screen rectangle and use it for the “All” directive. We also make an assumption that the name of the region is a collection of 2 images likely with offsetting corners of a rectangle of interest. The optionalProperties also gives you the flexibility to add dynamically a list of regions with names or rectangles on the fly. This gives you the added power to expand your vocabulary dynamically as projects and practices demand. Nothing is static, so developing your scripts to be open ended with well defined behavior is a good idea in all your efforts.

Here is some demonstration script to drive validation of the LimitSearchTo handler:

-- Demonstrate vocabulary development...  Each 'region' is a two or more collection
-- of images, or a reference to a rectangle (within the LimitSearchTo handler itself)
set additionalRegions to ( region1: (1,2,3,4), region2: (4,5,6,7) )
repeat with each aRegionName in (( "Menus", "Dashboard", "Content", "All", "Help" ) \
  &&& the keys of additionalRegions )
 LimitSearchTo  aRegionName, additionalRegions
 if region is not in ( "All", "Help" ) then LimitSearchTo  All
end repeat

Please take this as a base for your own vocabulary development efforts, and comment about what you have found to be useful. Below is a run result output for the following exercise script for LimitSearchTo…

Wed, 5/21/08 8:51:34 AM	START		Running Selection from Sandbox.script
Wed, 5/21/08 8:51:34 AM	everyimagelocation	Menus	found 2 images
Wed, 5/21/08 8:51:34 AM	set		SEARCHRECTANGLE = ((5,245),(199,714))
Wed, 5/21/08 8:51:34 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Wed, 5/21/08 8:51:35 AM	everyimagelocation	Dashboard	found 2 images
Wed, 5/21/08 8:51:35 AM	set		SEARCHRECTANGLE = ((2,113),(1024,204))
Wed, 5/21/08 8:51:35 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Wed, 5/21/08 8:51:35 AM	everyimagelocation	Content	found 2 images
Wed, 5/21/08 8:51:35 AM	set		SEARCHRECTANGLE = ((201,206),(1020,713))
Wed, 5/21/08 8:51:35 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Wed, 5/21/08 8:51:35 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Wed, 5/21/08 8:51:36 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Usage: LimitSearchTo  [ Menus | Dashboard | Content | Help ], optionalProperties
Wed, 5/21/08 8:51:36 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Wed, 5/21/08 8:51:36 AM	set		SEARCHRECTANGLE = (1,2,3,4)
Wed, 5/21/08 8:51:36 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Wed, 5/21/08 8:51:36 AM	set		SEARCHRECTANGLE = (4,5,6,7)
Wed, 5/21/08 8:51:36 AM	set		SEARCHRECTANGLE = (0,0,1024,768)
Selection Executed in 0:00:02

I hope you have found this useful, please feel free to share in your own threads findings and ideas about your particular situation, needs, wants, practices and visions.

While debugging some text streams I have been working on, I decided to make my life a bit easier in knowing what a particular character string contained. Remember what 09 is, or 10, or even 13 means… linefeed, newline, return (or did I get this wrong too) is a bit much to recall let along get correct. Instead you can use the handy charToDesc() handler to spit out the details of a string’s character constituents.

put charsToDesc("Todd" & space & "was" & tab & "here!!!" & return)

Offset    Char  Oct     Dec     Hex     Cntrl   Description
       1: T	124	84	54	Uppercase T
       2: o	157	111	6f	Lowercase o
       3: d	144	100	64	Lowercase d
       4: d	144	100	64	Lowercase d
       5: SP	40	32	20	Space
       6: w	167	119	77	Lowercase w
       7: a	141	97	61	Lowercase a
       8: s	163	115	73	Lowercase s
       9: HT	11	9	9	^I	Horizontal tab, move to next tab stop
      10: h	150	104	68	Lowercase h
      11: e	145	101	65	Lowercase e
      12: r	162	114	72	Lowercase r
      13: e	145	101	65	Lowercase e
      14: !	41	33	21	Exclamation mark
      15: !	41	33	21	Exclamation mark
      16: !	41	33	21	Exclamation mark
      17: LF	12	10	a	^J	Line Feed

A bit of a warning is in order. If you feed this handler long strings, it will produce long results.

(** a long version of the numToChar() function. 
Gives details of the characters (mostly designed for hidden chars,
but does deal with most of the ASCII table)
Most table data ripped/borrowed/lifted directly from
http://www.robelle.com/smugbook/ascii.html
Blame them for any errors or omissions.
**)

params  aString

get asciiTableDetails()

put "Offset    Char  Oct     Dec     Hex     Cntrl   Description"
repeat with each char aChar in aString
	set s to each line of ( lines 2 to -1 of it ) where word 3 of each is charToNum(aChar)
	write format("%8d: ", repeatIndex())
	if number of items in s is not 1 then put aChar
	else put item 1 of s
end repeat


(** assumed unique entries for each char in the list.
ASCII based, does not deal with UNICODE currently,
however you are welcome to expand and do this work yourself :) **)

to asciiTableDetails

	return <<Char>>)

Which would return to your script the bare essential text of the page in question. This can be very powerful for simple text search in a page result instead of wading thru a bunch of possibly troublesome tags in the result.

As a side note, yet marginally interesting to a technical historian, is the history of the ASCII character set with some tidbits of how it may be related to Morse Code (digital, or descrete signals). A good read while you are processing possibly large file sets with hidden characters and their meanings [NOTE: you may want to perform all this within SenseTalk containers instead of output to a Eggplant run window].

http://www.wps.com/projects/codes/index.html

And finally you may want to use the tried and true tool ‘cat -vt’ to help detect hidden unprintable characters. First ‘put’ the contents into a file path, and then issue ‘cat -vt filepath’, you can ‘man cat’ for more information on the CLI options to cat. The latter two steps would normally be done from a VT100 terminal window, of which Terminal.app comes with OSX. Another CLI tool that just rocks blocks, is iTerm.app (available online at

http://www.versiontracker.com/dyn/moreinfo/macosx/17484

Good luck and happy SenseTalkin’ with ya!

Here you go, a quick and easy front end for searching a file for a string and placing the search results into a file, pasteboard, or into the run results window. Along with GUI and speed, this script remembers old used values between script runs, as well as being a tutorial on how to make life easy on yourself and others by scripting gently.

params  resultsPath, aStringToFind, saveToPath

universal  lastSearchStringEntered, lastSearchPath, lastSavePath

set tempFilePath to "/tmp/TempFile-" & the long seconds split by "." joined by ""

if resultsPath is empty or resultsPath is not a file then
 -- Get the file path to search
 if lastSearchPath is empty then set lastSearchPath to UserInfo().HomeDirectory
 answer file "Enter or select the results file path:" in folder lastSearchPath
 if it is in ( "Cancel", empty ) then exit all
 set resultsPath to it
 set lastSearchPath to the folder of resultsPath
end if

if aStringToFind is empty then
 -- Ask for the string to search for
 if lastSearchStringEntered is empty then set lastSearchStringEntered to "LogError"
 ask "Enter a string:" title "Find a substring" with lastSearchStringEntered
 if it is in ( "Cancel", empty ) then exit all
 set aStringToFind to it
 set lastSearchStringEntered to aStringToFind
end if

-- Request if user wants to save to file or pasteboard
answer "Save to Pasteboard or a File" with \
  "Pasteboard" or "File" or "Standard Out" title "A place for salvation..."
if it is in ( "Cancel", empty ) then exit all

set foundStuff to each line of file resultsPath where each contains aStringToFind
if saveToPath is empty or saveToPath is not a file then
 -- Either act on the File, or stuff it all into a pasteboard.
 if it is "File" then
  if lastSavePath is empty then set lastSavePath to UserInfo().HomeDirectory
  ask file "Where to save results" title "Save data to file" in folder lastSavePath 
  if it is in ( "Cancel", empty ) then exit all
  put foundStuff into file it
  set lastSavePath to the folder of it
  open it with TextEdit  -- could put a open or not here???
 else if it is "Pasteboard" then
  put foundStuff into file tempFilePath
  shell "cat " & quote & tempFilePath & quote & " | pbcopy"
  delete file tempFilePath
 else
  put foundStuff
 end if
else
 put foundStuff into file saveToPath
end if

Most of this script is a wrapper around various parts of a simple need to get data from a file. In eccense there is one line that matters, where each contains aStringToFind :slight_smile:

To help get the IP numbers of connections from Eggplant, here is a filter handler allowing quick and easy access to the same data with actual IP values for ServerID instead of the Name override property set to ServerID.

params  aFilter

set aList to an empty list
repeat with each connection in AllConnectionInfo()
 if ( each item in the values of connection where each contains aFilter ) is not () then
  set aNewConnection to an empty property list
  repeat with each key in the keys of connection
   set aNewConnection.(key) to QuoteString(connection.(key))
  end repeat
  insert aNewConnection into aList
 end if
end repeat

return  aList

to QuoteString  aString
 if char 1 of aString is not quote then put quote before aString
 if char -1 of aString is not quote then put quote after aString
 return  aString
end QuoteString
-- See: http://www.trap17.com/index.php/ipv4-vs-ipv6_t24034.html
-- returns the IP if it is valid, or the items of a list of IPs that are valid.
-- A resonable filter/verification tool.

params   IPs...

repeat with each anIP in IPs by reference
	set validNumbers to each item delimited by "." of anIP where each is between 0 and 255
	get number of items in validNumbers
	if it is not in ( 4, 16 ) or it <> number of items delimited by "." in anIP then delete anIP
end repeat

return  IPs

Here is a revised WaitForClick handler that does what it implies by name. We first parse out the parameters which are delay, images… and an optional ‘hidden’ value which specifies the instance if provided by a positive number. As a convenience handler, this is a staple in my bag of SenseTalk tricks. Hope you find it as useful in your testing practices.

params  delay, images...

set instance to 1
get a reference to the last item of images
if it is a positive number then
 set instance to it
 delete it
end if

if delay is not a positive number then
 insert delay before images
 set delay to 30 seconds
end if

-- if an image, it will become a location...
repeat with each image in images
 if image is not a point then
  MoveTo  RemoteScreenSize()
  WaitFor  delay, image
  set imageLocations to EveryImageLocation(image)
  get number of items in imageLocations
  if instance > it then
   LogWarning "Setting image instance to be selected to the maximum number of images found (" & it & \
     "), you specified a larger number of images to be selected " & instance & "."
   set instance to it
  end if
  set image to item instance of imageLocations
 end if
 Click  Image
end repeat

I have so many version of WaitForClick, I’m not sure which is best or least likely to cause issues, however this one seems to be my favorite right now.

Here are a set of random data generators (often useful when making new filler/bogus test data.) Included is a SSN, First and Last name, even ‘real’ looking company name generators. Each object uses the getProp for a derived composite value for each instance of the RandomPerson object. Named RandomPerson, once is created the instance no longer is ‘random’…

Here is one that I like and hope others have a use for, the ImageCountShouldBe handler. You pass it a count and an image name. Although not as often useful, a list of images may be passed as well. Originally just a simple five line segment of script I was using over and over, ideal for a pullout and factor to a handler:

MoveTo  RemoteScreenSize()  -- move mouse out of the way
get number of items in EveryImageLocation(imageName)
if it <> expectedCount then
 LogError "The number of matching images found for image element '" & \
   image & "' should have be '"& expectedCount & \
   "', yet we have found '" & it & "' in only instances."

Now bloated and somewhat more robust, we have the all new ImageCountShouldBe handler:

params  count, images...

if count is not a positive number and count is not empty then
 insert count before images
 set count to 1
end if

MoveTo  RemoteScreenSize()  -- get mouse out of the way, allowing max image find as possible.

repeat with each image of images
 get number of items in EveryImageLocation(image)
 if it <> count then
  LogError "The number of matching images found for image element '" & image & "' should have be '"& count & \
    "', yet we have found '" & it & "' in only instances."
 else if universal imageCountShouldBeLogged is not in ( empty, off, no, false ) then
  Log "The number of matching images found for image element '" & image & "' was "& count & " instances."
 end if
end repeat

Here is how you can use this new handler, simple and to the point.

set universal imageCountShouldBeLogged to true  -- how you want reporting.
ImageCountShouldBe 10, testing

I’m sitting here wondering how in the world all the visual data that I have to verify on an hourly basis is going to get done easily while maintainable. Coming to my rescue yet again is SenseTalk, and some Eggplant functionality. I check the generation of PDFs and layouts fairly regularly as part of 90% of my automation development efforts, across projects, so I must have powerful easy to use tools in my toolbox of goodies.

Being able to to quickly verify the content of a page or area of layout is important, particularly against a baseline of external data sources. This in turn allows quick relatively fast migration from regressed tests to new version of software, both the tests and the AUT.

Here is a quick script to allow the identification either by point or image of a view on a webpage or AUT’s window to be selected all, and verified for content against a local file in /path/to/your/test.suite/Data. I like to use the term expectedData so it is clear in reading what is going on… And because I don’t always remember the path to the local suite, why not use a handler which considers the target in resolving the path, and put that handler into your tool-belt of tool-belts, EggplantCommons.suite or your own library suites.

params  imageOrLocation, pathOrData, stripLeadingTrailing

if there is a file pathOrData then
 set data to file pathOrData
else
 set dir to (( target's long name )'s folder's folder & "Data/")
 set files to ( each item in ( the files in dir ) where each contains pathOrData )
 set pathOrData to item 1 of files
end if

if stripLeadingTrailing is empty or stripLeadingTrailing is not a boolean then set stripLeadingTrailing to false

WaitForClick imageOrLocation

get FoundImageLocation()
SelectAll  it
Copy  it
Click  it  -- unselect so future images may be found.

wait 1 second  -- give SUT time to move data to pboard.
get RemoteClipboard()

if there is a file pathOrData then \
  set pathOrData to file pathOrData
if not stripLeadingTrailing then \
  set ( it, pathOrData ) to ( words 1 to -1 of it, words 1 to -1 of pathOrData )

return  it is pathOrData

There is a fair amount going on in this handler, yet in summary an image or location is used to click at a spot on the AUT/SUT. The data path or data source is resolved and used for comparison. Leading and trailing white space (strict comparisons) are either opted in or out, by default out. A fixed 1 second wait time is exercised to try and help the SUT get all the data onto the pasteboard and post a change. If you see your not getting the changes you need, make sure to lengthen this time a bit. Be careful, this time adds up quickly in many iterations to wasted minutes and hours done haphazardly.

And finally the comparison truth is returned.

One fairly simple yet powerful mod to this script would be to include a rectangle in which to drag out a region and copy that to the clipboard. Might want to simply check to see if the first to parameters are either existing images, or if they are not points to try and find the images assuming the images are available in one of the helper suites of your work. This brings up some very powerful expanded behaviour to this hander, left to the astute reader and EP/ST scripter.

Here is a simple function, but oh so powerful.

Often in multi application environments I must to verify text generated in one application against the data presented in another. This poses some problems, mostly intra and inter-suite communications. Using the results of RunWithNewResults helps, as does scraping screens for data that an application creates (for instance when creating new items in a database). Making assumptions the data generated (item numbers for instance) are accurate [kinda dangerous, but we live on the edge at times], we can scrape that info from the GUI and pass it along to the next dependent script in the chain of scripts to run.

(** take one or two parameters, if two then drag and drop, if just one move
to and select all **)

params  topLeft, bottomRight

get ImageLocation(topLeft)

if paramCount() is 1 then
 Click  topLeft
 SelectAll
else if paramCount() is 2 then
 Drag  topLeft
 Drop  bottomRight
end if

RightClick  it
WaitForClick  copy
wait until not ImageFound(copy)

return  the RemoteClipboard

So, what is this about you say? Well, its a simple one or two parameter screen scraping tool. Taken a point, or a two point region, we click, select all or drag and drop. That highlighted area is then right clicked and copied to the pasteboard. Finally that remote data is sent back to the calling script with a ‘the remoteClipboard’.

Here you can pass a rectangle to the function RandomPointWithin, and get back as you may expect a point within that rectangle, any point. No, this is not magic at all… Its about taking the base of the rectangle, and randomizing a number from 0 to the width and another from 0 to the height, add those respectively to the x and y portions of the origin of the rectangle… Presto, magic chango, you have a random point in the rectangle.

params  aRect

set x to aRect.x + random ( 0, aRect.width )
set y to aRect.y + random ( 0, aRect.height )

return ( x, y )


to UnitTest  x

 if x is not a number then set x to 5
 
 repeat x times
  put RandomPointWithin((0, 0, 30, 10 )) & " | "
 end repeat
 
end UnitTest

And if you unit test this, as we may always try in good scripting practices…

RandomPointWithin.UnitTest(100)
(4,3) | (19,7) | (19,10) | (8,3) | (29,6) | (16,9) | (13,2) | (9,2) | (27,7) | (3,1) | (6,4) | (21,6) | (7,3) | (24,10) | (9,9) | (8,5) | (8,5) | (18,4) | (28,1) | (12,5) | (15,2) | (24,1) | (29,0) | (27,6) | (9,9) | (12,3) | (6,9) | (7,2) | (12,0) | (21,2) | (23,9) | (0,8) | (24,6) | (21,6) | (12,6) | (15,3) | (26,0) | (20,9) | (6,5) | (18,5) | (12,7) | (29,5) | (28,2) | (29,2) | (1,3) | (28,10) | (15,8) | (19,4) | (14,5) | (26,7) | (10,7) | (18,5) | (16,3) | (29,3) | (22,5) | (4,1) | (11,2) | (20,4) | (3,8) | (17,5) | (1,6) | (26,10) | (10,9) | (23,5) | (24,10) | (18,0) | (27,1) | (8,0) | (11,6) | (30,0) | (18,10) | (4,10) | (2,3) | (2,4) | (5,5) | (7,8) | (29,0) | (21,4) | (12,7) | (1,0) | (30,8) | (8,0) | (23,3) | (4,10) | (18,0) | (4,7) | (3,6) | (28,10) | (26,5) | (6,6) | (16,8) | (6,10) | (11,6) | (21,4) | (8,8) | (27,6) | (9,0) | (0,0) | (14,3) | (3,8) | 

Hope you enjoy, and find stuff like this useful.

While I’m developing full screen or partial verification scripts for clients, it not uncommon for there to be changes occurring from one second to the next between actions. These changes must be verified at that point in the presentation layer of the GUI against baseline standards (values or layout requirements) for the software as part of the acceptance process as well as the QA process immediately at hand.

So, here is the basis of a sophisticated screen scraping system based on region identification (visual apparent) and complete page scraping which takes all visual text as its target.

(** take one or two parameters, if two then drag and drop, if just one move
to and select all **)

params  topLeft, bottomRight

get ImageLocation(topLeft)

if paramCount() is 1 then
 Click  topLeft
 SelectAll
else if paramCount() is 2 then
 Drag  topLeft
 Drop  bottomRight
end if

RightClick  it
WaitForClick  copy
wait until not ImageFound(copy)

return  the RemoteClipboard

So, what is this all about you say? Well, it is the core of a simple scraping tool. Taken a point, or a two point region, click, select all or drag and drop. The highlited area can be right clicked and copied to the pasteboard, and returned to the calling Eggplant script with a ‘get the remoteClipboard’ command.

Next time around I’ll approach the notion of regions and how you can use naming conventions along with other meta data to help locate, identify and gather information from the remote system quickly and effectively making your day a nicer happening.

Here we have a simple handler to normalize the spacing of a string with single spaces between words. If you wanted, you could add simple the double space after a punctuation/end of sentence.

params  @data

repeat with each line in data by reference
	set it to words 1 to -1 of it
end repeat

split data by return
join data with space

set data to each word of data
join data by space

return data

Where in example we have some text to normalize, you can see it is all messed up. So we throw the power of word selection, split and joins, and presto it all works out well in the end.

set data to <<On July 2 the Congress voted for independence by approving the Lee Resolution, twelve delegations voting in favor while the New York delegation abstained. (New
York did not cast its vote for the Lee Resolution until July 9.)
The Declaration was reworked somewhat in general session of the Continental
Congress. Congress, meeting in Inde  endence Hall in Philadelphia, shed revising
Jefferson's dr statement on July 4, approved it, and sent it to a printer. At the
signing, Ben.amin Franklin is quoted as having replied to a comment by Hancock
that they must all hang together: "Yes, we must, indeed, all hang together, or most
assuredly we shall all hang separately,"[4] a play on words indicating that failure to
stay united and succeed would lead to being tried and executed, individually, for
treason.>>

NormalizeSpacing @data
put data

And we end up with this…

On July 2 the Congress voted for independence by approving the Lee Resolution, twelve delegations voting in favor while the New York delegation abstained. (New York did not cast its vote for the Lee Resolution until July 9.) The Declaration was reworked somewhat in general session of the Continental Congress. Congress, meeting in Inde endence Hall in Philadelphia, shed revising Jefferson's dr statement on July 4, approved it, and sent it to a printer. At the signing, Ben.amin Franklin is quoted as having replied to a comment by Hancock that they must all hang together: "Yes, we must, indeed, all hang together, or most assuredly we shall all hang separately," [4] a play on words indicating that failure to stay united and succeed would lead to being tried and executed, individually, for treason.

Here’s a couple of quick and dirty helpers, if you follow the EggDoc standards for comment (see EggDoc for details)…

getProp  summary
 return words 1 to -1 of second item delimited by "(**" \
   of second item delimited by "**)" of my script
end getProp

This second property is more detailed as should your EggDoc comments. It gives the parameters and their details as well.

getProp  usage
 return words 1 to -1 of second item delimited by "(**" \
   of first item delimited by "**)" of my script
end getProp

You can then sweep thru each object/script and the objects with a property of usage or summary that are not equal can be used to build a usage and summary table for your internal and external publication.

Here is an example of how to use the summary command for a Script called TempFileName, which returns a temp file in the file system with some powerful naming features included.

put TempFileName's summary

Enjoy!

1 Like

Is the FilesExistExamples.suite.zip still available for download? :slight_smile: Thanks!

No, sorry.