Code assets
Scriptless Web lets you run JavaScript code while the test is running in the browser's context. This article explains how to create and use a code snippet. For more information, see the Inject code tutorial.
To create a code snippet:
-
In the Assets view, in the left pane, click Code Assets.
-
On the right, click Add Code.
-
In the Create a code asset dialog box, provide the following:
-
The asset's name
-
(Optional) A description of up to 100 characters
-
Your JavaScript code
The dialog box includes sample code to help you get started.
-
- Click CREATE.
To use a code asset in your test:
-
On the canvas, in Advanced mode, add a new step and select System > Run code.
-
In the Add Flow Element dialog box, from the Code to run (JavaScript) list, select the code asset you created.
-
If you want to validate the result, do the following:
-
Select the Check result check box.
-
In the Command Result field, enter a string value.
Important: If the code asset returns a value of a different type, the result check will fail because it expects a value of typestring
. In this case, consider modifying the code asset to convert the returned value tostring
.For example, the following code returns a value of type
boolean
.Copylet variable1 = true;
function variable2() {
return variable1;
}
return variable2();Adding the following line to the code asset will convert the
boolean
value to astring
so that the result check will succeed.Copyreturn variable2().toString();
-
-
If you want to use the result as a context variable later:
-
Select the Push value to context check box.
-
Select a value from theTransformationlist. For more information, see context variables.
-
- Click ADD.
To edit a code snippet:
-
Hover over a code and click Edit .
-
In the Update a code asset dialog box, provide the following:
-
The asset's name
-
(Optional) A description of up to 100 characters
-
Your JavaScript code
The dialog box includes sample code to help you get started.
-
- Click UPDATE.
To delete a code snippet:
-
Hover over a code snippet and click Delete .