artes 1.0.46 → 1.0.48

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/index.js CHANGED
@@ -6,9 +6,11 @@ const {
6
6
  context,
7
7
  element,
8
8
  selector,
9
+ saveVar,
10
+ extractVarsFromResponse,
9
11
  page,
10
12
  request,
11
- random
13
+ random,
12
14
  } = require("./src/helper/imports/commons");
13
15
  const {
14
16
  keyboard,
@@ -25,6 +27,8 @@ module.exports = {
25
27
  Then,
26
28
  element,
27
29
  selector,
30
+ saveVar,
31
+ extractVarsFromResponse,
28
32
  context,
29
33
  random,
30
34
  page,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "artes",
3
- "version": "1.0.46",
3
+ "version": "1.0.48",
4
4
  "description": "The package provide step definitions and user writes feature files, and the package handles automation, with optional POM files and custom step definitions.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -3,10 +3,11 @@ const { Given, When, Then } = require("@cucumber/cucumber");
3
3
  const {
4
4
  getElement,
5
5
  getSelector,
6
+ extractVarsFromResponse,
6
7
  saveVar,
7
8
  resolveVariable,
8
9
  } = require("../pomController/elementController");
9
- const { faker } = require('@faker-js/faker');
10
+ const { faker } = require("@faker-js/faker");
10
11
  const { context } = require("../../hooks/context");
11
12
 
12
13
  const element = getElement;
@@ -39,6 +40,7 @@ module.exports = {
39
40
  Then,
40
41
  element,
41
42
  selector,
43
+ extractVarsFromResponse,
42
44
  saveVar,
43
45
  resolveVariable,
44
46
  random,
@@ -85,6 +85,33 @@ class Elements {
85
85
  return selector;
86
86
  }
87
87
 
88
+ static extractVarsFromResponse(vars, customVarName) {
89
+ const responseBody = context.response.responseBody;
90
+
91
+ function getValueByPath(obj, path) {
92
+ const keys = path.split(".");
93
+ let current = obj;
94
+
95
+ for (const key of keys) {
96
+ if (current && typeof current === "object" && key in current) {
97
+ current = current[key];
98
+ } else {
99
+ return undefined;
100
+ }
101
+ }
102
+
103
+ return current;
104
+ }
105
+
106
+ vars.split(",").forEach((v) => {
107
+ const path = v.trim();
108
+ const value = getValueByPath(responseBody, path);
109
+ if (value !== undefined) {
110
+ this.saveVar(value, customVarName, path);
111
+ }
112
+ });
113
+ }
114
+
88
115
  static saveVar(value, customName, path) {
89
116
  if (!customName) {
90
117
  const flatKey = path
@@ -113,6 +140,7 @@ module.exports = {
113
140
  getElement: Elements.getElement.bind(Elements),
114
141
  addElements: Elements.addElements.bind(Elements),
115
142
  getSelector: Elements.getSelector.bind(Elements),
143
+ extractVarsFromResponse: Elements.extractVarsFromResponse.bind(Elements),
116
144
  saveVar: Elements.saveVar.bind(Elements),
117
145
  resolveVariable: Elements.resolveVariable.bind(Elements),
118
146
  };
@@ -141,7 +141,7 @@ const api = {
141
141
  context.response = response;
142
142
  },
143
143
  vars: () => {
144
- return context.vars
144
+ return context.vars;
145
145
  },
146
146
  };
147
147
 
@@ -3,38 +3,12 @@ const {
3
3
  context,
4
4
  expect,
5
5
  selector,
6
- saveVar
6
+ extractVarsFromResponse,
7
+ saveVar,
7
8
  } = require("../helper/imports/commons");
8
9
  const { api } = require("../helper/stepFunctions/exporter");
9
10
  const Ajv = require("ajv");
10
11
 
11
- function extractVarsFromResponse(vars, customVarName) {
12
- const responseBody = context.response.responseBody;
13
-
14
- function getValueByPath(obj, path) {
15
- const keys = path.split(".");
16
- let current = obj;
17
-
18
- for (const key of keys) {
19
- if (current && typeof current === "object" && key in current) {
20
- current = current[key];
21
- } else {
22
- return undefined;
23
- }
24
- }
25
-
26
- return current;
27
- }
28
-
29
- vars.split(",").forEach((v) => {
30
- const path = v.trim();
31
- const value = getValueByPath(responseBody, path);
32
- if (value !== undefined) {
33
- saveVar(value, customVarName, path);
34
- }
35
- });
36
- }
37
-
38
12
  When("User sends GET request to {string}", async function (url) {
39
13
  await api.get(url);
40
14
  });
@@ -154,7 +128,6 @@ When(
154
128
 
155
129
  When("User wants to see saved variables", async function () {
156
130
  console.log("\nVariables:", api.vars(), "\n");
157
-
158
131
  });
159
132
 
160
133
  When("User wants to see request body", async function () {
@@ -3,6 +3,7 @@ const {
3
3
  selector,
4
4
  expect,
5
5
  element,
6
+ extractVarsFromResponse,
6
7
  } = require("../helper/imports/commons");
7
8
  const { assert, frame } = require("../helper/stepFunctions/exporter");
8
9
 
@@ -813,3 +814,12 @@ Then("User expects should have {int} {string}", async (count, elements) => {
813
814
  const elementCount = await frame.count(elements);
814
815
  expect(elementCount).toEqual(count);
815
816
  });
817
+
818
+ Then(
819
+ "User expects that response has {string} field with {string} value",
820
+ async (field, value) => {
821
+ extractVarsFromResponse(field, field);
822
+
823
+ expect(context.vars[field]).toBe(value);
824
+ },
825
+ );
@@ -0,0 +1,25 @@
1
+ const { Given, context, random } = require("../helper/imports/commons");
2
+
3
+ Given("User sets random words as {string} variable", async (key) => {
4
+ const words = random.lorem.words({ min: 2, max: 5 });
5
+ context.vars[key] = words;
6
+ });
7
+
8
+ Given(
9
+ "User sets random number from {int} to {int} as {string} variable",
10
+ async (from, to, key) => {
11
+ const number = random.number.int({ min: from, max: to });
12
+ context.vars[key] = number;
13
+ },
14
+ );
15
+
16
+ Given(
17
+ "User sends GET request to {string} and save {string} variable as a {string} randomly",
18
+ async (api, varName, variableKey) => {
19
+ const res = await fetch(api);
20
+ const body = await res.json();
21
+ const randomContent =
22
+ body.content[random.number.int({ min: 0, max: body.content.length - 1 })];
23
+ context.vars[variableKey] = randomContent[varName];
24
+ },
25
+ );