Menu

Using Magic Variables in Universal Agent

Overview

In Universal Agent, there are Magic Variables, which contains contextual values of the current execution that you can programmatically access via Execute Command. 

Note: Universal Agent only assigns contextual values to these variables at run time if the selected Executor is either node, python or python3. 

$PROJECT_DATA <string>

This variable contain values of the current executing project under JSON format. 

Example:

{
    "project_id":12345,
    "properties": { 
        "job_id":12345,
        "user_schedule": { 
            "id":12345,
            "name":"John Doe"
        }
    }
}

$TESTRUNS_LIST <string>

This variable contains a list of test runs that was scheduled to be executed by Universal Agent. The values are under JSON format.

Example:

[
    {
      "parentId": 843974,
      "parentType": "test-suite",
      "created": "2018-04-16T16:04:23.438Z",
      "links": [
        {
          "rel": "self",
          "href": ""
        }
      ],
      "id": 11595056,
      "name": "What's New section",
      "order": 2,
      "pid": "TR-2",
      "created_date": "2018-04-16T16:04:23.438Z",
      "last_modified_date": "2018-04-16T16:04:23.438Z",
      "properties": [
        {
          "field_id": 1,
          "field_name": "Field Name",
          "field_value": "1",
          "field_value_name": "1"
        }
      ],
      "test_case": {
        "automation_content": "sample.junit.CalculatorTest#testAddition",
        "links": [
          {
            "rel": "self",
            "href": ""
          }
        ],
        "id": 1,
        "name": "Test Case 1",
        "order": 1,
        "pid": "TC-1",
        "created_date": "2018-04-16T16:04:23.438Z",
        "last_modified_date": "2018-04-16T16:04:23.438Z",
        "properties": [
          {
            "field_id": 1,
            "field_name": "Field Name",
            "field_value": "1",
            "field_value_name": "1"
          }
        ],
        "web_url": "https://apitryout.qtestnet.com/p/1/portal/project#tab=testdesign&object=1&id=1",
        "test_steps": [
          {
            "links": [
              {
                "rel": "self",
                "href": ""
              }
            ],
            "id": 111,
            "description": "Test Step description",
            "expected": "Test Step description",
            "order": 1,
            "group": 0,
            "called_test_case_name": "string",
            "root_called_test_case_id": 0,
            "parent_test_step_id": 0,
            "called_test_case_id": 0
          }
        ],
        "parent_id": 2107628,
        "test_case_version_id": 11,
        "version": "string",
        "description": "Description of test case 1",
        "precondition": "Precondition of test case 1",
        "creator_id": 1,
        "agent_ids": [
          0
        ]
      },
      "test_case_version_id": 13765789,
      "creator_id": 5202
    },
    {
      "parentId": 843974,
      "parentType": "test-suite",
      "created": "2018-04-16T16:04:23.438Z",
      "links": [
        {
          "rel": "self",
          "href": ""
        }
      ],
      "id": 11595056,
      "name": "What's New section",
      "order": 2,
      "pid": "TR-2",
      "created_date": "2018-04-16T16:04:23.438Z",
      "last_modified_date": "2018-04-16T16:04:23.438Z",
      "properties": [
        {
          "field_id": 1,
          "field_name": "Field Name",
          "field_value": "1",
          "field_value_name": "1"
        }
      ],
      "test_case": {
        "automation_content": "sample.junit.CalculatorTest#testSubtraction",
        "links": [
          {
            "rel": "self",
            "href": ""
          }
        ],
        "id": 1,
        "name": "Test Case 2",
        "order": 1,
        "pid": "TC-1",
        "created_date": "2018-04-16T16:04:23.438Z",
        "last_modified_date": "2018-04-16T16:04:23.438Z",
        "properties": [
          {
            "field_id": 1,
            "field_name": "Field Name",
            "field_value": "1",
            "field_value_name": "1"
          }
        ],
        "web_url": "https://apitryout.qtestnet.com/p/1/portal/project#tab=testdesign&object=1&id=1",
        "test_steps": [
          {
            "links": [
              {
                "rel": "self",
                "href": ""
              }
            ],
            "id": 111,
            "description": "Test Step description",
            "expected": "Test Step description",
            "order": 1,
            "group": 0,
            "called_test_case_name": "string",
            "root_called_test_case_id": 0,
            "parent_test_step_id": 0,
            "called_test_case_id": 0
          }
        ],
        "parent_id": 2107628,
        "test_case_version_id": 11,
        "version": "string",
        "description": "Description of test case 1",
        "precondition": "Precondition of test case 1",
        "creator_id": 1,
        "agent_ids": [
          0
        ]
      },
      "test_case_version_id": 13765789,
      "creator_id": 5202
    }
]
 

$TESTCASES_AC <string>

This variable contains automation content, separated by a comma, of all the test cases that are scheduled to be executed by Universal Agent. Value of this variable is extracted from the $TESTRUNS_LIST variable and available for convenient use.

  • Note: automation content is used to identify automated test cases. So make sure this value is unique when assigning it to an automated test case.

Example:

sample.junit.CalculatorTest#testAddition,sample.junit.CalculatorTest#testSubtraction

Using Magic Variables

You can write code to access a magic variable in the Execute Command section of Universal Agent. Note that the selected executor must be either node, python or python3

Below are some examples of accessing magic variables at runtime, when the Universal Agent kicks off its execution.

Example 1: extract automation content of the scheduled test cases from $TESTCASES_AC variable

1. Open the Universal Agent execution page.

2. Go to the Execute Command section.

3. Select Executor as python.

3. Enter the code below in the Execute Command editor:

testcasesAC = $TESTCASE_AC;
arr = testcasesAC.split(",")
i = 1
for automation_content in arr:
  print "Test case automation_content #%d: %s " % (i, automation_content)
  i = i + 1

Your configuration will now look like the image below.

magic-var-example-1.png

Now, when your Universal Agent executes, it will print the output to the Universal Agent's Console Log, like below.

magic-var-execution-1.png

Example 2: retrieve information of user who scheduled the test execution from $PROJECT_DATA variable

1. Open the Universal Agent execution page.

2. Go to Execute Command section.

3. Select Executor as node.

3. Enter the code below in the Execute Command editor:

var projectData = JSON.parse($PROJECT_DATA);
console.log(`Project ID: ${projectData.project_id}`);
console.log(`Job ID: ${projectData.properties.job_id}`);
console.log(`User ID: ${projectData.properties.user_schedule.id}`);
console.log(`User Name: ${projectData.properties.user_schedule.name}`);

Your configuration now looks like the image below.

magic-var-example-2.png

Now, when your Universal Agent executes, it will print the output to the Console Log, like the image below. 

magic-var-execute-2.png 

Example 3: retrieve scheduled test cases from $TESTRUNS_LIST variable and print test case name and automation content to the console.

1. Open the Universal Agent execution page.

2. Go to the Execute Command section.

3. Select Executor as node.

3. Enter the code below in the Execute Command editor:

var testrunsList = JSON.parse($TESTRUNS_LIST);
// loop through the scheduled test runs list and print test case's name and automation content
for(var i = 0; i < testrunsList.length; i++) {
  console.log(`****** Test case #${i + 1} ******`);
  console.log(`Name: ${testrunsList[i].test_case.name}`);
  console.log(`Automation Content: ${testrunsList[i].test_case.automation_content}`);
}

Your configuration now looks like the image below.

magic-var-example-3.png

Now, when your Universal Agent executes, it will print the output to the Console Log as seen below.

testruns-list-magic-var-in-action.png

Powered by Zendesk