Skip to content

Commit 21a69b9

Browse files
Merge pull request #12 from akshatlambdatest/MLE-12050
MLE-12050 Added functionality to fetch enahnced cypress reporting.
2 parents 64f418b + 4f5681c commit 21a69b9

File tree

7 files changed

+244
-7
lines changed

7 files changed

+244
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export LT_ACCESS_KEY=def
2525
// The build name whose test session information is to be fetched
2626
export LT_BUILD_NAME=Demo
2727

28+
## Retrieve sessions list
2829
fetch-Lt-sessions
30+
31+
## Support for enhanced cypress reporting
32+
fetch-Lt-sessions --enhancedCyReport
2933
```
3034

3135
## **License**

index.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/usr/bin/env node
22

3+
const yargs = require("yargs/yargs");
4+
const { hideBin } = require("yargs/helpers");
5+
const argv = yargs(hideBin(process.argv)).argv;
6+
const { filterCyTests } = require("./lib/helpers/utils");
7+
const { fetchCyEnhancedReport } = require("./lib/helpers/utils/cypress");
8+
39
const ltClient = require("@lambdatest/node-rest-client");
410

511
const fetchSession = async (options) => {
@@ -31,8 +37,29 @@ const fetchSession = async (options) => {
3137
},
3238
};
3339
}
40+
41+
// fetch enhanced cy tests data
42+
const fetchEnhancedCyReport = argv && argv.enhancedCyReport;
43+
3444
const data = await autoClient.getSessionsOfBuild(options);
35-
console.log(JSON.stringify(data));
45+
46+
if (!fetchEnhancedCyReport) {
47+
console.log(JSON.stringify(data));
48+
return;
49+
}
50+
51+
// fetch cypress enhanced test data
52+
if (fetchEnhancedCyReport) {
53+
const cyTests = filterCyTests(data.data.data);
54+
if (cyTests.length == 0) {
55+
console.error(
56+
`No cypress tests found in the build with name ${process.env.LT_BUILD_NAME}`
57+
);
58+
return;
59+
}
60+
const cyReport = await fetchCyEnhancedReport(cyTests, autoClient);
61+
console.log(JSON.stringify(cyReport));
62+
}
3663
return;
3764
};
3865

lib/const/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
TEST_TYPE_CYPRESS: "cypress",
3+
};

lib/helpers/utils/cypress.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const _ = require("lodash");
2+
const fetchCyEnhancedReport = async (testIDArr = [], ltClient) => {
3+
// structure of response
4+
// const data = {
5+
// success,
6+
// error,
7+
// msg
8+
// data,
9+
// failedTests,
10+
// };
11+
if (!ltClient || !_.isArray(testIDArr)) {
12+
return {
13+
success: false,
14+
error: "Either cypress test ids is not an array or Automation client is not passed",
15+
msg: "",
16+
data: [],
17+
};
18+
}
19+
20+
let cyReportArr = [];
21+
let result = [];
22+
let failedTests = [];
23+
const chunkSize = 20;
24+
// fetch cypress test enhanced report in chunks of 20
25+
for (let i = 0; i < testIDArr.length; i += chunkSize) {
26+
cyReportArr = [];
27+
const testChunk = testIDArr.slice(i, i + chunkSize);
28+
cyReportArr = _.map(testChunk, (elem) => {
29+
return ltClient.fetchCyEnhancedReport(elem);
30+
});
31+
try {
32+
const cyReport = await Promise.all(cyReportArr.map((p) => p.catch((e) => e)));
33+
_.forEach(cyReport, (report, index) => {
34+
if (!(report instanceof Error)) {
35+
result.push({
36+
[testChunk[index]]: report,
37+
});
38+
} else {
39+
// failed tests whose report can't be fetched
40+
failedTests.push({
41+
[testChunk[index]]: "unable to fetch detailed cypress report",
42+
});
43+
}
44+
});
45+
} catch (e) {
46+
console.log("unable to fetch cypress enhanced report, exception", e);
47+
return {
48+
success: false,
49+
error: "Unable to fetch cypress enhanced report",
50+
msg: "",
51+
data: [],
52+
failedTests: [],
53+
};
54+
}
55+
}
56+
return {
57+
success: true,
58+
error: "",
59+
msg: "Retrieve enhanced cypress report was successfull",
60+
data: result,
61+
failedTests,
62+
};
63+
};
64+
65+
module.exports = {
66+
fetchCyEnhancedReport,
67+
};

lib/helpers/utils/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const _ = require("lodash");
2+
const { TEST_TYPE_CYPRESS } = require("../../const");
3+
4+
const filterCyTests = (tests = []) => {
5+
if (!_.isArray(tests)) {
6+
return [];
7+
}
8+
const cyTests = _.filter(tests, (test) => {
9+
if (test && test.test_type && test.test_type.toLowerCase() == TEST_TYPE_CYPRESS) {
10+
return true;
11+
}
12+
return false;
13+
});
14+
return _.map(cyTests, "test_id");
15+
};
16+
17+
module.exports = {
18+
filterCyTests,
19+
};

package-lock.json

Lines changed: 119 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdatest/node-fetch-sessions",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "This CLI fetches lambdatest automation test session details based on build name",
55
"main": "index.js",
66
"bin": {
@@ -27,6 +27,8 @@
2727
"cli"
2828
],
2929
"dependencies": {
30-
"@lambdatest/node-rest-client": "^1.0.4"
30+
"@lambdatest/node-rest-client": "^1.0.5",
31+
"lodash": "^4.17.21",
32+
"yargs": "^17.5.1"
3133
}
3234
}

0 commit comments

Comments
 (0)