Chai scripting language

You can use Chai as the default option for scripting in the Assert, Decision, and Function actions in tests, and in the Assert using Function actions in the message editor in HCL OneTest API. You can find information about the usage of the Chai scripting in these actions.

Chai overview

Chai is an assertion library for node.js and a browser that can be paired with any JavaScript testing framework. For more information, refer to the Chai Documentation.

With the introduction of the Chai Assertion Styles in HCL OneTest API 10.1.1, the following features are changed:
  • You can select Chai as an option for scripting in actions where the HCL OneTest API legacy scripting and ECMA scripting are supported.
  • You can access the response from previous test actions in the form of a JavaScript object by using the named variables response.
  • You can use the Chai function named as format that is used to check the format of the objects.
  • You do not have to explicitly tag or configure other actions to expose data.
The following Styles for the Chai assertions are supported:
  • Assert
  • Expect
  • Should
The following information provides details on the usage of the Chai assertion styles:

The Assert action is a conditional action that allows you to construct a list of expressions that determine whether a test proceeds. If the Assert action passes, the test continues. If the Assert action fails, the test fails and no further test actions are performed. See Assert.

A Decision is a conditional action, the body of which is essentially a function and whose result controls test flow. A Decision can have one of the two outcomes of true or false. See Decision.

A Function action runs a single function or composite set of functions from the registered set. Functions and expressions are loaded from a folder that is contained within the Classes folder in the project and is available to all users of the project. See Function.

You can set up assertions for the responses in tests and allow those messages that pass the defined criteria.

The format function can be used to check for the following supported schema formats in HCL OneTest API:
  • CHIPS
  • Fedwire
  • JSON
  • MIME
  • SWIFT
  • XML

Using the Assert Chai Style

The following examples show the usage of the Chai Assert Style:

var foo=true;
assert.isTrue(foo, "foo should be true");
assert.isAbove(5, 2, '5 is strictly greater than 2');
assert.isBelow(3, 6, '3 is strictly less than 6');
Examples of the Assert Style usage in test actions, when you want to identify responses that have a predefined header, can have the following syntax:
assert.isNotNull(response);
assert.isNotNull(response.header);
assert.isNotNull(response.header.Headers);
assert.exists(response);
assert.exists(response.header);
assert.exists(response.header.Headers);
An example to filter for messages from a specific email account can have the following syntax:
assert.equal(response.header.Headers['From'],'tester8640@gmail.com');
Examples of the Assert Style usage in test actions, when you want to identify responses that have content in the message based on a specific schema, can have the following syntax:
expect('{1:XXXXXXXXXXXX0000}{2:I101}{4::20:actual-}{5:{CHK:}}').to.be.format('swift');
expect(response.body.text).to.be.format('json');
expect(response.body.text).to.be.format('xml');
expect(response.body.text).to.be.format('mime');

Using the Expect Chai Style

The following examples show the usage of the Chai Expect Style:
expect(2).to.not.equal(1);
expect(2).to.equal(2);
expect(null).to.be.null;
expect([1, 2, 3]).to.have.lengthOf(3);
Examples of the Expect Style usage in test actions, when you want to filter the responses or incoming messages to match certain criteria, can have the following syntax:
expect(response.header.Headers).to.have.property('Date');
expect(response.header.Headers).to.have.property('From');
expect(response.header.Headers).to.have.property('Content-Type');
expect(response.header.Headers['Content-Type']).to.equal('text/plain; charset=UTF-8');

Using the Should Chai Style

The following examples show the usage of the Chai Should Style:
var name=null;
should.not.exist(name);
Examples of the Should Style usage in test actions, when you want to filter the responses or incoming messages to match certain criteria, can have the following syntax:
should.exist(response);
should.exist(response.header);