JavaScript functions in Apimation

JavaScript function calls can be used anywhere data needs to be either generated or processed in more advanced way. Currently Apimation supports standard JavaScript and Node.js.

Example

test: case
name: Retrieve user
setName: user
casedetails:
  vars:
    $userBanned: $assertUserData.userBanned
  jsVars:
    $assertUserDescription: "Tests/UserTests/js/getUserDescription.js"
  nodeJsVars:
    $assertUserData: "Tests/UserTests/js/getUserData.js"
. . .

JavaScript

To start using JavaScript in your tests you need to provide path to JavaScript file. In order to obtain return value from defined function it must be called within your JS file, otherwise 'no value' is written as result. Thus, you can have multiple function definitions. Moreover, only one function call result can be written to variable. Even if you have multiple function calls in your file, only last one will be used. Provided example test case includes JavaScript file getUserDescription.js with following content:

getUserDescription();

function getUserDescription() {
    return "User descripton"
}

Node.js

First of all make sure Node.js is installed on your machine (at least 10.15.0 LTS recommended) To start using Node.js in your tests you need to provide absolute path to Node.js file in nodeJsVars. Then it is possible to call in test case or test step:

  • a variable ($nodeJsVar.nodeVar);
  • a function ($nodeJsVar.nodeFunction());
  • an object with its functions ($nodeJsVar.object.nodeFunction()) and properties($nodeJsVar.object.property).

Provided example test case includes Node.js file with following content:

var userBanned = false

var userPassword = {
    password: randomInt(5000,7000),
    getEncrypted: func(){
        return "encrypted password"
    }
}

function randomInt(low, high) {
  return Math.floor(Math.random() * (high - low) + low)
}

It is possible to use any additional Node.js libraries. Make sure you have node-modules in your Apimation project or provide a full (absolute) path to library within your Node.js code:

var randomString = require("randomstring");
var userPassword = {
    password: randomString.generate(16)
}

Usage of jsVars and nodeJSVars

There are several methods how to put JavaScript variables into your tests.

Put in case variable list

When JavaScript (or Node.js) variable is put as case variable, its value will be generated only once. Then, for example, whenever $userDescription is used, the value will stay same.

  test: case
  . . .
  vars:
    $userDescription: $assertUserDescription
  jsVars:
    $assertUserDescription: "<path to project dir>/Tests/UserTests/js/getUserDescription.js"

Put as position variables in loops

When JavaScript (or Node.js) variable is put as position variable in test case loop, a new value will be generated for each iteration. In example below each of three looped test steps will have its own $userPassword value.

 test: case
 . . .
 nodeJsVars:
    $assertUserData: "<path to project dir>/Tests/UserTests/js/getUserData.js"
  loop:
    - range: 1
      count: 3
      listInPositions:
        $userPassword: ["$assertUserData.userPassword.password"]
 . . .

results matching ""

    No results matching ""