Differences
This shows you the differences between two versions of the page.
tutorial:scripting_state_model [2020/11/24 22:09] admin [Activating plugins] |
tutorial:scripting_state_model [2021/01/05 03:00] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Tutorial: Automation Scripting - State Model ====== | ||
- | |||
- | Learning Objectives: | ||
- | * [[#Model scripting]] | ||
- | * [[#Model triggers]] | ||
- | * [[# | ||
- | * [[#User variables]] | ||
- | * [[#Assert and track requirements]] | ||
- | * [[# | ||
- | |||
- | This tutorial assumes that you are able to create a simple // | ||
- | |||
- | ===== Model scripting ===== | ||
- | [[https:// | ||
- | |||
- | //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 (' | ||
- | } | ||
- | |||
- | The groovy function is then attached to the states and/or transitions in the model through annotation: | ||
- | | ||
- | def MBT_Start () { | ||
- | $SYS.log ('mbt started. '); | ||
- | | ||
- | where ' | ||
- | | ||
- | def ' | ||
- | $SYS.log(' | ||
- | } | ||
- | |||
- | You will need to add following " | ||
- | | ||
- | |||
- | |||
- | 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. | ||
- | |||
- | ===== Model triggers ===== | ||
- | [[../ | ||
- | * 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 // | ||
- | |||
- | To create a trigger function, press //Ctrl-I// in [[../ | ||
- | |||
- | {{wiki: | ||
- | |||
- | |||
- | Below is an excerpt from a model trigger scripts: | ||
- | // Model Triggers | ||
- | | ||
- | |||
- | | ||
- | def ' | ||
- | $SYS.addTestOutput('< | ||
- | $SYS.addTestOutput('< | ||
- | $SYS.log ('mbt started. '); | ||
- | } | ||
- | |||
- | | ||
- | def ' | ||
- | $SYS.addTestOutput('</ | ||
- | $SYS.saveTestOutput(' | ||
- | } | ||
- | |||
- | | ||
- | def ' | ||
- | $SYS.log(' | ||
- | } | ||
- | |||
- | | ||
- | def ' | ||
- | $SYS.addTestOutput('< | ||
- | $SYS.addTestOutput('< | ||
- | } | ||
- | |||
- | | ||
- | def ' | ||
- | $SYS.addTestOutput('< | ||
- | } | ||
- | |||
- | Feel free to add any additional scripts you wish. | ||
- | |||
- | Once scripts are completed, run the model and inspect the output file " | ||
- | |||
- | |||
- | |||
- | |||
- | ===== 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 [[../ | ||
- | |||
- | After the plugins are activated for the model, the plugin IDs are automatically added to the code-assist popup list (// | ||
- | |||
- | {{wiki: | ||
- | |||
- | Go ahead and activate [[../ | ||
- | |||
- | For example: | ||
- | |||
- | | ||
- | |||
- | For test automation, for example web application you would activate // | ||
- | |||
- | For example: | ||
- | |||
- | | ||
- | |||
- | You could also use //PAGES// tab to define page object: | ||
- | // | ||
- | | ||
- | def addMainPage () { | ||
- | mainPage = $SYS.addPage(' | ||
- | elem = mainPage.addElement(' | ||
- | elem.addAction(' | ||
- | | ||
- | }) | ||
- | } | ||
- | | ||
- | |||
- | Then in // | ||
- | | ||
- | |||
- | |||
- | ===== User variables ===== | ||
- | |||
- | The variables that you created/ | ||
- | |||
- | To create a user variable, call a system function: | ||
- | |||
- | | ||
- | |||
- | And to reference a user variable any trigger functions: | ||
- | |||
- | | ||
- | |||
- | Typically you would declare all user variables that you want to use in // | ||
- | |||
- | | ||
- | def ' | ||
- | ... | ||
- | $VAR.var1 = 100 | ||
- | ... | ||
- | } | ||
- | |||
- | |||
- | |||
- | ===== Assert and track requirements ===== | ||
- | It's the responsibility of your scripts (TRIGGERS) to check and validate that AUT is behaving correctly. Your script does this by checking elements on AUT through //plugins// functions and assert that AUT elements have the expected values. | ||
- | |||
- | For simplicity sake, we will just use a random number generator to simulate the values from your AUT and use that to demonstrate how to perform the assertion and tracking the requirements. | ||
- | |||
- | The assert and tracking requirement are done with one of the two $SYS functions: | ||
- | |||
- | * $SYS.assertTrue (' | ||
- | * $SYS.assertFalse (' | ||
- | |||
- | where ' | ||
- | |||
- | As example, the following script will assert random number is greater than 5: | ||
- | |||
- | | ||
- | |||
- | You may also perform check with if/else and then raise the defect explicitly, for example: | ||
- | | ||
- | if ($RANDOM.randNum(0, | ||
- | $SYS.addReqPassed (' | ||
- | } | ||
- | else { | ||
- | $SYS.addReqFailed (' | ||
- | } | ||
- | |||
- | The requirement ' | ||
- | |||
- | Add your own script to perform assertion and requirement tracking to the model TRIGGERS, re-run the model, and check [[../ | ||
- | |||
- | |||
- | ===== Initialization scripts ===== | ||
- | Often times you may want to pass some configuration settings to your model scripts, for example, the AUT url which might change depending on if you are running your model against your development environment or QA environment or for this tutorial, maybe we want to change the threshold value of the random number the system must generate - which we have hard-coded to 5. | ||
- | |||
- | You could certainly accomplish this by setting // | ||
- | |||
- | // | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||