====== Tutorial: Automation Scripting - State Model ====== Learning Objectives: * [[#Model scripting]] * [[#Model triggers]] * [[#Activating plugins]] * [[#User variables]] * [[#Assert and track requirements]] * [[#Initialization scripts]] 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 [[state_modeling | Tutorial - State-based Modeling]]. ===== Model scripting ===== [[https://groovy-lang.org/syntax.html | 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 [[../ide_script | Script Editor]] for more details. ===== Model triggers ===== [[../ide_script#trigger_script | 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 [[../ide_script | Script Editor]] and select the trigger type or a state / transition from the code assist list: {{wiki:overview:tut_sccript_trigger_list.png?100}} 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('

Test Output

'); $SYS.addTestOutput('
    ');\ $SYS.log ('mbt started. '); } @TRIGGER('MBT_END') def 'MBT_END' () { $SYS.addTestOutput('
'); $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('
  • Test Case ' + $SYS.getPathName() + '