Skip to content

Commit 44c529b

Browse files
Merge pull request #13 from qavajs/update-memory-integration
Update memory integration
2 parents f8e3c1a + 4eed0da commit 44c529b

File tree

9 files changed

+158
-203
lines changed

9 files changed

+158
-203
lines changed

package-lock.json

Lines changed: 108 additions & 126 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/steps-api",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "api steps for @qavajs project",
55
"main": "./index.js",
66
"scripts": {
@@ -31,9 +31,9 @@
3131
"homepage": "https://github.com/qavajs/steps-api#readme",
3232
"devDependencies": {
3333
"@babel/core": "^7.18.6",
34-
"@cucumber/cucumber": "^8.10.0",
34+
"@cucumber/cucumber": "^8.11.1",
3535
"@qavajs/cli": "^0.0.17",
36-
"@qavajs/memory": "^1.1.1",
36+
"@qavajs/memory": "^1.2.0",
3737
"@qavajs/xunit-formatter": "^0.0.3",
3838
"@types/babel__core": "^7.1.19",
3939
"@types/chai": "^4.3.1",
@@ -59,13 +59,13 @@
5959
"prettier": "^2.3.2",
6060
"ts-jest": "^28.0.5",
6161
"ts-node": "^10.9.1",
62-
"typescript": "^4.7.3"
62+
"typescript": "^4.9.5",
63+
"fs-extra": "^11.1.0"
6364
},
6465
"dependencies": {
6566
"@qavajs/api-service": "^0.0.3",
6667
"@qavajs/validation": "^0.0.2",
67-
"chai": "^4.3.6",
68-
"fs-extra": "^10.1.0",
68+
"chai": "^4.3.7",
6969
"node-fetch": "^2.6.1"
7070
}
7171
}

