ScriptBuilder Execute Code Instruction

Prev Next

The Execute code instruction can run a Javascript code during the automated test run.

You can use the Execute code instruction to -

  • Run generic JavaScript code functions, such as API calls

  • Access or modify the browser DOM and display alerts with Web API functions such as document.getElementById, alert, and others.

You can use the following shortcuts in your JavaScript code - httpGet, httpPost, and PanayaVars.

This instruction is available for web application scripts only.

Note

Due to security restrictions calling API including login credentials or api keys is not permitted. Instead, execute a program that will handle such calls.

Important!

The instruction will fail if any of the following occur while running the JavaScript code -

  • An exception is thrown

  • Invalid JavaScript syntax is detected

httpGet

Our httpGet shortcut for HTTP GET requires a URL and returns a string.

Example -Pull Exchange Rate Value

var currencyJsonResult = httpGet('https://api.frankfurter.app/latest?from=USD&to=GBP'); //Receive the currency exchange rate value

      //Example of result:  {"amount":1.0,"base":"USD","date":"2024-06-25","rates":{"GBP":0.78836}}
 

var jsobObj = JSON.parse(currencyJsonResult);

var rateGbpToUsd = jsobObj.rates.GBP; //Extract the GBP to USD exchange rate value from results using JS parser

if (rateGbpToUsd > 0) //If the rate is larger than 0, then 

{

    panayaVars.productPrice = rateGbpToUsd*panayaVars.productPrice; //Populate the <productPrice> parameter. 
//productPrice is a predefined Panaya parameter

}

httpPost

Our httpPost shortcut for HTTP POST requires the following two parameters and returns a string. -

  • URL of the page that contains the data

  • Data sent as application/x-www-form-urlencoded (the keys and values are encoded in key-value tuples separated by ‘&', with a ‘=' between the key and the value. Non-alphanumeric characters in both keys and values should be URL encoded) E.g.: id=123&category=book

Good to Know!

If you need to extract a Javascript object from the response, use - JSON.parse(response).

PanayaVars

The script can access Panaya Variables (Read or Write) using the following syntax -
panayaVars.parameterName.

In the example above, the parameterName is the name of the parameter defined in the script using the Set parameter instruction. When the script execution is complete, Panaya variables are sent back to the ScriptBuilder / Agent application with their current value.

Use the syntax below to read a Panaya variable -

JSON.parse(response)

var scriptPrice = panayaVars.price * 5;
The price is the parameter defined in the application.

Use the syntax below to write a Panaya variable -

panayaVars.price = 100.25;

The price is the parameter defined in the application.

Important!

When utilizing panayaVars in Javascript, use the exact capitalization case for the parameter names.

AI-generated JavaScript Code

You can use AI to automatically generate JavaScript code that can be used inside your automated test scripts.

In the Execute Code pop-up, click the Create button and use plain English to write a prompt for Panaya to generate the JavaScript code using AI.

Good to know!

AI-generated JavaScript Code Explanation

You can use the new “Explain” button for JavaScript in your automated scripts. Panaya uses AI-generated explanations that will make your work a whole lot easier.

In the execute code pop-up with existing JavaScript code, click the Explain button for Panaya to use AI and generate a clear explanation of what the code does.