Using Global Utility Functions
Number of APIs: 1
This is a basic Collection showing a method of storing a set of reusable functions at the Global Variable level and using these in your requests.
The following set of functions are store inside a Global Variable.
utils = {
randomString: function (minValue, maxValue, dataSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
if (!minValue) {
minValue = 10;
maxValue = 10;
}
if (!maxValue) {
maxValue = minValue;
}
let length = _.random(minValue, maxValue),
randomText = "";
for (let i = 0; i < length; i++)
randomText += dataSet.charAt(Math.floor(Math.random() * dataSet.length));
return randomText;
},
randomAlpha: function (minValue, maxValue, dataSet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
return utils.randomString(minValue, maxValue, dataSet);
},
formatDate: function (date, format = 'DD/MM/YYYY') {
let moment = require('moment');
return moment(date).format(format);
},
convertTimestamp: function (time, format = 'DD/MM/YYYY') {
let moment = require('moment');
return moment.unix(time).format(format);
},
isDateBefore: function (a, b) {
let moment = require('moment');
return moment(a).isBefore(b);
},
isDateAfter: function (a, b) {
let moment = require('moment');
return moment(a).isAfter(b);
},
checkResponseTime: (pm, ms) => {
pm.test(`Response time should be less than ${ms}`, () => {
pm.expect(pm.response.responseTime).lt(ms);
});
},
checkStatusCode: (pm, code, message = "Status code does not match") => {
pm.test(`Status code should be ${code}`, () => {
pm.expect(pm.response.code, message).to.equal(code);
});
}
};