forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomation-script.js
executable file
·102 lines (91 loc) · 3.16 KB
/
automation-script.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
'use strict';
let request = require('request'),
path = require('path'),
fs = require('fs'),
exec = require('child_process').exec,
nodemailer = require('nodemailer'),
config = require('./config.js'),
reportFileName = "report.html";
//configure the smtp
let transporter = nodemailer.createTransport(config.smtp);
let automation = function() {
let self = this;
console.log("-----automation started ------")
self.init();
// self.run();
}
automation.prototype.init = function() {
try {
let self = this;
// initalise the runscope config
let options = {
method: "POST",
url: config.runscope.url,
qs: {
"url": config.url,
"username": config.runscope.username,
"password": config.runscope.password,
"api_key": config.stack.api_key,
"delivery_token": config.stack.delivery_token,
"Content-Type": "application/json"
}
};
console.log("Creating data in Contentstack...");
// trigger runscope url for data creation in Contentstack
request(options, function(err, res, body) {
if (!err && body) {
setTimeout(function() {
console.log("Data created in Contentstack...");
self.run();
}, 240000);
}
});
} catch (err) {
console.error("Init error: ", err.message || err);
process.exit(0);
}
}
automation.prototype.run = function() {
let self = this;
let _path = path.join(process.cwd(), 'test');
//change directory to run "node index.js | tap-json > report.json" command
// process.chdir(_path);
// run command "node index.js" to run the test cases
console.log("Running the test cases....");
// let executeCommand = "node index.js";
let executeCommand = "node index.js | tap-html > " + reportFileName;
exec(executeCommand, function(err, stdout, stderr) {
if (!err) {
console.log("Test cases runned successfully....");
self.sendMail();
} else {
console.error("error: ", err.message || err)
}
});
};
automation.prototype.sendMail = function() {
console.log("Mailing report....");
let reportPath = path.join(__dirname, '../', 'test', reportFileName);
if (fs.existsSync(reportPath)) {
let message = {
from: 'uttam.ukkoji@contentstack.com',
to: 'uttam.ukkoji@contentstack.com',
subject: 'Report of JS SDK test cases | ' + new Date(),
html: '<p>Hi Team, Please check below attachment of test cases report.</p>',
attachments: [{
filename: "reports.html",
path: reportPath
}]
};
transporter.sendMail(message, function(err, info) {
if (!err) {
console.log("Report mailed successfully !!!");
console.log("-----automation finished ------")
}
});
transporter.close();
} else {
console.error("Error: Sorry report don't exist");
}
};
module.exports = automation;