Skip to content

Commit c1cc380

Browse files
serhatbolsuSerhat Bolsu
andauthored
Post report to teams tool, update to singleton design (#2)
* Added microsoft teams report publisher * Allure reporting handled when allure report is not used * Resource object support intellisense * README update Co-authored-by: Serhat Bolsu <s944588@emirates.com>
1 parent 938c365 commit c1cc380

File tree

7 files changed

+115
-16
lines changed

7 files changed

+115
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.idea/*
33
.env
44
/allure-*/*
5+
testResults*.json

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Uses resource object, this is a similar method used in selenium with **page Obje
7171
- in `vegetable.resource.js` you can write various methods with regards to this specific endpoint.
7272
- re-usability increased with common vegetable endpoint functions.
7373
- usage of superagent/requests encapsulated, however still usable look at tc#3
74-
- Pre-initialized resource-objects, it serve as a facade to service under test.
74+
- Pre-initialized resource-objects are singleton objects. So single object is shared inside test case.
7575

7676

7777
## Familiarizing Yourself with the System Under Test
@@ -118,6 +118,12 @@ Run individual test suite
118118

119119
```npm test -- test/basic.spec.js```
120120

121+
Post report to Microsoft Teams channel
122+
```
123+
export HOOK_URL=<microsoft_web_hook_url>
124+
npm run report.teams
125+
126+
```
121127

122128
## Contributors
123129
Special thanks to [gsypolt](https://github.com/gsypolt/api-testing-javascript) as he is the owner of vegetable api.
@@ -128,3 +134,4 @@ Please create a pull request.
128134
## ToDO
129135

130136
- Jenkinsfile
137+
- Slack Reporter (looking for help, send a PR if you are interested)

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "node server/server.js",
8-
"test": "jest",
9-
"lint": "eslint ."
8+
"test": "jest --json --outputFile='./testResults.json'",
9+
"lint": "eslint .",
10+
"report.teams" : "TEST_FRAMEWORK=jest PROJECT_NAME='Api Sample Test Boilerplate' PROJECT_ENVIRONMENT=SIT PROJECT_TEST_TYPE=API node utils/teamsReporter.js ../testResults.json"
1011
},
1112
"husky": {
1213
"hooks": {

resources/BaseApi.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ class BaseApi {
1919

2020
setAllureAttachment(res) {
2121
// TODO: Handle other types that allure report can show properly.
22-
if (Object.keys(res.body).length !== 0) {
23-
const contentType = res.headers['content-type']
24-
.includes('application/json') ? 'application/json' : 'txt';
25-
const obj = contentType === 'application/json' ? JSON.stringify(res.body) : res.body;
26-
reporter.addAttachment(res.req.path, obj, contentType);
22+
try {
23+
if (Object.keys(res.body).length !== 0) {
24+
const contentType = res.headers['content-type']
25+
.includes('application/json') ? 'application/json' : 'txt';
26+
const obj = contentType === 'application/json' ? JSON.stringify(res.body) : res.body;
27+
reporter.addAttachment(res.req.path, obj, contentType);
28+
}
29+
} catch (error) {
30+
// ignore if allure is not set
2731
}
2832
}
2933

resources/api.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
const VegetableResource = require('./vegetable.resource');
22

33
class Api {
4-
constructor() {
5-
this.resources = {};
6-
}
7-
84
vegetable() {
9-
return this.resources.vegetable = this.resources.vegetable || new VegetableResource();
5+
return new VegetableResource();
106
}
117
}
128

resources/vegetable.resource.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const BaseApi = require('./BaseApi');
2+
let instance = null;
23

34
function Vegetable(jsonBody) {
45
this.id = jsonBody.id;
@@ -10,9 +11,14 @@ function Vegetable(jsonBody) {
1011

1112
class VegetableResource extends BaseApi {
1213
constructor() {
13-
super();
14-
this.path = '/vegetables';
15-
this.vegetables = [];
14+
if (!instance) {
15+
super();
16+
instance = this;
17+
this.path = '/vegetables';
18+
this.vegetables = [];
19+
}
20+
21+
return instance;
1622
}
1723

1824
async create(name, price, releaseDate, origin) {

utils/teamsReporter.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const request = require('superagent');
2+
3+
function main() {
4+
if (process.env.TEST_FRAMEWORK === undefined) {
5+
throw new Error('TEST_FRAMEWORK should be defined either as "jest" or "mocha"');
6+
} else if (process.env.HOOK_URL === undefined) {
7+
throw new Error('HOOK_URL should be defined as Microsoft teams channel hook url');
8+
}
9+
const file = process.argv[2];
10+
if (!file) throw new Error('File path is not given');
11+
12+
const platform = process.env.TEST_FRAMEWORK;
13+
const hookUrl = process.env.HOOK_URL;
14+
const project = process.env.PROJECT_NAME;
15+
const environment = process.env.PROJECT_ENVIRONMENT;
16+
const type = process.env.PROJECT_TEST_TYPE;
17+
const buildUrl = process.env.BUILD_URL;
18+
19+
let total = 0;
20+
let passed = 0;
21+
let failed = 0;
22+
let skipped = 0;
23+
24+
// logic to get results
25+
if (platform === 'jest') {
26+
const testData = require(file);
27+
total = testData['numTotalTests'];
28+
passed= testData['numPassedTests'];
29+
failed = testData['numFailedTests'];
30+
skipped = total - passed - failed;
31+
} else if (platform === 'mocha') {
32+
// TODO: Implement this
33+
}
34+
35+
let results = `**${total} Tests**: &#x2714;${passed} passed`;
36+
if (failed > 0) results += `, &#x2757;${failed} failed`;
37+
if (skipped > 0) results += `, &#x2796;${skipped} skipped`;
38+
39+
request.post(hookUrl)
40+
.send(
41+
{
42+
"@type": "MessageCard",
43+
"@context": "http://schema.org/extensions",
44+
"themeColor": "33F0FF",
45+
"summary": `Test report for ${project}`,
46+
"sections": [
47+
{
48+
49+
"activityTitle": `## ${project}, ${environment}, ${type}`,
50+
// "activitySubtitle": "Status of build #90",
51+
"facts": [
52+
{
53+
"name": "Environment",
54+
"value": environment,
55+
},
56+
// {
57+
// "name": "Type",
58+
// "value": "UI",
59+
// },
60+
{
61+
"name": "Status",
62+
"value": results,
63+
},
64+
],
65+
"markdown": true,
66+
},
67+
],
68+
"potentialAction": [
69+
{
70+
"@type": "OpenUri",
71+
"name": "View Report",
72+
"targets": [
73+
{ "os": "default", "uri": buildUrl },
74+
],
75+
},
76+
],
77+
},
78+
)
79+
.then( (res) => {
80+
console.log(`Post results to Microsoft Teams with status: ${res.status}`);
81+
});
82+
}
83+
84+
main();

0 commit comments

Comments
 (0)