Inject code | Tutorial
Watch this short video to learn how to extend our codeless solution with code snippets in Javascript to implement more complex test steps, beyond the top 90+% of predefined actions offered out of the box (such as clicking a button, entering text, or simple drag-and-drop operations). You can then tie these code snippets to one or more steps in your tests and execute them in the context of the browser. If you don't know how to code, you can work with a developer in your organization.
For your convenience, we have also listed the steps for the main procedures below the video.
- On the canvas, in Advanced mode, add a new element and select System > Run code.
- In the Add Flow Elementform, from the Code run (JavaScript) list, select an existing code snippet or Add new code asset.
- If you selected to add a new code asset, in the Create new code asset dialog box, do the following:
- Enter a name for the asset.
- (Optional) Enter a description.
Edit the basic, default code snippet by modifying the function between the curly brackets as needed.
Note:The code needs to be syntactically correct and error-free.
Ideally, you want to run generic code that you can reuse for different purposes based on varying parameters, supplying the argument keys at runtime, through the test step's Keys to send field.
You can implement logic by using IF statements. You can also run code that performs a validation.
- Click CREATE. Back in the Add Flow Element dialog box, the code asset you just created is now selected.
- Click ADD.
- To test the snippet, click Run.
If you use logic in your test, you can write the results to an error log.
- On the interactive modeling page, expand the step for which you want to check results.
- Select the Check result checkbox.
- In the Command Result field, enter a value, for example pass.
- Click Rerun to run the test again with the change you just made.
- When the run is complete, at the top right, click the information icon.
- In the System pane on the right, on the ERRORS tab, check for validation errors. If the command result is different from what you specified in step 3, this information is listed here.
If you use logic in your tests, you can write results to a variable.
- On the interactive modeling page, expand the step for which you want to write the results to a variable.
- Select the Push result to context checkbox.
- In the Name of Variable field, enter the name of the variable to which you want to push the results.
- From the Transformation list, select None.
- Click Rerun to run the test again with the change you just made.
- When the run is complete, at the top right, click the information icon.
- In the System pane on the right, on the VARIABLES tab, check for the variable name you entered in step 3. The variable value is the result of the test step.