src/apiActionSteps.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ When(
2323
async function (method: string, url: string, headers: any, key: string) {
2424
const conf: RequestInit = {
2525
method,
26-
...headers,
26+
...(await headers),
2727
};
28-
const response = await sendHttpRequest(url, conf);
28+
const response = await sendHttpRequest(await url, conf);
2929

3030
// store response to memory to be able to use it in next steps
3131
memory.setValue(key, response);
@@ -47,11 +47,11 @@ When(
4747
When(
4848
'I send {string} request to {landingUrl}{headers} with qs {string} and save response as {string}',
4949
async function (method: string, url: string, headers: any, params: string, key: string) {
50-
params = memory.getValue(params);
51-
url = `${url}${params}`;
50+
params = await memory.getValue(params);
51+
url = `${await url}${params}`;
5252
const conf: RequestInit = {
5353
method,
54-
...headers,
54+
...(await headers),
5555
};
5656
const response = await sendHttpRequest(url, conf);
5757

@@ -82,10 +82,10 @@ When(
8282
async function (method: string, url: string, headers: any, requestBody: JSON, key: string) {
8383
const conf: RequestInit = {
8484
method,
85-
body: JSON.stringify(requestBody),
86-
...headers,
85+
body: JSON.stringify(await requestBody),
86+
...(await headers),
8787
};
88-
const response = await sendHttpRequest(url, conf);
88+
const response = await sendHttpRequest(await url, conf);
8989

9090
// store response to memory to be able to use it in next steps
9191
memory.setValue(key, response);
@@ -108,14 +108,14 @@ When(
108108
When(
109109
'I send {string} request to {landingUrl}{headers} with qs {string} and Body {json} and save response as {string}',
110110
async function (method: string, url: string, headers: any, params: string, requestBody: JSON, key: string) {
111-
params = memory.getValue(params);
112-
url = `${url}${params}`;
111+
params = await memory.getValue(params);
112+
url = `${await url}${params}`;
113113
const conf: RequestInit = {
114114
method,
115-
body: JSON.stringify(requestBody),
116-
...headers,
115+
body: JSON.stringify(await requestBody),
116+
...(await headers),
117117
};
118-
const response = await sendHttpRequest(url, conf);
118+
const response = await sendHttpRequest(await url, conf);
119119

120120
// store response to memory to be able to use it in next steps
121121
memory.setValue(key, response);
@@ -144,10 +144,10 @@ When(
144144
async function (method: string, key: string, url: string, headers: any, requestBody: JSON) {
145145
const conf: RequestInit = {
146146
method,
147-
body: requestBody,
148-
...headers,
147+
body: await requestBody,
148+
...(await headers),
149149
};
150-
const response = await sendHttpRequest(url, conf);
150+
const response = await sendHttpRequest(await url, conf);
151151

152152
// store response to memory to be able to use it in next steps
153153
memory.setValue(key, response);

src/apiVerificationSteps.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import memory from '@qavajs/memory';
1111
*
1212
* @param {String} statusCode should be valid status code
1313
*/
14-
Then('Response {response} Status Code {apiValidation} {string}', (response: any, validationType: string, statusCode: string) => {
14+
Then('Response {response} Status Code {apiValidation} {string}', async (response: any, validationType: string, statusCode: string) => {
1515
const validation = getValidation(validationType);
16-
statusCode = memory.getValue(statusCode);
16+
statusCode = await memory.getValue(statusCode);
1717
validation(response.status, parseInt(statusCode, 10));
1818
});
1919

@@ -25,9 +25,9 @@ Then('Response {response} Status Code {apiValidation} {string}', (response: any,
2525
*
2626
* @param {String} statusMessage should be valid status code
2727
*/
28-
Then('Response {response} Status Message {apiValidation} {string}', (response: any, validationType: string, statusMessage: string) => {
28+
Then('Response {response} Status Message {apiValidation} {string}', async (response: any, validationType: string, statusMessage: string) => {
2929
const validation = getValidation(validationType);
30-
statusMessage = memory.getValue(statusMessage);
30+
statusMessage = await memory.getValue(statusMessage);
3131
validation(response.statusText, statusMessage);
3232
});
3333

@@ -72,9 +72,9 @@ Then('Response {response} contains:', (property: any, dataTable: any) => {
7272
* @param {String} pathQuery json path
7373
* @param {String} type should be named as expected value type
7474
*/
75-
Then('Response {response} {apiValidation} {string}', (property: any, validationType: string, type: string) => {
75+
Then('Response {response} {apiValidation} {string}', async (property: any, validationType: string, type: string) => {
7676
const validation = getValidation(validationType);
77-
type = memory.getValue(type);
77+
type = await memory.getValue(type);
7878
validation(typeof property, type);
7979
});
8080

@@ -88,10 +88,10 @@ Then('Response {response} {apiValidation} {string}', (property: any, validationT
8888
* @param {String} action should be named as expected action (equal to|less than|greater)
8989
* @param {String} expectedValue Number for comparing with array size
9090
*/
91-
Then('Response {response} size {apiValidation} {string}', (property: any, validationType: string, expectedValue: string) => {
91+
Then('Response {response} size {apiValidation} {string}', async (property: any, validationType: string, expectedValue: string) => {
9292
const validation = getValidation(validationType);
9393
const count = property.length;
94-
expectedValue = memory.getValue(expectedValue);
94+
expectedValue = await memory.getValue(expectedValue);
9595
validation(count, expectedValue);
9696
});
9797

@@ -106,6 +106,6 @@ Then('Response {response} size {apiValidation} {string}', (property: any, valida
106106
*/
107107
Then('I verify response {response} {apiValidation} {string}', async (property: any, validationType: string, expectedValue: string) => {
108108
const validation = getValidation(validationType);
109-
expectedValue = memory.getValue(expectedValue);
109+
expectedValue = await memory.getValue(expectedValue);
110110
validation(property, expectedValue);
111111
});

src/parameterTypes.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { defineParameterType } from '@cucumber/cucumber';
22
import memory from '@qavajs/memory';
3-
import fse from 'fs-extra';
4-
import { getTestDataFilePath } from './utils';
53

64
/**
75
* Used for urls.
@@ -14,14 +12,7 @@ defineParameterType({
1412
regexp: /"([^"\\]*(\\.[^"\\]*)*)"/,
1513
name: 'landingUrl',
1614
useForSnippets: false,
17-
transformer: (string: string) => {
18-
if (string.indexOf('http') === 0) {
19-
return string;
20-
}
21-
if (string.startsWith('$')) {
22-
return memory.getValue(string);
23-
}
24-
},
15+
transformer: (string: string) => memory.getValue(string),
2516
});
2617

2718
/**
@@ -42,13 +33,7 @@ defineParameterType({
4233
regexp: /"([^"\\]*(\\.[^"\\]*)*)"/,
4334
name: 'json',
4435
useForSnippets: false,
45-
transformer: (str: string) => {
46-
if (str.startsWith('$')) {
47-
return memory.getValue(str);
48-
}
49-
const filePath = getTestDataFilePath(str);
50-
return fse.readJSONSync(filePath);
51-
},
36+
transformer: (str: string) => memory.getValue(str),
5237
});
5338

5439
/**
@@ -72,11 +57,7 @@ defineParameterType({
7257
if (!str) {
7358
return {};
7459
}
75-
if (str.includes('$')) {
76-
return memory.getValue(str);
77-
}
78-
const filePath = getTestDataFilePath(str);
79-
return fse.readJSONSync(filePath);
60+
return memory.getValue(str);
8061
},
8162
});
8263

src/utils.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

test-e2e/features/api.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Feature: API
3434
And Response "$response" Status Message to be equal 'OK'
3535

3636
Scenario: Verify simple send with query and headers as file
37-
When I send 'GET' request to "https://jsonplaceholder.typicode.com/posts" with headers "headers.json" with qs "?userId=1" and save response as 'response'
37+
When I send 'GET' request to "https://jsonplaceholder.typicode.com/posts" with headers "$json('testData/headers.json')" with qs "?userId=1" and save response as 'response'
3838
Then Response "$response" Status Code to be equal '200'
3939
And Response "$response" Status Message to be equal 'OK'
4040

@@ -70,7 +70,7 @@ Feature: API
7070
And Response "$response" Status Message to be equal 'Created'
7171

7272
Scenario: Verify POST with valid request body as file
73-
When I send "POST" request to "https://jsonplaceholder.typicode.com/posts" with Body "test_data_file.json" and save response as "response"
73+
When I send "POST" request to "https://jsonplaceholder.typicode.com/posts" with Body "$json('testData/test_data_file.json')" and save response as "response"
7474
Then Response "$response" Status Code to be equal '201'
7575
And Response "$response" Status Message to be equal 'Created'
7676
And Response "$response.payload" contains:
@@ -80,7 +80,7 @@ Feature: API
8080
| body |
8181

8282
Scenario: Verify POST with valid request body as file and headers as file
83-
When I send "POST" request to "https://jsonplaceholder.typicode.com/posts" with headers "headers.json" with Body "test_data_file.json" and save response as "response"
83+
When I send "POST" request to "https://jsonplaceholder.typicode.com/posts" with headers "$json('testData/headers.json')" with Body "$json('testData/test_data_file.json')" and save response as "response"
8484
Then Response "$response" Status Code to be equal '201'
8585
And Response "$response" Status Message to be equal 'Created'
8686
And Response "$response.payload" contains:

test-e2e/memory/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
export default class Memory {}
1+
import fs from 'fs/promises';
2+
3+
export default class Memory {
4+
json = async (path: string): Promise<any> => {
5+
const file = await fs.readFile(path, 'utf-8');
6+
return JSON.parse(file);
7+
};
8+
}

test-e2e/step-definitions/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import memory from '@qavajs/memory';
33
import { expect } from 'chai';
44

55
declare global {
6-
let config: any;
6+
let config: any;
77
}
88

9-
Then('I expect {string} memory value to be equal {string}', async function(actual, expected) {
10-
const actualValue = memory.getValue(actual);
11-
const expectedValue = memory.getValue(expected);
12-
expect(expectedValue).to.eql(actualValue);
9+
Then('I expect {string} memory value to be equal {string}', async function (actual, expected) {
10+
const actualValue = memory.getValue(actual);
11+
const expectedValue = memory.getValue(expected);
12+
expect(expectedValue).to.eql(actualValue);
1313
});

0 commit comments

Comments
 (0)