Skip to content

Commit

Permalink
fix: kill all IE processes when terminating browser process
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-hamel committed Mar 30, 2014
1 parent c20733a commit de86563
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,62 @@
var fs = require('fs');
var exec = require('child_process').exec;

var IEBrowser = function(baseBrowserDecorator, args) {
var processName = 'iexplore.exe';

var IEBrowser = function(baseBrowserDecorator, logger, args) {
baseBrowserDecorator(this);

var log = logger.create('launcher');
args = args || {};
var flags = args.flags || [];

this._getOptions = function(url) {
return [
'-extoff'
].concat(flags, [url]);
};

var baseOnProcessExit = this._onProcessExit;
this._onProcessExit = function(code, errorOutput) {
var pid = this._process.pid;
killExtraIEProcess(pid, function() {
baseOnProcessExit(code, errorOutput);
});
};

// Spawning iexplore.exe spawns two processes (IE does that). The way karma kills the
// browser process (hard kill) leaves the other process in memory.
//
// The second process is created using command-line args like this:
// "C:\Program Files\Internet Explorer\iexplore.exe" SCODEF:2632 CREDAT:275457 /prefetch:2
// Where the SCODEF value is the pid of the 'original' process created by this launcher.
//
// This function kills any iexplore.exe process who's command line args match 'SCODEF:pid'.
// On IE11 this will kill the extra process. On older versions, no process will be found.
function killExtraIEProcess(pid, cb) {

var scodef = 'SCODEF:' + pid;

//wmic.exe : http://msdn.microsoft.com/en-us/library/aa394531(v=vs.85).aspx
var wmic = 'wmic.exe Path win32_Process ' +
'where "Name=\'' + processName + '\' and ' +
'CommandLine Like \'%' + scodef + '%\'" call Terminate';

exec(wmic, function(err) {
if (err) {
log.error('Killing extra IE process failed. ' + err);
}
else {
log.debug('Killed extra IE process ' + pid);
}
cb();
});

}
};

function getInternetExplorerExe() {
var suffix = '\\Internet Explorer\\iexplore.exe',
var suffix = '\\Internet Explorer\\' + processName,
prefixes = [process.env['PROGRAMW6432'], process.env['PROGRAMFILES(X86)'], process.env['PROGRAMFILES']],
prefix, i;

Expand All @@ -36,7 +78,7 @@ IEBrowser.prototype = {
ENV_CMD: 'IE_BIN'
};

IEBrowser.$inject = ['baseBrowserDecorator'];
IEBrowser.$inject = ['baseBrowserDecorator', 'logger', 'args'];


// PUBLISH DI MODULE
Expand Down

0 comments on commit de86563

Please sign in to comment.