-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJakefile
192 lines (167 loc) · 5.16 KB
/
Jakefile
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
let { task, desc } = require('jake');
require('dotenv').config();
desc('This is the default task.');
task('default', function () {
console.log('parent task');
});
desc('This is some other task. It depends on the default task');
task('slack', ['default'], function (environment) {
const https = require('https');
const yourWebHookURL = 'https://hooks.slack.com/services/'+process.env.SLACK_TOKEN;
const userAccountNotification = {
'username': 'Protractor | Test Report',
'text': '@channel Your tests are cooked!',
'icon_emoji': ':popcorn:',
'attachments': [{
'color': statusColor(),
'fields': [
{
'title': 'Environment',
'value': environment,
'short': true // long fields will be full width
},
{
'title': 'Total Scenarios',
'value': scenarioCount(),
'short': true
},
{
'title': 'Status',
'value': status() + ' ' + ':champagne: ' + passedCount() + ' :comet: ' + failedCount(),
'short': true
},
{
'title': 'Time Taken',
'value': '3 min 30 secs',
'short': true
}
],
"actions": [
{
"type": "button",
"text": "See test results",
"url": "http://example.com" // url the button will take the user if clicked
},
],
}]
};
/**
* Handles the actual sending request.
* We're turning the https.request into a promise here for convenience
* @param webhookURL
* @param messageBody
* @return {Promise}
*/
function sendSlackMessage (webhookURL, messageBody) {
// make sure the incoming message body can be parsed into valid JSON
try {
messageBody = JSON.stringify(messageBody);
} catch (e) {
throw new Error('Failed to stringify messageBody', e);
}
// Promisify the https.request
return new Promise((resolve, reject) => {
// general request options, we defined that it's a POST request and content is JSON
const requestOptions = {
method: 'POST',
header: {
'Content-Type': 'application/json'
}
};
// actual request
const req = https.request(webhookURL, requestOptions, (res) => {
let response = '';
res.on('data', (d) => {
response += d;
});
// response finished, resolve the promise with data
res.on('end', () => {
resolve(response);
})
});
// there was an error, reject the promise
req.on('error', (e) => {
reject(e);
});
// send our message body (was parsed to JSON beforehand)
req.write(messageBody);
req.end();
});
}
// main
(async function () {
if (!yourWebHookURL) {
console.error('Please fill in your Webhook URL');
}
try {
const slackResponse = await sendSlackMessage(yourWebHookURL, userAccountNotification);
console.log('Message response', slackResponse);
} catch (e) {
console.error('There was a error with the request', e);
}
})();
function scenarioCount() {
var json = require(process.cwd() + "/reports/html/cucumber_reporter.html.json");
var [i, j, scenario] = [0,0,0,0];
while(i != Object.keys(json).length) {
while(j != Object.keys(json[i]['elements']).length) {
scenario += 1, j += 1;
}
j = 0, i += 1;
};
return scenario;
};
function failedCount() {
var json = require(process.cwd() + "/reports/html/cucumber_reporter.html.json");
var [i, j, k, scenario, passed, failed] = [0, 0, 0, 0, 0, 0];
while(i != Object.keys(json).length) {
while(j != Object.keys(json[i]['elements']).length) {
while(k != Object.keys(json[i]['elements'][j]['steps']).length) {
if(Object.values(json[i]['elements'][j]['steps'][k]['result']['status']).join().replace(/,/g,'') == 'failed') {
failed += 1;
break;
}
k += 1;
}
scenario += 1, j += 1;
k = 0;
}
j = 0, i += 1;
};
return failed;
};
function passedCount() {
var json = require(process.cwd() + "/reports/html/cucumber_reporter.html.json");
var [i, j, k, scenario, passed] = [0, 0, 0, 0, 0];
while(i != Object.keys(json).length) {
while(j != Object.keys(json[i]['elements']).length) {
while(k != Object.keys(json[i]['elements'][j]['steps']).length) {
if(Object.values(json[i]['elements'][j]['steps'][k]['result']['status']).join().replace(/,/g,'') == 'failed') {
break;
} else if((Object.values(json[i]['elements'][j]['steps'][k]['result']['status']).join().replace(/,/g,'') == 'passed') && (k+1 == Object.keys(json[i]['elements'][j]['steps']).length)) {
passed += 1;
}
k += 1;
}
scenario += 1, j += 1;
k = 0;
}
j = 0, i += 1;
};
return passed;
};
function status() {
if (failedCount() > 0) {
return 'FAILED';
} else {
return 'PASSED';
}
};
function statusColor() {
if (failedCount() > 0) {
return '#bd2d0d';
} else {
return '#0aa30a';
}
};
});