-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (127 loc) · 3.91 KB
/
index.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
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
const https = require('https');
const axios = require('axios');
const path = require('path');
const marked = require('marked');
const { markedTerminal } = require('marked-terminal');
const dotenv = require('dotenv');
const stripAnsi = require('strip-ansi');
const stackTraceParser = require('./utils/stackTrace.js');
let SERVICE_URL = process.env.NIGHTWATCH_ANALYSIS_SERVICE_URL;
if (!SERVICE_URL) {
dotenv.config({
path: path.resolve(__dirname, '.env')
});
SERVICE_URL = process.env.SERVICE_URL;
}
marked.use(markedTerminal());
/**
* Send an API call to the service with the error message, stack trace,
* code snippet, and a screenshot file.
* @param {String} errorMessage - The error message including stack trace.
* @param {String} codeSnippet - A code snippet from the test case.
* @param {Object} additionalDetails
*/
function sendErrorAnalysisRequest({errorMessage, codeSnippet, additionalDetails}) {
const payload = {
errorMessage,
codeSnippet,
additionalDetails
};
const headers = { 'Content-Type': 'application/json' };
console.log('Running error analysis...');
return axios.post(SERVICE_URL, payload, {
headers,
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
}
function getErrorMessages(results) {
if (!results.lastError) {
return null;
}
return Object.keys(results.modules).reduce((prev, key) => {
const moduleData = results.modules[key];
if (moduleData.lastError) {
const testcases = getTestFailures(moduleData);
prev[key] = testcases;
}
return prev;
}, {})
}
function getTestFailures(moduleData) {
return Object.keys(moduleData.completed).reduce((prev, key) => {
const assertions = moduleData.completed[key].assertions;
const errors = moduleData.completed[key].errorsPerTest;
const testcase = {
failures: [],
errors,
failed: errors !== undefined && errors > 0
};
for (const assertion of assertions) {
if (assertion.failure) {
testcase.failures.push(assertion);
if (!testcase.failed) {
testcase.failed = true;
}
}
}
if (testcase.failed) {
prev[key] = testcase;
}
return prev;
}, {});
}
module.exports = {
reporterName: 'nightwatch-openai-plugin',
settings: {
timeoutMs: 60000
},
async reporter(results) {
const errors = getErrorMessages(results);
if (!errors) {
return;
}
const outputs = [];
Object.keys(errors).forEach(moduleName => {
const testcases = errors[moduleName];
Object.keys(testcases).forEach(testcaseName => {
const failures = testcases[testcaseName].failures ?? [];
const errors = testcases[testcaseName].errors ?? [];
let codeSnippet = '';
let errorMessage = 'Testcase name: ' + testcaseName + '"""';
let screenshotPath = '';
if (failures[0]) {
const failure = failures[0];
codeSnippet = stackTraceParser(failure.stackTrace);
errorMessage += `Assertion failed message: ${stripAnsi(failure.message)}`;
screenshotPath = failure.screenshots && failure.screenshots[0];
}
if (errors) {
errorMessage += '""" Additional errors: ' + errors.join('"""') + '"""';
}
outputs.push({
codeSnippet,
errorMessage,
screenshotPath,
additionalDetails: {
nightwatchVersion: '3.2.1',
configFile: '',
platform: '',
browser: '',
headless: ''
}
});
});
});
for (const output of outputs) {
try {
const response = await sendErrorAnalysisRequest(output);
const terminalOutput = marked.parse(response.data.analyzedResult);
console.log('Error analysis completed:', terminalOutput);
} catch (err) {
console.error('Error analysis failed:', err.response?.data || err.message);
}
}
}
}