webship-js 1.0.43 → 1.0.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +0 -0
- package/CNAME +0 -0
- package/dist/nightwatch.conf.dev.js +93 -0
- package/examples/about-us.html +0 -0
- package/examples/contact-us.html +0 -0
- package/examples/index.html +0 -0
- package/examples/test--then--i-should--see-text-in-element.html +0 -0
- package/examples/test--then--i-should--see-text-matching-pattern.html +0 -0
- package/examples/test--then--the-checkbox-checked.html +0 -0
- package/examples/test--then--the-element-should_not-contain-cssproperty.html +0 -0
- package/examples/test--then--the-link-should-contain.html +0 -0
- package/examples/test--when--i-attach-file-to-feild.html +0 -0
- package/examples/test--when--i-check-uncheck-checkbox.html +0 -0
- package/examples/test--when--i-click-link.html +0 -0
- package/examples/test--when--i-press-button.html +0 -0
- package/examples/test--when--i-reload-page.html +0 -0
- package/examples/test--when--i-scroll-functionality.html +0 -0
- package/examples/test--when--i-scroll-left-right-functionality.html +0 -0
- package/examples/test--when--i-select-option-from-dropdownlist.html +0 -0
- package/examples/test--when-i-wait-max-of-time.html +0 -0
- package/examples/test--when-i-wait-page-until-is-loaded.html +0 -0
- package/examples/test--when-i-wait-specific-time.html +0 -0
- package/generate-reports.js +0 -0
- package/nightwatch.conf.js +1 -1
- package/package.json +1 -1
- package/tests/assets/webshipco.png +0 -0
- package/tests/features/api-step-definitions-individual.feature +185 -0
- package/tests/features/complete-api-testing-examples.feature +205 -0
- package/tests/features/test--given--i-am-on-homepage.feature +2 -2
- package/tests/features/test--then--i-should--not--see-text-in-element.feature +2 -2
- package/tests/features/test--then--i-should--see-text-in-element-by-attr.feature +2 -2
- package/tests/features/test--then--i-should--see-text-in-element.feature +2 -2
- package/tests/features/test--then--i-should--see-text-matching-pattern-in-element.feature +2 -2
- package/tests/features/test--then--i-should--see-text-matching-pattern.feature +2 -2
- package/tests/features/test--then--i-should-be-on-page.feature +2 -2
- package/tests/features/test--then--i-should-be-on-the-homepage.feature +2 -2
- package/tests/features/test--then--i-should-not-see-an-element-by-attr.feature +2 -2
- package/tests/features/test--then--i-should-not-see-an-element.feature +2 -2
- package/tests/features/test--then--i-should-see-an-element-by-attr.feature +2 -2
- package/tests/features/test--then--i-should-see-an-element.feature +2 -2
- package/tests/features/test--then--i-should-see-text-in-table-row.feature +2 -2
- package/tests/features/test--then--the-checkbox-is-checked.feature +2 -2
- package/tests/features/test--then--the-checkbox-should-be-checked.feature +2 -2
- package/tests/features/test--then--the-element-should-contain-cssproperty.feature +2 -2
- package/tests/features/test--then--the-element-should-not-contain-cssproperty.feature +2 -2
- package/tests/features/test--then--the-field-should-contain-value.feature +2 -2
- package/tests/features/test--then--the-response-should-contain.feature +2 -2
- package/tests/features/test--then--the-response-status-code-should-be.feature +2 -2
- package/tests/features/test--then--the-url-should-match.feature +2 -2
- package/tests/features/test--then-the-link-should-contain-by-attribute.feature +2 -2
- package/tests/features/test--then-the-link-should-contain.feature +2 -2
- package/tests/features/test--when--i-attach-file-to-feild.feature +2 -2
- package/tests/features/test--when--i-check-checkbox.feature +2 -2
- package/tests/features/test--when--i-click-link-by-attribute.feature +2 -2
- package/tests/features/test--when--i-click-link.feature +2 -2
- package/tests/features/test--when--i-click-text-in-table-row.feature +21 -21
- package/tests/features/test--when--i-fill-in-by-attr.feature +2 -2
- package/tests/features/test--when--i-fill-in-table-by-attr.feature +2 -2
- package/tests/features/test--when--i-fill-in.feature +2 -2
- package/tests/features/test--when--i-go-to-homepage.feature +3 -3
- package/tests/features/test--when--i-move-forward-backward-one-page.feature +2 -2
- package/tests/features/test--when--i-press-button-by-attribute.feature +2 -2
- package/tests/features/test--when--i-press-button.feature +2 -2
- package/tests/features/test--when--i-reload-page.feature +2 -2
- package/tests/features/test--when--i-scroll-left-right.feature +2 -2
- package/tests/features/test--when--i-scroll.feature +2 -2
- package/tests/features/test--when--i-select-option-from-dropdownlist.feature +2 -2
- package/tests/features/test--when--i-wait-page-is-loaded.feature +2 -2
- package/tests/features/test--when--i-wait-time.feature +2 -2
- package/tests/step-definitions/webship-api.js +535 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
const { Given, When, Then } = require('@cucumber/cucumber');
|
|
2
|
+
const { Before, After, BeforeStep, AfterStep } = require('@cucumber/cucumber');
|
|
3
|
+
const axios = require('axios');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
|
|
6
|
+
// Global variables to store API response and request data
|
|
7
|
+
let apiResponse = null;
|
|
8
|
+
let apiRequestData = null;
|
|
9
|
+
let apiHeaders = {};
|
|
10
|
+
let baseURL = '';
|
|
11
|
+
let authorization = '';
|
|
12
|
+
let placeHolders = {};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Safely pauses based on global config for API calls.
|
|
16
|
+
* @param {string} key
|
|
17
|
+
*/
|
|
18
|
+
function safePause(key) {
|
|
19
|
+
const time = (global.browser?.globals?.minimum_wait_time?.[key]) || 0;
|
|
20
|
+
if (time > 0) {
|
|
21
|
+
return new Promise(resolve => setTimeout(resolve, time));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
Before(async function () {
|
|
26
|
+
await safePause('before_scenario');
|
|
27
|
+
// Reset API state before each scenario
|
|
28
|
+
apiResponse = null;
|
|
29
|
+
apiRequestData = null;
|
|
30
|
+
apiHeaders = {};
|
|
31
|
+
authorization = '';
|
|
32
|
+
placeHolders = {};
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
After(async function () {
|
|
36
|
+
await safePause('after_scenario');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
BeforeStep(async function () {
|
|
40
|
+
await safePause('before_step');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
AfterStep(async function () {
|
|
44
|
+
await safePause('after_step');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Helper function to replace placeholders in text
|
|
49
|
+
*/
|
|
50
|
+
function replacePlaceHolder(string) {
|
|
51
|
+
for (const [key, value] of Object.entries(placeHolders)) {
|
|
52
|
+
string = string.replace(new RegExp(key, 'g'), value);
|
|
53
|
+
}
|
|
54
|
+
return string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Helper function to prepare URL by replacing placeholders and trimming slashes
|
|
59
|
+
*/
|
|
60
|
+
function prepareUrl(url) {
|
|
61
|
+
return replacePlaceHolder(url).replace(/^\/+/, '');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Helper function to get nested property from object using dot notation.
|
|
66
|
+
*/
|
|
67
|
+
function getNestedProperty(obj, path) {
|
|
68
|
+
return path.split('.').reduce((current, prop) => {
|
|
69
|
+
return current && current[prop] !== undefined ? current[prop] : undefined;
|
|
70
|
+
}, obj);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Adds Basic Authentication header to next request.
|
|
75
|
+
*
|
|
76
|
+
* Example #1: Given I am authenticating as "admin" with "password123" password
|
|
77
|
+
* Example #2: Given I am authenticating as "user@example.com" with "secret" password
|
|
78
|
+
*/
|
|
79
|
+
Given(/^I am authenticating as "([^"]*)" with "([^"]*)" password$/, function (username, password) {
|
|
80
|
+
delete apiHeaders['Authorization'];
|
|
81
|
+
authorization = Buffer.from(username + ':' + password).toString('base64');
|
|
82
|
+
apiHeaders['Authorization'] = 'Basic ' + authorization;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Sets a HTTP Header with value.
|
|
87
|
+
*
|
|
88
|
+
* Example #1: Given I set header "Content-Type" with value "application/json"
|
|
89
|
+
* Example #2: Given I set header "Authorization" with value "Bearer token123"
|
|
90
|
+
* Example #3: Given I set header "Accept" with value "application/xml"
|
|
91
|
+
*/
|
|
92
|
+
Given(/^I set header "([^"]*)" with value "([^"]*)"$/, function (name, value) {
|
|
93
|
+
apiHeaders[name] = replacePlaceHolder(value);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Set the base URL for API calls (alternative syntax).
|
|
98
|
+
*
|
|
99
|
+
* Example #1: Given the API base URL is "https://jsonplaceholder.typicode.com"
|
|
100
|
+
* Example #2: Given I set the API base URL to "https://api.example.com/v1"
|
|
101
|
+
* Example #3: Given the base URL is "http://localhost:3000/api"
|
|
102
|
+
*/
|
|
103
|
+
Given(/^(?:the API base URL is|I set the API base URL to|the base URL is) "([^"]*)"$/, function (url) {
|
|
104
|
+
baseURL = url.replace(/\/$/, ''); // Remove trailing slash
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Set request headers for API calls (alternative syntax).
|
|
109
|
+
*
|
|
110
|
+
* Example #1: Given I set the header "Content-Type" to "application/json"
|
|
111
|
+
* Example #2: Given I set the header "Authorization" to "Bearer token123"
|
|
112
|
+
* Example #3: Given the header "Accept" is "application/json"
|
|
113
|
+
*/
|
|
114
|
+
Given(/^(?:I set the header|the header) "([^"]*)" (?:to|is) "([^"]*)"$/, function (headerName, headerValue) {
|
|
115
|
+
apiHeaders[headerName] = replacePlaceHolder(headerValue);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Set multiple headers using a table.
|
|
120
|
+
*
|
|
121
|
+
* Example: Given I set the following headers:
|
|
122
|
+
* | Content-Type | application/json |
|
|
123
|
+
* | Authorization | Bearer token123 |
|
|
124
|
+
* | Accept | application/json |
|
|
125
|
+
*/
|
|
126
|
+
Given(/^I set the following headers:$/, function (table) {
|
|
127
|
+
table.rows().forEach(row => {
|
|
128
|
+
apiHeaders[row[0]] = replacePlaceHolder(row[1]);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Set request body data for POST/PUT requests.
|
|
134
|
+
*
|
|
135
|
+
* Example #1: Given I set the request body to '{"name": "John", "email": "john@example.com"}'
|
|
136
|
+
* Example #2: Given the request body is '{"title": "Test Post", "body": "This is a test"}'
|
|
137
|
+
*/
|
|
138
|
+
Given(/^(?:I set the request body to|the request body is) '([^']*)'$/, function (jsonData) {
|
|
139
|
+
try {
|
|
140
|
+
const processedData = replacePlaceHolder(jsonData);
|
|
141
|
+
apiRequestData = JSON.parse(processedData);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
throw new Error(`Invalid JSON in request body: ${error.message}`);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Set request body using a table.
|
|
149
|
+
*
|
|
150
|
+
* Example: Given I set the request body with:
|
|
151
|
+
* | name | John Doe |
|
|
152
|
+
* | email | john@example.com |
|
|
153
|
+
* | age | 30 |
|
|
154
|
+
*/
|
|
155
|
+
Given(/^I set the request body with:$/, function (table) {
|
|
156
|
+
apiRequestData = {};
|
|
157
|
+
table.rows().forEach(row => {
|
|
158
|
+
let value = replacePlaceHolder(row[1]);
|
|
159
|
+
|
|
160
|
+
// Try to parse as number or boolean, otherwise keep as string
|
|
161
|
+
if (value === 'true') value = true;
|
|
162
|
+
else if (value === 'false') value = false;
|
|
163
|
+
else if (!isNaN(value) && !isNaN(parseFloat(value))) value = parseFloat(value);
|
|
164
|
+
|
|
165
|
+
apiRequestData[row[0]] = value;
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Sends HTTP request to specific relative URL.
|
|
171
|
+
*
|
|
172
|
+
* Example #1: When I send a GET request to "/users"
|
|
173
|
+
* Example #2: When I send a POST request to "/posts"
|
|
174
|
+
* Example #3: When I send a PUT request to "/users/1"
|
|
175
|
+
* Example #4: When I send a DELETE request to "/posts/1"
|
|
176
|
+
*/
|
|
177
|
+
When(/^(?:I )?send a ([A-Z]+) request to "([^"]+)"$/, async function (method, endpoint) {
|
|
178
|
+
const url = baseURL + '/' + prepareUrl(endpoint);
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
apiResponse = await axios({
|
|
182
|
+
method: method,
|
|
183
|
+
url: url,
|
|
184
|
+
headers: apiHeaders,
|
|
185
|
+
data: apiRequestData,
|
|
186
|
+
validateStatus: function (status) {
|
|
187
|
+
return true; // Don't throw on any status code
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw new Error(`${method} request failed: ${error.message}`);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Sends HTTP request to specific URL with field values from Table.
|
|
197
|
+
*
|
|
198
|
+
* Example: When I send a POST request to "/users" with values:
|
|
199
|
+
* | name | John Doe |
|
|
200
|
+
* | email | john@example.com |
|
|
201
|
+
* | age | 30 |
|
|
202
|
+
*/
|
|
203
|
+
When(/^(?:I )?send a ([A-Z]+) request to "([^"]+)" with values:$/, async function (method, endpoint, table) {
|
|
204
|
+
const url = baseURL + '/' + prepareUrl(endpoint);
|
|
205
|
+
const fields = {};
|
|
206
|
+
|
|
207
|
+
const rowsHash = table.rowsHash();
|
|
208
|
+
Object.keys(rowsHash).forEach(key => {
|
|
209
|
+
fields[key] = replacePlaceHolder(rowsHash[key]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
apiResponse = await axios({
|
|
214
|
+
method: method,
|
|
215
|
+
url: url,
|
|
216
|
+
headers: { ...apiHeaders, 'Content-Type': 'application/json' },
|
|
217
|
+
data: JSON.stringify(fields),
|
|
218
|
+
validateStatus: function (status) {
|
|
219
|
+
return true; // Don't throw on any status code
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw new Error(`${method} request with values failed: ${error.message}`);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Sends HTTP request to specific URL with raw body from PyString.
|
|
229
|
+
*
|
|
230
|
+
* Example: When I send a POST request to "/users" with body:
|
|
231
|
+
* """
|
|
232
|
+
* {
|
|
233
|
+
* "name": "John Doe",
|
|
234
|
+
* "email": "john@example.com"
|
|
235
|
+
* }
|
|
236
|
+
* """
|
|
237
|
+
*/
|
|
238
|
+
When(/^(?:I )?send a ([A-Z]+) request to "([^"]+)" with body:$/, async function (method, endpoint, docString) {
|
|
239
|
+
const url = baseURL + '/' + prepareUrl(endpoint);
|
|
240
|
+
const body = replacePlaceHolder(docString.trim());
|
|
241
|
+
|
|
242
|
+
// Parse JSON if the body looks like JSON, otherwise send as string
|
|
243
|
+
let requestData = body;
|
|
244
|
+
try {
|
|
245
|
+
// Check if it's JSON by trying to parse it
|
|
246
|
+
requestData = JSON.parse(body);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
// If parsing fails, use the raw string
|
|
249
|
+
requestData = body;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Set appropriate Content-Type header if not already set
|
|
253
|
+
const headers = { ...apiHeaders };
|
|
254
|
+
if (!headers['Content-Type'] && !headers['content-type']) {
|
|
255
|
+
if (typeof requestData === 'object') {
|
|
256
|
+
headers['Content-Type'] = 'application/json';
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
apiResponse = await axios({
|
|
262
|
+
method: method,
|
|
263
|
+
url: url,
|
|
264
|
+
headers: headers,
|
|
265
|
+
data: requestData,
|
|
266
|
+
validateStatus: function (status) {
|
|
267
|
+
return true; // Don't throw on any status code
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
} catch (error) {
|
|
271
|
+
throw new Error(`${method} request with body failed: ${error.message}`);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Sends HTTP request to specific URL with form data.
|
|
277
|
+
*
|
|
278
|
+
* Example: When I send a POST request to "/login" with form data:
|
|
279
|
+
* """
|
|
280
|
+
* username=admin
|
|
281
|
+
* password=secret
|
|
282
|
+
* remember=true
|
|
283
|
+
* """
|
|
284
|
+
*/
|
|
285
|
+
When(/^(?:I )?send a ([A-Z]+) request to "([^"]+)" with form data:$/, async function (method, endpoint, docString) {
|
|
286
|
+
const url = baseURL + '/' + prepareUrl(endpoint);
|
|
287
|
+
const body = replacePlaceHolder(docString.trim());
|
|
288
|
+
|
|
289
|
+
// Parse form data
|
|
290
|
+
const fields = {};
|
|
291
|
+
body.split('\n').forEach(line => {
|
|
292
|
+
const [key, value] = line.split('=');
|
|
293
|
+
if (key && value !== undefined) {
|
|
294
|
+
fields[key.trim()] = value.trim();
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Convert to URL encoded format
|
|
299
|
+
const formData = Object.keys(fields)
|
|
300
|
+
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(fields[key]))
|
|
301
|
+
.join('&');
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
apiResponse = await axios({
|
|
305
|
+
method: method,
|
|
306
|
+
url: url,
|
|
307
|
+
headers: {
|
|
308
|
+
...apiHeaders,
|
|
309
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
310
|
+
},
|
|
311
|
+
data: formData,
|
|
312
|
+
validateStatus: function (status) {
|
|
313
|
+
return true; // Don't throw on any status code
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
} catch (error) {
|
|
317
|
+
throw new Error(`${method} request with form data failed: ${error.message}`);
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Checks that API response has specific status code.
|
|
323
|
+
*
|
|
324
|
+
* Example #1: Then the API response code should be 200
|
|
325
|
+
* Example #2: Then API response code should be 404
|
|
326
|
+
*/
|
|
327
|
+
Then(/^(?:the )?API response code should be (\d+)$/, function (expectedCode) {
|
|
328
|
+
if (!apiResponse) {
|
|
329
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const expected = parseInt(expectedCode);
|
|
333
|
+
const actual = parseInt(apiResponse.status);
|
|
334
|
+
assert.strictEqual(actual, expected,
|
|
335
|
+
`Expected status code ${expected}, but got ${actual}`);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Checks that API response body contains specific text.
|
|
340
|
+
*
|
|
341
|
+
* Example #1: Then the API response should contain "success"
|
|
342
|
+
* Example #2: Then API response should contain "John Doe"
|
|
343
|
+
*/
|
|
344
|
+
Then(/^(?:the )?API response should contain "([^"]*)"$/, function (expectedText) {
|
|
345
|
+
if (!apiResponse) {
|
|
346
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const responseBody = typeof apiResponse.data === 'string' ?
|
|
350
|
+
apiResponse.data : JSON.stringify(apiResponse.data);
|
|
351
|
+
|
|
352
|
+
const expectedRegexp = new RegExp(expectedText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
|
|
353
|
+
assert(expectedRegexp.test(responseBody),
|
|
354
|
+
`Response should contain "${expectedText}", but got: ${responseBody}`);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Checks that API response body doesn't contain specific text.
|
|
359
|
+
*
|
|
360
|
+
* Example #1: Then the API response should not contain "error"
|
|
361
|
+
* Example #2: Then API response should not contain "failed"
|
|
362
|
+
*/
|
|
363
|
+
Then(/^(?:the )?API response should not contain "([^"]*)"$/, function (unexpectedText) {
|
|
364
|
+
if (!apiResponse) {
|
|
365
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const responseBody = typeof apiResponse.data === 'string' ?
|
|
369
|
+
apiResponse.data : JSON.stringify(apiResponse.data);
|
|
370
|
+
|
|
371
|
+
const expectedRegexp = new RegExp(unexpectedText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
372
|
+
assert(!expectedRegexp.test(responseBody),
|
|
373
|
+
`Response should not contain "${unexpectedText}", but it does. Response: ${responseBody}`);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Checks that API response body contains JSON from PyString.
|
|
378
|
+
*
|
|
379
|
+
* Example: Then the API response should contain json:
|
|
380
|
+
* """
|
|
381
|
+
* {
|
|
382
|
+
* "name": "John Doe",
|
|
383
|
+
* "email": "john@example.com"
|
|
384
|
+
* }
|
|
385
|
+
* """
|
|
386
|
+
*/
|
|
387
|
+
Then(/^(?:the )?API response should contain json:$/, function (docString) {
|
|
388
|
+
if (!apiResponse) {
|
|
389
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
let expectedJson;
|
|
393
|
+
let actualJson;
|
|
394
|
+
|
|
395
|
+
try {
|
|
396
|
+
expectedJson = JSON.parse(replacePlaceHolder(docString.trim()));
|
|
397
|
+
} catch (error) {
|
|
398
|
+
throw new Error(`Cannot convert expected JSON: ${docString}`);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
try {
|
|
402
|
+
actualJson = typeof apiResponse.data === 'string' ?
|
|
403
|
+
JSON.parse(apiResponse.data) : apiResponse.data;
|
|
404
|
+
} catch (error) {
|
|
405
|
+
throw new Error(`Cannot convert actual response to JSON: ${apiResponse.data}`);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Check that expected properties exist in actual response and match values
|
|
409
|
+
Object.keys(expectedJson).forEach(key => {
|
|
410
|
+
assert(actualJson.hasOwnProperty(key),
|
|
411
|
+
`Expected property "${key}" not found in response. Actual response: ${JSON.stringify(actualJson, null, 2)}`);
|
|
412
|
+
assert.deepStrictEqual(actualJson[key], expectedJson[key],
|
|
413
|
+
`Property "${key}" mismatch. Expected: ${expectedJson[key]}, Actual: ${actualJson[key]}`);
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Alternative syntax for JSON property verification
|
|
419
|
+
*/
|
|
420
|
+
Then(/^(?:the JSON response should have|the API response should have|the JSON property) "([^"]*)" (?:equal to|should be) (.+)$/, function (propertyPath, expectedValue) {
|
|
421
|
+
if (!apiResponse) {
|
|
422
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Replace placeholders in expected value first
|
|
426
|
+
const processedExpectedValue = replacePlaceHolder(expectedValue);
|
|
427
|
+
|
|
428
|
+
// Parse expected value (remove quotes if string, parse numbers/booleans)
|
|
429
|
+
let parsedExpectedValue = processedExpectedValue;
|
|
430
|
+
if (processedExpectedValue.startsWith('"') && processedExpectedValue.endsWith('"')) {
|
|
431
|
+
parsedExpectedValue = processedExpectedValue.slice(1, -1);
|
|
432
|
+
} else if (processedExpectedValue === 'true') {
|
|
433
|
+
parsedExpectedValue = true;
|
|
434
|
+
} else if (processedExpectedValue === 'false') {
|
|
435
|
+
parsedExpectedValue = false;
|
|
436
|
+
} else if (!isNaN(processedExpectedValue) && !isNaN(parseFloat(processedExpectedValue))) {
|
|
437
|
+
parsedExpectedValue = parseFloat(processedExpectedValue);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Get the actual value from response
|
|
441
|
+
const actualValue = getNestedProperty(apiResponse.data, propertyPath);
|
|
442
|
+
|
|
443
|
+
assert.strictEqual(actualValue, parsedExpectedValue,
|
|
444
|
+
`Expected "${propertyPath}" to be ${parsedExpectedValue}, but got ${actualValue}`);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Verify a JSON property exists.
|
|
449
|
+
*
|
|
450
|
+
* Example #1: Then the JSON response should have property "id"
|
|
451
|
+
* Example #2: Then the API response should contain property "user.email"
|
|
452
|
+
*/
|
|
453
|
+
Then(/^(?:the JSON response should have property|the API response should contain property) "([^"]*)"$/, function (propertyPath) {
|
|
454
|
+
if (!apiResponse) {
|
|
455
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const actualValue = getNestedProperty(apiResponse.data, propertyPath);
|
|
459
|
+
|
|
460
|
+
assert(actualValue !== undefined,
|
|
461
|
+
`Expected property "${propertyPath}" to exist in response, but it doesn't`);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Verify a JSON property does not exist.
|
|
466
|
+
*
|
|
467
|
+
* Example #1: Then the JSON response should not have property "password"
|
|
468
|
+
* Example #2: Then the API response should not contain property "secret"
|
|
469
|
+
*/
|
|
470
|
+
Then(/^(?:the JSON response should not have property|the API response should not contain property) "([^"]*)"$/, function (propertyPath) {
|
|
471
|
+
if (!apiResponse) {
|
|
472
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const actualValue = getNestedProperty(apiResponse.data, propertyPath);
|
|
476
|
+
|
|
477
|
+
assert(actualValue === undefined,
|
|
478
|
+
`Expected property "${propertyPath}" not to exist in response, but it does`);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Verify the response is valid JSON.
|
|
483
|
+
*
|
|
484
|
+
* Example #1: Then the response should be valid JSON
|
|
485
|
+
* Example #2: Then the API response should be valid JSON
|
|
486
|
+
*/
|
|
487
|
+
Then(/^(?:the response should be valid JSON|the API response should be valid JSON)$/, function () {
|
|
488
|
+
if (!apiResponse) {
|
|
489
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
assert(typeof apiResponse.data === 'object' && apiResponse.data !== null,
|
|
493
|
+
'Response should be valid JSON');
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Verify response header value.
|
|
498
|
+
*
|
|
499
|
+
* Example #1: Then the response header "Content-Type" should be "application/json"
|
|
500
|
+
* Example #2: Then the header "Cache-Control" should contain "no-cache"
|
|
501
|
+
*/
|
|
502
|
+
Then(/^(?:the response header|the header) "([^"]*)" should (?:be|contain) "([^"]*)"$/, function (headerName, expectedValue) {
|
|
503
|
+
if (!apiResponse) {
|
|
504
|
+
throw new Error('No API response available. Make sure to send a request first.');
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const actualValue = apiResponse.headers[headerName.toLowerCase()];
|
|
508
|
+
|
|
509
|
+
assert(actualValue && actualValue.includes(expectedValue),
|
|
510
|
+
`Expected header "${headerName}" to contain "${expectedValue}", but got "${actualValue}"`);
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Prints last API response body for debugging.
|
|
515
|
+
*
|
|
516
|
+
* Example: Then print API response
|
|
517
|
+
*/
|
|
518
|
+
Then(/^print API response$/, function () {
|
|
519
|
+
if (!apiResponse) {
|
|
520
|
+
console.log('No API response available');
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
console.log(`${apiResponse.config.method.toUpperCase()} ${apiResponse.config.url} => ${apiResponse.status}:`);
|
|
525
|
+
console.log(typeof apiResponse.data === 'string' ? apiResponse.data : JSON.stringify(apiResponse.data, null, 2));
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Sets placeholder for replacement in URLs, requests, and responses.
|
|
530
|
+
*
|
|
531
|
+
* Example: Given I set placeholder "{{userId}}" to "123"
|
|
532
|
+
*/
|
|
533
|
+
Given(/^I set placeholder "([^"]*)" to "([^"]*)"$/, function (placeholder, value) {
|
|
534
|
+
placeHolders[placeholder] = value;
|
|
535
|
+
});
|