Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tutorial:scripting_state_model [2020/07/17 20:19]
admin [Tutorial: Automation Scripting - State Model]
tutorial:scripting_state_model [2021/01/05 03:00]
Line 1: Line 1:
-====== Tutorial: Automation Scripting - State Model ====== 
- 
-Learning Objectives: 
-  * [[#Model scripting]] 
-  * [[#Model triggers]] 
-  * [[#Activating plugins]] 
-  * [[#User variables]] 
-  * [[#Assert and track requirements]] 
-  * [[#Initialization scripts]] 
- 
- 
-===== 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 () { 
-      $SELENIUM.setBrowserChrome (); 
-      $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.page('MainPage').element('Quarter').perform('click'); 
-      bal = $SYS.page('MainPage').element('Balance').perform('getText'); 
-      if (bal.equals('0.25')) { 
-          passMsg = "Insert one Quarter passed, balance confirmed: " + bal; 
-          $SYS.addReqPassed("Q!", passMsg); 
-      } 
-      else { 
-         failMsg = "Insert one Quarter failed, expecting balance of 0.25, but got " + bal; 
-         $SYS.addReqFailed("Q1", failMsg, 'QUARTER-0-25'); 
-      } 
-   } 
- 
-You will need to following import to the beginning of the script for the trigger annotation keyword: 
-   import com.testoptimal.mscript.groovy.TRIGGER 
- 
-The above examples uses //SELENIUM// plugin with "$SELENIUM.setBrowserChrom()", you must activate //SELENIUM// plugin, see [[#Activating plugins]] for more details. 
- 
- 
-Besides triggers, scripts are also used for building UI Page Objects, Cucumber style test step definition and MCases.  Please refer to the specific wiki pages/tutorials 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' () { 
-      $SELENIUM.setBrowser ($SYS.getExecDir().getExecSetting().getOption('ideBrowser')); 
-      $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_FAIL') 
-   def 'MBT_FAIL' () { 
-      // Failure detected 
-      $SELENIUM.snapScreen(''); 
-   } 
-    
-   @TRIGGER('MBT_ERROR') 
-      def 'MBT_ERROR' () { 
-   } 
-    
-   @TRIGGER('U1062') 
-      def 'Start' () { 
-      $SELENIUM.getWebDriver().get('http://localhost:' + $UTIL.getPort() + '/DemoApp/VendingMachine/VendingMachine.html'); 
-      $SYS.page('MainPage').element('Cancel').perform('click'); 
-      bal = $SYS.page('MainPage').element('Balance').perform('getText'); 
-      if (!bal.equals('0.00')) { 
-         $SYS.addReqFailed ('Cancel failed to reset balance to 0.00', 'Cancel', 'CANCEL-FAILED'); 
-         $SYS.abort('Cancel does not work!!!'); 
-      } 
-   } 
-    
-   @TRIGGER('U1072') 
-   def 'Start: add25'() { 
-      $SYS.addTestOutput('<li><span>Test Case '  + $SYS.getPathName() + '</span><ul>'); 
-    
-      $SYS.addTestOutput('<li>Step: add a Quarter</li>'); 
-      $SYS.page('MainPage').element('Quarter').perform('click'); 
-      bal = $SYS.page('MainPage').element('Balance').perform('getText'); 
-      if (bal.equals('0.25')) { 
-          passMsg = "Insert one Quarter passed, balance confirmed: " + bal; 
-          $SYS.addReqPassed("Q!", passMsg); 
-      } 
-      else { 
-          failMsg = "Insert one Quarter failed, expecting balance of 0.25, but got " + bal; 
-          $SYS.addReqFailed("Q1", failMsg, 'QUARTER-0-25'); 
-      } 
-   } 
-    
-   @TRIGGER('U1073') 
-   def 'Start: add50'() { 
-      $SYS.addTestOutput('<li><span>Test Case '  + $SYS.getPathName() + '</span><ul>'); 
-    
-      $SYS.addTestOutput('<li>Step: add a Half-Dollar</li>'); 
-      $SYS.page('MainPage').element('HalfDollar').perform('click'); 
-      bal = $SYS.page('MainPage').element('Balance').perform('getText'); 
-      if (bal.equals('0.50')) { 
-          passMsg = "Insert one HalfDollar passed, balance confirmed: " + bal; 
-          $SYS.addReqPassed("Q!", passMsg); 
-      } 
-      else { 
-          failMsg = "Insert one HalfDollar failed, expecting balance of 0.50, but got " + bal; 
-          $SYS.addReqFailed("Q1", failMsg, 'QUARTER-0-50'); 
-      } 
-   } 
- 
-===== 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 [[../ide_script#plugins | 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//): 
- 
-{{wiki:idescreen:ide_script_ca.png? 100}} 
- 
- 
- 
-===== User and environment variables ===== 
- 
-==== User 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 
-      ... 
-   } 
- 
-==== Environment Variables ==== 
-//Environment Variables// are global in //TestOptimal// server and thus accessible to all running models. 
- 
-//Environment Variables// are configured / initialized in //config/config.properties// file, for example: 
- 
-    ENV.var1=value1 
-    ENV.var2=value2 
- 
-They can be updated/set by script via //$SYS.setEnvVar(...)//, for example: 
- 
-   $SYS.setEnvVar('var1', 'value2'); 
- 
-The new value is saved back to //config/config.properties// file and will survive server restart. 
-    
-//Environment Variables// are accessed via //$SYS.getEnvVar(...)//, for example: 
- 
-   $SYS.log("ENV variable var1=" + $SYS.getEnvVar('var1')); 
- 
- 
-    
-===== Assert and track requirements ===== 
-===== Initialization scripts ===== 
- 
- 
-