Reusing Code
There’s no way to get around it: Repetition is a fact of life in testing. Fortunately, there are several easy ways to let scripts and handlers do the repeating for you.
Calling Scripts from Other Scripts
One of the most common ways to reuse code is to call a script from a different script. To call a script, insert the script's name in your code, followed by any parameters that the script requires. The script call can be treated as if the script itself were a command or a function. For detailed information about calling other scripts, see Running Other Scripts and Handlers.
Following are examples of calling scripts from within a script:
CycleWindows // Calls a simple script name as a command
ConnectionScript "Old SUT", "New SUT" // Calls a simple script name with parameters
put CaptureTheScreen("SomePrefix",1) into myScreenCapture
// Calls a script as a function with parameters and puts the result in a variable
Whether you use the command call or the function call depends on the script itself. Both types can take input parameters, in which case the called script needs to begin with a params
declaration. The function call also returns values, in which case the called script must include a return
statement.
If you use a simple script name, with no spaces or special characters, and store scripts in the same suite or a helper suite, you can use the script name directly. See the examples above.
For a script in an unrelated suite, or a script with spaces or special characters in its name, you can type the Run
command, followed by the script path:
Run "/Users/Seiji/Documents/EggPlant Suites/Validation.suite/Scripts/ConnectionScript"
// Calls the Run command to open a script from an unrelated suite
Because of this ability to call a script from within another script, it's useful to think about writing scripts in segments that perform specific tasks or actions. This "modular" approach allows you the most opportunity to reuse scripts in different contexts. In such a case, your "main" script would include code for test or environment specifics but would call out to a different script to complete generic tasks.
More detailed information about using handlers and helper suites, which are both features that can help with code reuse and modular design, can be found below.
Handlers
To reuse code within the same script, you can write a handler
, which is essentially a subscript. A handler is a part of your main script that you can call anytime, as if you were calling another script.
Example: CaptureTheScreen
handler
to CaptureTheScreen prefix, count // Starts the handler
set fileName to prefix & "ScreenShot" & count // Puts prefixScreenShotCount into fileName
set tempFile to "/tmp/" & fileName // Puts "/tmp/filename" into tempFile
CaptureScreen tempFile //Captures a screenshot
return fileName // Sets fileName as the function return value
end CaptureTheScreen //Ends the handler
Returning Results of a Handler
If a script or handler returns a value, like this one, you can access the value by calling the Results
function in the next line. So, using the example above, you can call the handler and access the result as shown below.
Example: Returning results of a handler
CaptureTheScreen "MacTest", 6 // Calls the CaptureTheScreen handler
Set newFile to the result // Puts the returned value into a variable
Handlers as Commands or Functions
Because the CaptureTheScreen
handler is a generic handler, it could also be called as a function. The major difference in use is that a command is a complete statement by itself, and may be used without regard for whether it returns a value, while a function must be called as part of an expression.
Example: Calling handlers as commands and functions
CaptureTheScreen "MacTest", 6 // Called as a command; runs the handler
put CaptureTheScreen ("MacTest",6) // Called as a function; returns the value of the handler. Note parentheses.
Calling a handler from another script
There are three ways to call a handler from another script:
- Call the script name with a possessive
‘s
, followed by the name of the handler and its parameters. - Call the script name, adding a dot and the name of the handler to it.
- Call the handler name, followed by the word
of
and the script name.
Example: Calling a handler from another script
run Logging's CaptureTheScreen "MacTest", 6 // Calls handler with script name and possessive 's
run Logging.CaptureTheScreen "MacTest", 6 // Calls handler in the form script.handler
run CaptureTheScreen of Logging "MacTest", 6 // Calls handler with handler "of" script