-
Notifications
You must be signed in to change notification settings - Fork 2
/
load.js
103 lines (84 loc) · 2.29 KB
/
load.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
const steps = require('cypress-cucumber-preprocessor/steps');
const resolver = require('cypress-cucumber-preprocessor/lib/resolveStepDefinition');
const waitUntil = require('async-wait-until');
const flatten = require('./lib/flatten');
const dynamicReloading = false;
/**
*
* @param args array
* @param text string
* @returns string
*/
const replacePlaceholders = (args, text) => {
let i;
for (i = 1; i <= args.length; i++){
text = text.replace(`{arg${i}}`, args[i-1]);
}
return text;
};
/**
*
* @param variables object
* @param text string
* @returns string
*/
const replaceVariables = (variables, text) => {
for (const variable in variables) {
if(variables.hasOwnProperty(variable)){
text = text.replace('$'+variable, variables[variable]);
}
}
return text;
};
/**
*
* @param stepdata object
* @param variables object
*/
module.exports = (stepdata, variables = {}) => {
console.log("Loading picklejs Steps...");
const flattened = flatten(variables);
const values = Object.values(stepdata);
for (const value of values ){
value.steps.forEach((config) => {
steps.defineStep(config.name, function(...args){
config.steps.forEach((step) => {
let text = replacePlaceholders(args, step.text);
text = replaceVariables(flattened, text);
const runnable = {
argument: "undefined",
keyword: "Then",
type: "Step",
text: text
};
resolver.resolveAndRunStepDefinition(runnable);
});
});
});
}
if(dynamicReloading){
const ws = new WebSocket('ws://localhost:8765');
waitUntil(() => ws.readyState === WebSocket.OPEN, 2000, 50);
ws.onmessage = ev => {
console.log('message from OS');
if (ev.type === 'message' && ev.data) {
try {
const data = JSON.parse(ev.data);
if (data.command === 'reload' && data.filename) {
console.log(
'reloading Cypress because "%s" has changed',
data.filename
);
window.top.document.querySelector('.reporter .restart').click()
}
} catch (e) {
console.error('Could not parse message from plugin');
console.error(e.message);
console.error('original text');
console.error(ev.data);
}
}
};
}
console.log("Done.");
};