froth-webdriverio-framework 6.0.68 → 6.0.70

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.
@@ -1,338 +1,245 @@
1
+ /* =========================================================
2
+ Execution / Script API Helpers (Normalized)
3
+ ========================================================= */
1
4
 
2
- //const getLoginToken = require("./loginapi.js")
3
- // Function to get data from the API using the Bearer token
4
-
5
- async function getExecuitonDetails(frothUrl, token, id) {
6
- let jsondata = {};
7
- if (id != 0 || id != null || id != undefined) {
8
- const url = `${frothUrl}/api/test-execution-retrieve/${id}/`;
9
-
10
- try {
11
- console.log("URL: " + url)
12
-
13
- const response = await fetch(url, {
14
- method: 'GET',
15
- headers: {
16
- 'Authorization': `Bearer ${token}`,
17
- 'Content-Type': 'application/json'
18
-
19
- }
20
- });
21
- if (response.ok) {
22
- const data = await response.json();
23
- // console.log(data)
24
- jsondata.automation_suite_id = data.automation_suite_details.id;
25
- jsondata.test_cycle_id = data.test_cycle_details.id;
26
- jsondata.app_url = data.app_details ? data.app_details.app_url : null;
27
- jsondata.browser_stack_local = data.capability_details ? data.capability_details.browser_stack_local : null;
28
- jsondata.mediaurls = data.media_details ? data.media_details.map(media => media.media_url) : null;
29
-
30
- //console.log("media urls :" + jsondata.mediaurls );
31
-
32
-
33
- console.log("in get execution details json data :" + JSON.stringify(jsondata));
34
-
35
- return jsondata;
36
- } else if (response.status === 401) { // Unauthorized, token expired
37
-
38
- console.error("Unauthorized, token expired" + response.status);
39
-
40
- } else {
41
- const errorText = await response.text();
42
- console.error(`Data fetch failed response in getExecuitonDetails: ${response.status}`);
43
- // throw new Error(`HTTP error! status: ${response.status}`);
44
- }
5
+ const DEFAULT_HEADERS = (token) => ({
6
+ 'Authorization': `Bearer ${token}`,
7
+ 'Content-Type': 'application/json'
8
+ });
45
9
 
10
+ /* ===================== HELPERS ===================== */
46
11
 
12
+ function isValidId(id) {
13
+ return id !== null && id !== undefined && id !== 0;
14
+ }
47
15
 
48
- } catch (error) {
49
- console.error('getExecuitonDetails Error fetching data:', error);
16
+ async function handleResponse(response, context) {
17
+ if (response.ok) {
18
+ return response.json();
19
+ }
50
20
 
51
- }
52
- } else {
53
- console.error('getExecuitonDetails Error fetching data: Invalid ID or not executionid is linked to the execution.');
54
- }
21
+ if (response.status === 401) {
22
+ console.error(`🔒 Unauthorized (401) in ${context}`);
23
+ return null;
24
+ }
55
25
 
26
+ const errorText = await response.text();
27
+ console.error(`❌ ${context} failed [${response.status}]: ${errorText}`);
28
+ return null;
56
29
  }
57
30
 
58
- async function getExecuitonScriptDetails(frothUrl, token, execution_id, script_id, script_platform) {
59
- let id;
60
- console.log("script id is :" + script_id + " script platform is :" + script_platform + " execution id is :" + execution_id + " token is :" + token)
61
- const url = `${frothUrl}/api/execution-script-mapping/?execution_id=${execution_id}&automation_script_id=${script_id}&platform=${script_platform}`;
31
+ /* ===================== GET EXECUTION ===================== */
62
32
 
63
- try {
64
- console.log("URL: " + url)
33
+ async function getExecuitonDetails(frothUrl, token, id) {
34
+ if (!isValidId(id)) {
35
+ console.error('❌ Invalid execution ID');
36
+ return null;
37
+ }
38
+
39
+ const url = `${frothUrl}/api/test-execution-retrieve/${id}/`;
40
+ console.log('GET:', url);
41
+
42
+ try {
43
+ const response = await fetch(url, {
44
+ method: 'GET',
45
+ headers: DEFAULT_HEADERS(token)
46
+ });
47
+
48
+ const data = await handleResponse(response, 'getExecuitonDetails');
49
+ if (!data) return null;
50
+
51
+ const jsondata = {
52
+ automation_suite_id: data?.automation_suite_details?.id ?? null,
53
+ test_cycle_id: data?.test_cycle_details?.id ?? null,
54
+ app_url: data?.app_details?.app_url ?? null,
55
+ browser_stack_local: data?.capability_details?.browser_stack_local ?? null,
56
+ mediaurls: data?.media_details?.map(m => m.media_url) ?? []
57
+ };
58
+
59
+ console.log('✅ Execution details loaded');
60
+ return jsondata;
61
+
62
+ } catch (error) {
63
+ console.error('❌ getExecuitonDetails error:', error.message);
64
+ return null;
65
+ }
66
+ }
65
67
 
66
- const response = await fetch(url, {
67
- method: 'GET',
68
- headers: {
69
- 'Authorization': `Bearer ${token}`,
70
- 'Content-Type': 'application/json'
68
+ /* ===================== SCRIPT MAPPING ===================== */
69
+
70
+ async function getExecuitonScriptDetails(
71
+ frothUrl,
72
+ token,
73
+ execution_id,
74
+ script_id,
75
+ script_platform
76
+ ) {
77
+ if (!isValidId(script_id)) {
78
+ console.error('❌ Invalid script ID');
79
+ return null;
80
+ }
81
+
82
+ const url =
83
+ `${frothUrl}/api/execution-script-mapping/` +
84
+ `?execution_id=${execution_id}` +
85
+ `&automation_script_id=${script_id}` +
86
+ `&platform=${script_platform}`;
87
+
88
+ console.log('GET:', url);
89
+
90
+ try {
91
+ const response = await fetch(url, {
92
+ method: 'GET',
93
+ headers: DEFAULT_HEADERS(token)
94
+ });
95
+
96
+ const data = await handleResponse(response, 'getExecuitonScriptDetails');
97
+ if (!data || !data.length) return null;
98
+
99
+ return data[0].id;
100
+
101
+ } catch (error) {
102
+ console.error('❌ getExecuitonScriptDetails error:', error.message);
103
+ return null;
104
+ }
105
+ }
71
106
 
72
- }
73
- });
74
- if (response.ok) {
75
- const data = await response.json();
76
- // console.log(data)
77
- id = data[0].id;
78
- console.log("ID is :" + id)
79
- // console.log("json data :" + JSON.stringify(jsondata));
107
+ /* ===================== UPDATE EXECUTION ===================== */
80
108
 
81
- return id;
82
- } else if (response.status === 401) { // Unauthorized, token expired
83
- console.log("Unauthorized, token expired" + response.status)
109
+ async function updateExecuitonDetails(frothUrl, token, id, resultdetails) {
110
+ if (!isValidId(id)) {
111
+ console.error('❌ Invalid execution ID for update');
112
+ return;
113
+ }
84
114
 
85
- } else {
86
- const errorText = await response.text();
87
- console.error(`Data fetch failed response in getExecuitonScriptDetails: ${response.status}`);
88
- // throw new Error(`HTTP error! status: ${response.status}`);
89
- }
115
+ if (BUFFER.getItem('FROTH_UPDATE_EXECUTION') === 'TRUE') {
116
+ console.log('ℹ️ Execution already updated — skipping');
117
+ return;
118
+ }
90
119
 
120
+ const url = `${frothUrl}/api/test-execution-update/${id}/`;
121
+ const formData = new FormData();
91
122
 
123
+ console.log('PUT:', url);
92
124
 
93
- } catch (error) {
94
- console.error('getExecuitonScriptDetails Error fetching data:', error);
125
+ try {
126
+ formData.append('updated_through_bot', true);
95
127
 
128
+ if (resultdetails.excution_status) {
129
+ formData.append('excution_status', resultdetails.excution_status);
96
130
  }
97
131
 
98
- }
99
-
100
- async function update_CICDRUNID_ReportUrl(frothUrl, token, id) {
132
+ if (
133
+ resultdetails.excution_time &&
134
+ resultdetails.excution_time !== 'NaN:NaN:NaN'
135
+ ) {
136
+ formData.append('excution_time', resultdetails.excution_time);
137
+ }
101
138
 
139
+ if (Array.isArray(resultdetails.comments) && resultdetails.comments.length) {
140
+ formData.append('comments', resultdetails.comments.join('<br>'));
141
+ }
102
142
 
103
- const url = `${frothUrl}/api/test-execution-update/${id}/`;
104
- const formData = new FormData();
105
- try {
106
- console.log("URL" + url)
107
-
108
- // formData.append('id', BUFFER.getItem("FROTH_EXECUTION_ID"))
109
- formData.append('updated_through_bot', true)
110
- formData.append('run_id', process.env.CICD_RUN_ID)
111
-
112
-
113
- if (BUFFER.getItem("REPORT_URL") === null)
114
- console.log("Report URL is null")
115
- else {
116
- console.log("Report URL:" + BUFFER.getItem("REPORT_URL"))
117
- formData.append('report_url', BUFFER.getItem("REPORT_URL"))
118
- }
119
-
120
- const response = await fetch(url, {
121
- method: 'PUT',
122
- headers: {
123
- 'Authorization': `Bearer ${token}`
124
- },
125
- body: formData
126
-
127
- });
128
-
129
- if (response.ok) {
130
- const data = await response.json();
131
- } else if (response.status === 401) { // Unauthorized, token expired
132
- console.log("Unauthorized, token expired" + response.status)
133
- } else {
134
- const errorText = await response.text();
135
- console.error(`error in updating the status into DB ${response.status}`);
136
- }
137
-
138
- } catch (error) {
139
- console.error('Error fetching data:', error);
143
+ const response = await fetch(url, {
144
+ method: 'PUT',
145
+ headers: { 'Authorization': `Bearer ${token}` },
146
+ body: formData
147
+ });
140
148
 
141
- }
149
+ const data = await handleResponse(response, 'updateExecuitonDetails');
150
+ if (!data) return;
142
151
 
152
+ BUFFER.setItem('FROTH_UPDATE_EXECUTION', 'TRUE');
153
+ console.log('✅ Execution details updated');
143
154
 
155
+ } catch (error) {
156
+ console.error('❌ updateExecuitonDetails error:', error.message);
157
+ }
144
158
  }
145
159
 
146
- async function updateExecuitonDetails(frothUrl, token, id, resultdetails) {
147
- try {
148
- console.log("in update execution details UPDATE_EXECUTION: " + BUFFER.getItem("UPDATE_EXECUTION"))
149
- // if (BUFFER.getItem("FROTH_UPDATE_EXECUTION") === 'TRUE') {
150
- // console.log("Execution already updated.")
151
- // }
152
- // else {
153
- const url = `${frothUrl}/api/test-execution-update/${id}/`;
154
- const formData = new FormData();
155
-
156
- console.log("URL" + url)
157
- console.log("Execution details :" + resultdetails.excution_status + " == " + resultdetails.excution_time + " == " + resultdetails.comments)
158
- formData.append('updated_through_bot', true)
159
-
160
- if (resultdetails.excution_status === null) {
161
- console.log("Execution status is null")
162
- } else
163
- formData.append('excution_status', resultdetails.excution_status);
164
-
165
- if (resultdetails.excution_time === null || resultdetails.excution_time === "NaN:NaN:NaN") {
166
- console.log("Execution time is null")
167
- } else
168
- formData.append('excution_time', resultdetails.excution_time);
169
-
170
-
171
- if (resultdetails.comments === null)
172
- console.log("Comments is null")
173
- else if (resultdetails.comments.length === 0)
174
- console.log("Comments is empty")
175
- else
176
- formData.append('comments', resultdetails.comments.join('<br>'))
177
-
178
- // formData.append('comments', JSON.stringify(resultdetails.comments))
179
-
180
- const response = await fetch(url, {
181
- method: 'PUT',
182
- headers: {
183
- 'Authorization': `Bearer ${token}`
184
- },
185
- body: formData
186
-
187
- });
188
-
189
- if (response.ok) {
190
- const data = await response.json();
191
- BUFFER.setItem("FROTH_UPDATE_EXECUTION", 'TRUE')
192
- } else if (response.status === 401) { // Unauthorized, token expired
193
- console.log("Unauthorized, token expired" + response.status)
194
- } else {
195
- const errorText = await response.text();
196
- console.error(`error in updating the status into DB ${response.status}`);
197
- // throw new Error(`HTTP error! status: ${response.status}`);
198
- }
199
-
200
-
201
- // }
202
- } catch (error) {
203
- console.error('Error in updateExecuitonDetails :', error);
204
-
205
- }
160
+ /* ===================== UPDATE CICD / REPORT ===================== */
206
161
 
207
- }
162
+ async function update_CICDRUNID_ReportUrl(frothUrl, token, id) {
163
+ if (!isValidId(id)) return;
208
164
 
165
+ const url = `${frothUrl}/api/test-execution-update/${id}/`;
166
+ const formData = new FormData();
209
167
 
210
- async function updateScriptExecutionStatus(frothUrl, token, scriptid, script_platform, status) {
168
+ console.log('PUT:', url);
211
169
 
212
- if (scriptid != 0) {
213
- try {
214
- console.log("script platform is " + script_platform)
215
- const id = await getExecuitonScriptDetails(frothUrl, token, BUFFER.getItem("FROTH_EXECUTION_ID"), scriptid, script_platform)
216
- console.log("ID is :" + id)
217
- const url = `${frothUrl}/api/script-status-percentage/${id}/`;
218
- const formData = new FormData();
170
+ try {
171
+ formData.append('updated_through_bot', true);
172
+ formData.append('run_id', process.env.CICD_RUN_ID);
219
173
 
220
- console.log("URL" + url)
221
- // formData.append('execution_id', BUFFER.getItem("EXECUTION_ID"))
222
- formData.append('script_status', status)
223
- formData.append('updated_through_bot', true)
174
+ const reportUrl = BUFFER.getItem('REPORT_URL');
175
+ if (reportUrl) {
176
+ formData.append('report_url', reportUrl);
177
+ }
224
178
 
225
- const response = await fetch(url, {
226
- method: 'PUT',
227
- headers: {
228
- 'Authorization': `Bearer ${token}`
229
- },
230
- body: formData
179
+ const response = await fetch(url, {
180
+ method: 'PUT',
181
+ headers: { 'Authorization': `Bearer ${token}` },
182
+ body: formData
183
+ });
231
184
 
232
- });
185
+ await handleResponse(response, 'update_CICDRUNID_ReportUrl');
233
186
 
234
- if (response.ok) {
235
- const data = await response.json();
236
- // console.log("data is :"+data)
187
+ } catch (error) {
188
+ console.error('❌ update_CICDRUNID_ReportUrl error:', error.message);
189
+ }
190
+ }
237
191
 
238
- } else if (response.status === 401) { // Unauthorized, token expired
192
+ /* ===================== UPDATE SCRIPT STATUS ===================== */
193
+
194
+ async function updateScriptExecutionStatus(
195
+ frothUrl,
196
+ token,
197
+ scriptid,
198
+ script_platform,
199
+ status
200
+ ) {
201
+ if (!isValidId(scriptid)) {
202
+ console.error('❌ Invalid script ID');
203
+ return;
204
+ }
205
+
206
+ try {
207
+ const mappingId = await getExecuitonScriptDetails(
208
+ frothUrl,
209
+ token,
210
+ BUFFER.getItem('FROTH_EXECUTION_ID'),
211
+ scriptid,
212
+ script_platform
213
+ );
214
+
215
+ if (!mappingId) return;
216
+
217
+ const url = `${frothUrl}/api/script-status-percentage/${mappingId}/`;
218
+ const formData = new FormData();
239
219
 
240
- console.log("Unauthorized, token expired" + response.status)
220
+ console.log('PUT:', url);
241
221
 
242
- } else {
243
- const errorText = await response.text();
244
- console.error(`error in updating the status into DB ${response.status}`);
245
- }
246
- } catch (error) {
247
- console.error('Error updating data for script status:', error);
222
+ formData.append('script_status', status);
223
+ formData.append('updated_through_bot', true);
248
224
 
249
- }
250
- } else {
251
- console.error('Else: Error updating data for script status: Invalid ID');
252
- }
225
+ const response = await fetch(url, {
226
+ method: 'PUT',
227
+ headers: { 'Authorization': `Bearer ${token}` },
228
+ body: formData
229
+ });
253
230
 
254
- }
255
- async function updateScriptExecutionStatus(frothUrl, token, scriptid, script_platform, status) {
256
-
257
- if (scriptid != 0) {
258
- try {
259
- console.log("script platform is " + script_platform)
260
- const id = await getExecuitonScriptDetails(frothUrl, token, BUFFER.getItem("FROTH_EXECUTION_ID"), scriptid, script_platform)
261
- console.log("ID is :" + id)
262
- const url = `${frothUrl}/api/script-status-percentage/${id}/`;
263
- const formData = new FormData();
264
-
265
- console.log("URL" + url)
266
- // formData.append('execution_id', BUFFER.getItem("EXECUTION_ID"))
267
- formData.append('script_status', status)
268
- formData.append('updated_through_bot', true)
269
-
270
- const response = await fetch(url, {
271
- method: 'PUT',
272
- headers: {
273
- 'Authorization': `Bearer ${token}`
274
- },
275
- body: formData
276
-
277
- });
278
-
279
- if (response.ok) {
280
- const data = await response.json();
281
- // console.log("data is :"+data)
282
-
283
- } else if (response.status === 401) { // Unauthorized, token expired
284
- // Call login function to obtain a new token
285
- // const newToken = await getLoginToken(BUFFER.getItem("SERVICE_USER"), BUFFER.getItem("SERVICE_PASSWORD")); // You need to implement the login function
286
- // // Retry the request with the new token
287
- // return updateScriptExecutionStatus(frothUrl, newToken, scriptid, script_platform, status);
288
- console.log("Unauthorized, token expired" + response.status)
289
-
290
- } else {
291
- const errorText = await response.text();
292
- console.error(`error in updating the status into DB ${response.status}`);
293
- // throw new Error(`HTTP error! status: ${response.status}`);
294
- }
295
-
296
-
297
-
298
- } catch (error) {
299
- console.error('Error updating data for script status:', error);
300
-
301
- }
302
- } else {
303
- console.error('Else: Error updating data for script status: Invalid ID');
304
- }
231
+ await handleResponse(response, 'updateScriptExecutionStatus');
305
232
 
233
+ } catch (error) {
234
+ console.error('❌ updateScriptExecutionStatus error:', error.message);
235
+ }
306
236
  }
307
237
 
308
- //Execute the main function
309
-
310
- //Main function to execute the API call
311
- // async function main() {
312
- // try {
313
- // const frothUrl = "devapi.frothtestops.com";
314
- // const username = "subhra.subudhi@roboticodigital.com";
315
- // const password = "V2VsY29tZUAxMjM=";
316
-
317
- // const token = await getLoginToken(frothUrl, username, password);
318
- // if (!token) {
319
- // throw new Error('Login failed, no token obtained');
320
- // }
321
-
322
- // const id = 208;
323
- // // const data = await getExecuitonDetails(frothUrl, token, id);
324
- // // console.log("Retrieved JSON Data:", data);
325
- // const resultdetails = {}
326
- // const resultdata = "Pass"
327
- // const duration = "31355ms"
328
- // resultdetails.excution_status = resultdata === 'Pass' ? 'PASSED' : 'FAILED'
329
- // //resultdetails.excution_time = convertMillisecondsToTime(duration)
330
- // await getExecuitonScriptDetails(frothUrl, token, 449, 130);
331
- // } catch (error) {
332
- // console.error('Error in main function:', error);
333
- // }
334
- // }
335
-
336
- // main();
337
- module.exports = { getExecuitonDetails, updateExecuitonDetails, updateScriptExecutionStatus, update_CICDRUNID_ReportUrl };
238
+ /* ===================== EXPORTS ===================== */
338
239
 
240
+ module.exports = {
241
+ getExecuitonDetails,
242
+ updateExecuitonDetails,
243
+ updateScriptExecutionStatus,
244
+ update_CICDRUNID_ReportUrl
245
+ };