artes 1.0.46 → 1.0.47

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,6 +6,7 @@ const {
6
6
  context,
7
7
  element,
8
8
  selector,
9
+ saveVar,extractVarsFromResponse,
9
10
  page,
10
11
  request,
11
12
  random
@@ -25,6 +26,7 @@ module.exports = {
25
26
  Then,
26
27
  element,
27
28
  selector,
29
+ saveVar,extractVarsFromResponse,
28
30
  context,
29
31
  random,
30
32
  page,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "artes",
3
- "version": "1.0.46",
3
+ "version": "1.0.47",
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,6 +3,7 @@ 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");
@@ -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
+ 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
  };
@@ -3,37 +3,13 @@ const {
3
3
  context,
4
4
  expect,
5
5
  selector,
6
+ extractVarsFromResponse,
6
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
12
 
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
13
 
38
14
  When("User sends GET request to {string}", async function (url) {
39
15
  await api.get(url);
@@ -0,0 +1,24 @@
1
+ const { Given, context, random, expect, extractVarsFromResponse } = 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("User sets random number from {int} to {int} as {string} variable", async (from, to, key) => {
9
+ const number = random.number.int({min: from, max: to});
10
+ context.vars[key] = number;
11
+ });
12
+
13
+ Given('User sends GET request to {string} and save {string} variable as a {string} randomly', async (api, varName, variableKey) => {
14
+ const res = await fetch(api)
15
+ const body = await res.json();
16
+ const randomContent = body.content[random.number.int({ min: 0, max: body.content.length - 1 })];
17
+ context.vars[variableKey] = randomContent[varName];
18
+ })
19
+
20
+ Given('User expects that response has {string} field with {string} value', async (field, value) => {
21
+ extractVarsFromResponse(field, field);
22
+
23
+ expect(context.vars[field]).toBe(value);
24
+ })