Scripts

PruTAN provides JavaScript APIs that can be used in writing pre-request scripts and building tests. You can enter your JavaScript code and run necessary scripts.

The xv object

The xv,* object provides access to request and response data and variables in the your PruTAN instance.

The xv object houses the following methods

xv.env.set("variable", "value")

xv.env.set() can be used directly for quick and convenient environment variables definition

xv.env.set("baseURL", "https://httpbin.org");

xv.expect(value)

The expect method returns an expectation object, on which you can call matcher functions.

The example below calls the matcher function toBe on the expectation object that is returned by calling xv.expect with the response id, xv.response.body.id as an argument.

Use xv.expect directly for quick and convenient testing. Every xv.expect statement will generate a line on the test report.

// This test will pass
xv.expect(1).toBe(1);

// This test will fail
xv.expect(2).not.toBe(2);

xv.toBe(value)

Test for exact equality using toBe.

xv.expect(xv.response.body.category).toBe("Sneakers");

toBe uses strict equality and is recommended for primitive data types.

// These tests will fail
xv.expect("hello").toBe("Hello");
xv.expect(5).toBe("5");
xv.expect([]).toBe([]);

xv.toBeLevelxxx()

There are four different matcher functions for quick and convenient testing of the http status code that is returned:

  • toBeLevel2xx()
  • toBeLevel3xx()
  • toBeLevel4xx()
  • toBeLevel5xx()

For example, an argument passed to expect must be within 200 and 299 inclusive to pass toBeLevel2xx().

// These tests will pass
xv.expect(204).toBeLevel2xx();
xv.expect(308).toBeLevel3xx();
xv.expect(404).toBeLevel4xx();
xv.expect(503).toBeLevel5xx();

If the argument passed to expect() is a non-numeric value, it is first parsed with parseInt().

// This test will pass
xv.expect("404").toBeLevel4xx();

xv.response

Assert response data by accessing the xv.response object.

// This test will pass
xv.test("Response is ok", () => {
  xv.expect(xv.response.status).toBe(200);
});

Currently supports the following response values:

  • status: -number- The status code as an integer.
  • headers: -object- The response headers.
  • body: -object- the data in the response. In many requests, this is the JSON sent by the server.

Read more

Scripts

Read about pre-request scripts.

Tests

Read about post-request tests.

Environments

Read about environments.