Using the API Monitoring AI Script Assistant

The API Monitoring AI Script Assistant introduces a streamlined method for generating and refining scripts across various sections of test settings and steps. This tool leverages natural language input to automate script creation.

Like other AI-assisted features, the Script Assistant requires explicit consent and opt-in. See Manage AI Consent in API Monitoring and the Perforce Generative AI Policy for more details.

 

On this page:

Example Queries

Want to start right away? Here are some example queries that you can try immediately with the AI Script Assistant:

  • Example: Get a BlazeMeter variable
    Ask: Get blazemeter variable "status" and print its value
  • Example: Set a BlazeMeter variable
    Ask: Set blazemeter variable "status" and print its value
  • Example: Clear a global variable
    Ask: Delete blazemeter variable "status" from initial variables
  • Example: Check that test or test step status code is 200
    Ask: Validate that status code is 200
  • Example: Check that response body contains a given string
    Ask: Check response body contains string "string_you_want_to_search"
  • Example: Check JSON for an attribute value
    Ask: Check response body "attribute_to_search" is "value_to_search"
  • Example: Check for successful execution of a POST request
    Ask: Validate that the response status code is either 201 or 202
  • Example: Convert XML response into JSON body
    Ask: Convert xml response into json
  • Example: Validate the response for JSON schema
    Ask: Validate for json schema

About the AI Script Assistant

  • The AI Script Assistant is accessible within the Initial Script, Pre-request Scripts, and Post-response Scripts sections.

  • The functionality is supported for both Test Settings (environment-level configuration) and individual Test Steps.

  • Input Interface: A UI-based form enables you to define script logic using descriptive instructions.

  • Only user-provided query input is sent to the AI model. No additional user or system data is transmitted.

  • If needed, AI features can be disabled by the team owner in the team settings.

Generate Scripts using the AI Script Assistant

  1. Navigate to Test Settings or Test Steps where scripting is required. The AI Script Assistant is available in the Initial Script, Pre-request Scripts, and Post-response Scripts sections.

  2. Click the AI Script Assistant button located in the script editor.

  3. A pop-up input form will appear. Enter a description of the script logic you want to generate, such as: “Get the last Sunday date.” Click Ask AI.

  4. The AI model will generate a script and insert it directly into the editor textbox. Review the output to ensure it aligns with your requirements.

  5. (Optional) If you would like to adjust the script, you can insert a new query and include the existing script in the prompt by selecting the Add Existing Script to Prompt box. Click Ask AI.

    See the Tips section to learn best practices about using JavaScript variables versus BlazeMeter (Runscope) variables with the AI Assistant.

  6. Validate the script, make any necessary adjustments, and save your configuration.

Free-tier users have a limit of 5 AI queries per day. This limit applies to the entire team, which in most cases consists of a single user.

Tips for writing good queries

  • Your query should be related to the JavaScript code or script generation in the context of API Monitoring tool only. For unrelated queries, the AI model may reject the query with this error message: "Unable to generate a valid response for the given input. Please ensure the input is related to generation of scripts."

  • If you wish to store or retrieve data in or from an API Monitoring variable, explicitly mention "blazemeter" in the query. For example: "Assert the response status code is 200 and assign it to a blazemeter variable." If BlazeMeter is not mentioned, the AI model will assign the value to a JavaScript variable. As a reminder, JavaScript variables and API Monitoring variables have different syntax.

Example AI responses

The following examples show unedited responses from the AI Script Assistant model based on natural language queries.

User query: Validate if POST request is successful

AI response:

Copy
if (request.method === 'POST' && response.status >= 200 && response.status < 300) { 
    log('POST request successful’); 
} else if (request.method === 'POST') { 
    log('POST request failed with status: ' + response.status);
}

User query: Convert XML response body to JSON

AI response:

Copy
var xmlString = response.body;
var jsonData = runscope_xml2json(xmlString);
log(jsonData);

User query: Get the last Sunday date

AI response:

Copy
function getLastSunday() {
    const today = new Date();
    const day = today.getDay();
    const diff = today.getDate() - day + (day === 0 ? -7 : 0);
    return new Date(today.setDate(diff));
}

const lastSunday = getLastSunday();
log(lastSunday);

User query: Extract a key 'timestamp' from the API response and add 1 day to it

AI response:

Copy
function addDayToTimestamp(timestamp) {
    const date = new Date(timestamp);
    date.setDate(date.getDate() + 1);
    return date.toISOString();
}

const timestamp = response.body.timestamp;

if (timestamp) {
    const newTimestamp = addDayToTimestamp(timestamp);
    log(newTimestamp);
} else {
    log('Timestamp not found in the response body.');
}

User query: Extract a key 'hex' from the API response and calculate its MD5 value

AI response:

Copy
function calculateMD5(key) {
    // Check if CryptoJS is available
    if (typeof CryptoJS === 'undefined') {
        throw new Error('CryptoJS library is not available.');
    }
    return CryptoJS.MD5(key).toString();
}

const hexKey = response.body.hex;

if (hexKey) {
    const md5Hash = calculateMD5(hexKey);
    log("MD5 Hash: " + md5Hash);
} else {
    log("Key 'hex' not found in the response body.");
}