This is an old revision of the document!
Tutorial: Automation Scripting - State Model
Learning Objectives:
This tutorial assumes that you are able to create a simple State-based Model and have created a simple model to write scripts for. If not, please refer to Tutorial - State-based Modeling.
Model scripting
Apache Groovy script is the scripting language for your models.
Groovy script is syntactically very similar to most of programming language that allows you to create classes, functions and basic logic and arithmetic operations.
You will typically wrap your expected processing logic in a groovy function:
def MBT_Start () { $SYS.log ('inside MBT_Start TRIGGER'); }
The groovy function is then attached to the states and/or transitions in the model through annotation:
@TRIGGER('MBT_START') def MBT_Start () { $SYS.log ('mbt started. '); }
where 'MBT_START' is the identifier of system trigger (MBT_START) or UID of the state / transition, below is an example of transition trigger:
@TRIGGER('U1072') def 'Start: add25'() { $SYS.log('State Start, transition addQuarter'); }
You will need to add following “import” to the beginning of the script for the trigger annotation keyword:
import com.testoptimal.mscript.groovy.TRIGGER
Besides triggers, scripts are also used for building UI Page Objects, Cucumber style test step definition and MCases, which are out of scope of this tutorial. Please refer to Script Editor for more details.
Model triggers
Model triggers are model's execution hook to run groovy functions as model is executed. Model triggers include:
- MBT_START - executed at the beginning of model execution and before model automation starts
- MBT_END - executed at the end of model execution
- MBT_FAIL - executed up each defect (bug) detection
- MBT_ERROR - executed on fatal error during model execution
- ALL_STATES - executed when any state is traversed before specific state trigger is executed
- ALL_TRANS - executed when any transition is traversed before specific transition trigger is executed
- Specific state trigger - executed when the specific state is traversed
- Specific transition trigger - executed when the specific transition is traversed
Use the annotation @TRIGGER('id or uid') to attach a groovy function to the trigger.
To create a trigger function, press Ctrl-I in Script Editor and select the trigger type or a state / transition from the code assist list:
Below is an excerpt from a model trigger scripts:
// Model Triggers import com.testoptimal.mscript.groovy.TRIGGER @TRIGGER('MBT_START') def 'MBT_START' () { $SYS.addTestOutput('<html><body><H1>Test Output</H1>'); $SYS.addTestOutput('<ol>');\ $SYS.log ('mbt started. '); } @TRIGGER('MBT_END') def 'MBT_END' () { $SYS.addTestOutput('</ol></body></html>'); $SYS.saveTestOutput('testOutput.html'); } @TRIGGER('MBT_ERROR') def 'MBT_ERROR' () { $SYS.log('Fatal error encounter, path to reproduce error: ' + $SYS.trace()); } @TRIGGER('U1062') def 'State_1' () { $SYS.addTestOutput('<li><span>Test Case ' + $SYS.getPathName() + '</span><ul>'); $SYS.addTestOutput('<li><span>At state State_1</span></li>'); } @TRIGGER('U1072') def 'State_1: trans_A'() { $SYS.addTestOutput('<li>Step: trans_A</li>'); }
Feel free to add any additional scripts you wish.
Once scripts are completed, run the model and inspect the output file “testOutput.html” in ARTIFACT tab as well as Model Log file via menu Run / Model Log.
Activating plugins
Plugins provides additional script functions to interact with AUT or perform specific operations in the model scripts.
Plugins must be activated for each model as described in activating plugins. Follow the instruction to activate SELENIUM plugin for this tutorial. But feel free to experiment with other plugins.
After the plugins are activated for the model, the plugin IDs are automatically added to the code-assist popup list (Ctrl-Space):
Go ahead and activate Random Data Generator. This adds $RANDOM to the code-assist popup list. And now you can use any of the plugin functions provided by Random Data Generator.
For example:
$SYS.log('Random phone#: ' + $RANDOM.randPhone('999-999-9999') );
User and environment variables
The variables that you created/declared in groovy scripts are local within the trigger/ function. Often times you may want to create a global variable that is accessible from multiple triggers / functions. User Variable does exactly that for you.
To create a user variable, call a system function:
$VAR.var1 = 100
And to reference a user variable any trigger functions:
$SYS.log ('User var1=' + $VAR.var1)
Typically you would declare all user variables that you want to use in MBT_START trigger so that you know what user variables are being used in the model and make sure they are initialized to a default value:
@TRIGGER ('MBT_START') def 'MBT_Start' () { ... $VAR.var1 = 100 ... }