This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
base-test-class.js
186 lines (165 loc) · 5.44 KB
/
base-test-class.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import _ from "lodash";
import settings from "./settings";
import Worker from "./worker/magellan";
import logger from "./util/logger";
const fs = require("fs");
const path = require("path");
function getMostRecentFileName(dir, callback) {
fs.readdir(dir, (err, files) => {
if (err) {
return callback(err);
}
const ret = _.max(files, f => {
var fullpath = path.join(dir, f);
return fs.statSync(fullpath).ctime;
});
return callback(null, ret ? `${dir}/${ret}` : null);
});
}
const BaseTest = function(steps, customizedSettings = null) {
/**
* NOTICE: we don't encourage to pass [before, beforeEach, afterEach, after]
* together with steps into the constructor. PLEASE extend the base test
* and define these four methods there if they are necessary
*/
const self = this;
const enumerables = ["before", "after", "beforeEach", "afterEach"];
this.isWorker = settings.isWorker;
this.env = settings.env;
if (customizedSettings) {
this.isWorker = customizedSettings.isWorker;
this.env = customizedSettings.env;
}
// copy steps to self
_.forEach(steps, (v, k) => {
Object.defineProperty(self, k, { enumerable: true, value: v });
});
// copy before, beforeEach, afterEach, after to prototype
_.forEach(enumerables, k => {
const srcFn = self[k] || BaseTest.prototype[k];
if (srcFn) {
Object.defineProperty(self, k, { enumerable: true, value: srcFn });
}
});
};
BaseTest.prototype = {
/*eslint-disable callback-return*/
before(client, callback) {
this.failures = [];
this.passed = 0;
// we only want timeoutsAsyncScript to be set once the whole session to limit
// the number of http requests we sent
this.isAsyncTimeoutSet = false;
this.isSupposedToFailInBefore = false;
if (this.isWorker) {
this.worker = new Worker({ nightwatch: client });
process.addListener("message", this.worker.handleMessage);
}
client.perform(() => {
callback();
});
},
beforeEach(client) {
if (!this.isAsyncTimeoutSet) {
client.timeoutsAsyncScript(settings.JS_MAX_TIMEOUT);
this.isAsyncTimeoutSet = true;
}
// Note: Sometimes, the session hasn't been established yet but we have control.
var sessionId =
client.sessionId || client.capabilities["webdriver.remote.sessionid"];
if (sessionId) {
settings.sessionId = sessionId;
if (this.isWorker) {
this.worker.emitMetadata({
sessionId: settings.sessionId,
capabilities: client.capabilities
});
}
}
},
afterEach(client, callback) {
if (this.results) {
// in case we failed in `before`
// keep track of failed tests for reporting purposes
if (this.results.failed || this.results.errors) {
// Note: this.client.currentTest.name is also available to display
// the name of the specific step within the test where we've failed.
this.failures.push(client.currentTest.module);
}
if (this.results.passed) {
this.passed += this.results.passed;
}
}
if (!this.isAsyncTimeoutSet) {
client.timeoutsAsyncScript(settings.JS_MAX_TIMEOUT);
this.isAsyncTimeoutSet = true;
}
// Note: Sometimes, the session hasn't been established yet but we have control.
var sessionId =
client.sessionId || client.capabilities["webdriver.remote.sessionid"];
if (sessionId) {
settings.sessionId = sessionId;
if (this.isWorker) {
const testCases = client.currentTest && client.currentTest.results && client.currentTest.results.testcases
? _.mapValues(client.currentTest.results.testcases, o => {
return {
errors: o.errors,
failed: o.failed
};
})
: null;
if (
client.screenshotsPath &&
(client.currentTest.results.failed ||
client.currentTest.results.errors)
) {
const prefix = client.currentTest.module
.replace(/\s/g, "-")
.replace(/"|'/g, "");
const screenShotDir = `${client.screenshotsPath}/${prefix}`
getMostRecentFileName(
screenShotDir,
(err, screenShotPath) => {
if (err) {
logger.warn(`Failed to get screenshot. Error: ${err}`);
}
this.worker.emitMetadata({
sessionId: settings.sessionId,
capabilities: client.capabilities,
screenShotPath,
testCases
});
}
);
}else{
this.worker.emitMetadata({
sessionId: settings.sessionId,
capabilities: client.capabilities,
testCases
});
}
}
}
callback();
},
/*eslint-disable callback-return*/
after(client, callback) {
const self = this;
if (this.isWorker) {
process.removeListener("message", self.worker.handleMessage);
}
if (self.isSupposedToFailInBefore) {
// there is a bug in nightwatch that if test fails in `before`, test
// would still be reported as passed with a exit code = 0. We'll have
// to let magellan know the test fails in this way
/* istanbul ignore next */
/*eslint no-process-exit:0 */
/*eslint no-magic-numbers:0 */
process.exit(100);
}
// executor should eat it's own error in summerize()
client.end();
callback();
}
};
module.exports = BaseTest;