Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: kill all IE processes when terminating browser process #16

Merged
merged 1 commit into from
Apr 1, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 65 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
var fs = require('fs');
var util = require('util');
var urlparse = require('url').parse;
var urlformat = require('url').format;
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');
var flags = args.flags || [];

// Handle x-ua-compatible option:
//
// Usage :
// customLaunchers: {
// IE9: {
// base: 'IE',
// 'x-ua-compatible': 'IE=EmulateIE9'
// }
// }
//
// This is done by passing the option on the url, in response the Karma server will
// set the following meta in the page.
// <meta http-equiv="X-UA-Compatible" content="[VALUE]"/>
function handleXUaCompatible(args, urlObj) {
if (args['x-ua-compatible']) {
urlObj.query['x-ua-compatible'] = args['x-ua-compatible'];
}
}

// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to define your functions before you use them :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dignifiedquire this project does not have code style check yet. I'm going to do this in #17. I'd merge this one as-is and fix the all the styles with #17 if you don't mind.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure


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();
});

}

this._getOptions = function (url) {
var urlObj = urlparse(url, true);

Expand All @@ -20,31 +72,19 @@ var IEBrowser = function(baseBrowserDecorator, args) {
'-extoff'
].concat(flags, [url]);
};
};

/**
* Handle x-ua-compatible option:
*
* Usage :
* customLaunchers: {
* IE9: {
* base: 'IE',
* 'x-ua-compatible': 'IE=EmulateIE9'
* }
* }
*
* This is done by passing the option on the url, in response the Karma server will
* set the following meta in the page.
* <meta http-equiv="X-UA-Compatible" content="[VALUE]"/>
*/
function handleXUaCompatible(args, urlObj) {
if (args['x-ua-compatible']) {
urlObj.query['x-ua-compatible'] = args['x-ua-compatible'];
}
}
var baseOnProcessExit = this._onProcessExit;
this._onProcessExit = function(code, errorOutput) {
var pid = this._process.pid;
killExtraIEProcess(pid, function() {
baseOnProcessExit(code, errorOutput);
});
};

};

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 @@ -66,8 +106,7 @@ IEBrowser.prototype = {
ENV_CMD: 'IE_BIN'
};

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

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

// PUBLISH DI MODULE
module.exports = {
Expand Down