Approaches to Script Modularization and Organization
When you sit down to design a modular architecture for your test scripts, you are confronted with a choice between two different mechanisms for organizing and accessing scripts: Eggplant’s Suites, and SenseTalk’s BackScripts (better known as the Start Using command). This posting will explain these two approaches and describe the pros and cons of each, so you can make an informed decision about which mechanism is right for you.
An Eggplant Suite is the primary level of organization offered in Eggplant for grouping a set of related scripts and images. Eggplant provides the suite mechanism but doesn’t prescribe how it should be used. Some users create a suite for each application or version they are testing. Others like to break things down further and create separate suites for different functional areas within a single application.
Suites can also be used at a higher level of organization, such as to contain a collection of master control scripts that may call scripts in a variety of other suites.
Calling Scripts in Open Suites
A script can call other scripts located in the same suite by simply using the script name as a command or function name. Scripts from another suite can also be called in this way, if the other suite is “open” for use. A suite can be open in three different ways. First, the suite containing the current script is always open. Second, any suite listed as a Helper suite (on the “Helpers” tab) of an open suite is also open. This rule also applies to helpers of helpers, and so forth to any level.
Finally, a suite can be opened dynamically in a script using the OpenSuite command. A suite opened in this way remains open only for the duration of the handler in which it was opened (it is closed automatically when the current handler finishes). It may also be closed sooner using the CloseSuite command.
In addition to locating scripts in any open suites, Eggplant can also use images from any open suite. This is an important advantage of using suites.
About Start Using and the BackScripts
The Start Using command is used to make a library script available for use by other scripts. Any handlers of a script that is “in use” can be called directly from any script, in the same way that local handlers in the current script can be called. The Start Using command is technically just a convenient way to insert a script into the BackScripts list, which can contain any number of scripts that are in use at any given time. Scripts can be removed from the BackScripts with the Stop Using command.
Calling Handlers in BackScripts
Whenever a command or function is called in a script, the SenseTalk runtime system first looks for a handler in the same script, then in each of the scripts in the BackScripts list. If no matching handler is found, a built-in command or function is called. If there is no built-in command or function, then Eggplant will search through the open suites for a script matching the command or function name that was called.
Making the Choice
Both of these mechanisms are useful depending on your needs.
Advantages of Suites:
- The images within open suites are available for use, as well as their scripts.
- Suites are clearly visible within Eggplant, with all of the scripts in a suite plainly listed in the Scripts tab of the suite window.
- A suite’s standard helpers are similarly visible under the Helpers tab.
- Suite usage can be controlled dynamically within a script using the OpenSuite and CloseSuite commands.
Disadvantages of Suites:
- Some users may feel that it is less convenient to have several suites open at once.
- Each handler is in its own script file.
- Can’t override built-in commands and functions.
Advantages of Start Using/BackScripts:
- Some users may find it convenient to package multiple handlers in a single script, particularly in the case of related collections of handlers that call one another.
- BackScripts can intercept and override or modify calls to built-in commands and functions.
- BackScript usage can be controlled dynamically within a script using the Start Using and Stop Using commands.
Disadvantages of Start Using/BackScripts:
- BackScripts don’t provide access to images for their callers (although handlers in a backScript do have access to their own images).
- BackScripts may have a small impact on performance (see below).
In most situations, we don’t expect either open suites or backScripts to have a significant impact on script performance. If script performance is highly important in your tests, you may want to be aware of the following tradeoffs between these two mechanisms. First, it’s important to clearly understand the way SenseTalk scripts function. Every command or function generates a “message” that is passed along a “message-passing path” until a handler is found for the message. A slightly simplified version of the message-passing path is as follows:
- The message is sent to the script that is running. If the script contains a handler for the message, that handler will receive the message.
- Each of the scripts in the backScripts list will be checked to see if it has a handler for the message.
- The message is sent to Eggplant/SenseTalk to see if there is a built-in command or function to handle the message.
- Each of the open suites is checked to see if it has a script with the same name as the message. If so, that script will receive the message.
- If no handler was found for the message, an error occurs.
Some of the consequences of the message-passing path are as follows:
- BackScripts lengthen the message path for calls to built-in commands and functions. If you have many scripts in the backScripts list, there will be a small impact on overall script execution speed as each script in the list is checked for a message match.
- Because backScripts are inserted in the path at step 2, and open suites at step 4, that is why backScripts can override built-in commands, and scripts in open suites cannot.
Both Eggplant Suites and Start Using/BackScripts are useful mechanisms to know about, that can help you to modularize and organize the functionality of your test scripts. In general, we recommend using suites in Eggplant for their greater visibility and inclusion of images. You may prefer backScripts in certain situations, such as when you have a need to override built-in commands, or as a way to deliver a packaged library of interrelated commands and functions.