-
Notifications
You must be signed in to change notification settings - Fork 1
/
pm_asserts.js
114 lines (99 loc) · 2.79 KB
/
pm_asserts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* This script defines global variables to provide Asserts for Postman unit test case Runner.
*/
/* global pm */
/* global tv4 */
/* global tests */
/* global postman */
/**
* Check if Response have JSON body
* @returns void
*/
function isJsonResponse() {
pm.test("Response should have JSON Body", function () {
pm.response.to.have.jsonBody();
});
}
/**
* Check if Response have expected Status Code
* @param {integer} statusCode
* @returns void
*/
function isResponseStatus(statusCode) {
pm.test("Status code is " + statusCode, function () {
pm.response.to.have.status(statusCode);
});
}
/**
* Validate JSON Response with Tiny Validator (tv4, built-in into Postman), using JSON Schema
* @param {object} schema - JSON Schema for Validation
* @returns void
*/
function validateJsonSchema(schema) {
tests["Response Data is Valid"] = tv4.validate(pm.response.json(), schema);
if (tv4.error !== null) {
tests["Validation ERROR => " + tv4.error.message] = "";
}
}
/**
* Check if two agruments are Equals.
* Optional: message - you can provide your custom text
* @param {any} expected
* @param {any} actual
* @param {string} message
* @returns void
*/
function isEquals(expected, actual, message) {
message = message || "Expected value is Equals to actual";
pm.test(message, function () {
pm.expect(expected).to.eql(actual);
});
}
/**
* Check if Raw Response Body Contains `needle`.
* Optional: message - you can provide your custom text
* @param {string} message
* @param {string} needle
* @returns void
*/
function isRawResponseBodyContains(needle, message) {
message = message || "Raw Response Body contains " + needle;
pm.test(message, function () {
pm.expect(pm.response.text()).to.include(needle);
});
}
/**
* Set up and Validate Variable from Response to local ENV
*
* @param {string} localVar - Postman ENV variable name
* @param {string | number | boolean} jsonValue - pm.response Object value
* @returns void
*/
function ensureEnv(localVar, jsonValue) {
pm.environment.unset(localVar);
pm.environment.set(localVar, jsonValue);
var envValue = pm.environment.get(localVar);
var message = "Variable is set to ENV: " + localVar + " = " + envValue;
pm.test(message, function () {
pm.expect(jsonValue).to.eql(envValue);
});
}
/**
* Set next request
*
* @param {string} requestName - the next request name
*/
function nextRequest(requestName){
postman.setNextRequest(requestName);
}
/**
* Log Header param to Test Result
*
* @param {string} key - header param key
* @param {string} message - key name for example
* @returns void
*/
function logHeader(key, message) {
message = message || "Header " + key + ":";
pm.test(message + " " + pm.request.headers.get(key));
}