Skip to content

Commit d7a54aa

Browse files
committed
Fixed a problem with Selenium server log not being written properly in case of test failures/errors
1 parent b1e7d1a commit d7a54aa

File tree

3 files changed

+56
-40
lines changed

3 files changed

+56
-40
lines changed

lib/runner/cli/clirunner.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,11 @@ CliRunner.prototype = {
276276
* Stops the selenium server if it is running
277277
* @returns {CliRunner}
278278
*/
279-
stopSelenium : function() {
279+
stopSelenium : function(callback) {
280280
if (this.manageSelenium) {
281-
Selenium.stopServer();
281+
Selenium.stopServer(callback);
282+
} else {
283+
callback();
282284
}
283285
return this;
284286
},
@@ -307,18 +309,18 @@ CliRunner.prototype = {
307309
testcase : self.argv.testcase,
308310
end_session_on_fail : self.endSessionOnFail
309311
}, function(err, results) {
310-
self.stopSelenium();
311-
312-
var globalsContext = self.test_settings && self.test_settings.globals || null;
313-
var afterGlobal = Utils.checkFunction('after', globalsContext) || function(done) {done();};
314-
315-
try {
316-
afterGlobal.call(globalsContext, function done() {
317-
self.globalErrorHandler(err, results, finished);
318-
});
319-
} catch (ex) {
320-
self.globalErrorHandler(ex, results, finished);
321-
}
312+
self.stopSelenium(function() {
313+
var globalsContext = self.test_settings && self.test_settings.globals || null;
314+
var afterGlobal = Utils.checkFunction('after', globalsContext) || function(done) {done();};
315+
316+
try {
317+
afterGlobal.call(globalsContext, function done() {
318+
self.globalErrorHandler(err, results, finished);
319+
});
320+
} catch (ex) {
321+
self.globalErrorHandler(ex, results, finished);
322+
}
323+
});
322324
});
323325
});
324326

@@ -522,13 +524,14 @@ CliRunner.prototype = {
522524

523525
this.startSelenium(function() {
524526
self.startChildProcesses(envs, function(o, code) {
525-
self.stopSelenium();
526-
if (done) {
527-
done(o, code);
528-
}
529-
if (code) {
530-
process.exit(code);
531-
}
527+
self.stopSelenium(function() {
528+
if (done) {
529+
done(o, code);
530+
}
531+
if (code) {
532+
process.exit(code);
533+
}
534+
});
532535
});
533536
});
534537

lib/runner/selenium.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ SeleniumServer.prototype.stop = function(callback) {
6868
}
6969
try {
7070
this.process.kill();
71-
callback(true);
71+
this.writeLogFile(callback);
7272
} catch (e) {
7373
Logger.warn('Selenium process could not be stopped.');
7474
console.log(e);
75-
callback(false);
75+
callback();
7676
}
7777
};
7878

@@ -81,18 +81,27 @@ SeleniumServer.prototype.exitHandler = function (code) {
8181
};
8282

8383
SeleniumServer.prototype.closeHandler = function() {
84+
Logger.info('Selenium process finished.');
85+
};
86+
87+
SeleniumServer.prototype.writeLogFile = function(callback) {
88+
if (this.settings.selenium.log_path === false) {
89+
callback();
90+
return;
91+
}
92+
8493
if (typeof this.settings.selenium.log_path == 'undefined') {
8594
this.settings.selenium.log_path = '';
8695
}
8796

88-
if (this.settings.selenium.log_path !== false) {
89-
fs.writeFile(path.join(this.settings.selenium.log_path, DEFAULT_LOG_FILE), this.output, 'utf8', function(err) {
90-
if (err) {
91-
console.log(Logger.colors.light_red('\nError writing log file to:'), err.path);
92-
}
93-
});
94-
}
95-
Logger.info('Selenium process finished.');
97+
var filePath = path.resolve(path.join(this.settings.selenium.log_path, DEFAULT_LOG_FILE));
98+
99+
fs.writeFile(filePath, this.output, function(err) {
100+
if (err) {
101+
console.log(Logger.colors.light_red('\nError writing log file to:'), err.path);
102+
}
103+
callback();
104+
});
96105
};
97106

98107
SeleniumServer.prototype.onError = function(err) {
@@ -143,7 +152,7 @@ module.exports = new (function() {
143152

144153
if (!server) {
145154
console.log('Selenium server is not running.');
146-
callback(false);
155+
callback();
147156
return;
148157
}
149158

lib/runner/testsuite.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ TestSuite.prototype.initAfterEachHook = function() {
130130
asyncArgsCount = 2;
131131
}
132132
asyncFn = Utils.makeFnAsync(asyncArgsCount, hookFn, module);
133-
return self.makePromise(function(done) {
134-
var doneFn = self.adaptDoneCallback(done, 'afterEach');
133+
return self.makePromise(function(done, deferred) {
134+
var doneFn = self.adaptDoneCallback(done, 'afterEach', deferred);
135135
if (expectedArgs < 2) {
136136
// user has only supplied the done callback argument (pre v0.6 behaviour), e.g.:
137137
// afterEach : function(done) { ... }
@@ -298,8 +298,8 @@ TestSuite.prototype.globalAfterEach = function() {
298298
};
299299

300300
TestSuite.prototype.adaptGlobalHook = function(hookName) {
301-
return this.makePromise(function(done) {
302-
var doneFn = this.adaptDoneCallback(done, 'global ' + hookName);
301+
return this.makePromise(function(done, deffered) {
302+
var doneFn = this.adaptDoneCallback(done, 'global ' + hookName, deffered);
303303
var argsCount, expectedCount = 1;
304304
if (Utils.checkFunction(hookName, this.options.globals)) {
305305
argsCount = this.options.globals[hookName].length;
@@ -318,10 +318,14 @@ TestSuite.prototype.adaptGlobalHook = function(hookName) {
318318
});
319319
};
320320

321-
TestSuite.prototype.adaptDoneCallback = function(done, hookName) {
321+
TestSuite.prototype.adaptDoneCallback = function(done, hookName, deferred) {
322322
var timeout = setTimeout(function() {
323-
throw new Error('done() callback timeout was reached while executing '+ hookName + '.' +
324-
' Make sure to call the done() callback when the operation finishes.');
323+
try {
324+
throw new Error('done() callback timeout was reached while executing '+ hookName + '.' +
325+
' Make sure to call the done() callback when the operation finishes.');
326+
} catch (err) {
327+
deferred.reject(err);
328+
}
325329
}, ASYNC_HOOK_TIMEOUT);
326330

327331
return function() {
@@ -338,7 +342,7 @@ TestSuite.prototype.makePromise = function (fn) {
338342
try {
339343
fn.call(this, function() {
340344
deferred.resolve();
341-
});
345+
}, deferred);
342346
} catch (e) {
343347
deferred.reject(e);
344348
}

0 commit comments

Comments
 (0)