forked from dalekjs/dalek-browser-ie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
406 lines (351 loc) · 9.73 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*!
*
* Copyright (c) 2013 Sebastian Golasch
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
'use strict';
// ext. libs
var Q = require('q');
var cp = require('child_process');
var portscanner = require('portscanner');
// int. libs
var iedriver = require('./lib/iedriver');
/**
* This module is a browser plugin for [DalekJS](//github.com/dalekjs/dalek).
* It provides all a WebDriverServer & browser launcher for Internet Explorer.
*
* The browser plugin can be installed with the following command:
*
* ```bash
* $ npm install dalek-browser-ie --save-dev
* ```
*
* You can use the browser plugin by adding a config option to the your Dalekfile
*
* ```javascript
* "browsers": ["IE"]
* ```
*
* Or you can tell Dalek that it should test in this browser via the command line:
*
* ```bash
* $ dalek mytest.js -b IE
* ```
*
* The Webdriver Server tries to open Port 5555 by default,
* if this port is blocked, it tries to use a port between 5555 & 5564
* You can specifiy a different port from within your [Dalekfile](/pages/config.html) like so:
*
* ```javascript
* "browsers": {
* "ie": {
* "port": 6555
* }
* }
* ```
*
* It is also possible to specify a range of ports:
*
* ```javascript
* "browsers": {
* "ie": {
* "portRange": [6100, 6120]
* }
* }
* ```
*
* @module DalekJS
* @class InternetExplorer
* @namespace Browser
* @part InternetExplorer
* @api
*/
var InternetExplorer = {
/**
* Verbose version of the browser name
*
* @property longName
* @type string
* @default Internet Explorer
* @api
*/
longName: 'Internet Explorer',
/**
* Default port of the IEDriverServer
* The port may change, cause the port conflict resultion
* tool might pick another one, if the default one is blocked
*
* @property port
* @type integer
* @default 5555
*/
port: 5555,
/**
* Default maximum port of the IEDriverServer
* The port is the highest port in the range that can be allocated
* by the IEDriverServer
*
* @property maxPort
* @type integer
* @default 5654
*/
maxPort: 5654,
/**
* Default host of the IEDriverServer
* The host may be overridden with
* a user configured value
*
* @property host
* @type string
* @default localhost
*/
host: 'localhost',
/**
* Default desired capabilities that should be
* transferred when the browser session gets requested
*
* @property desiredCapabilities
* @type object
*/
desiredCapabilities: {
browserName: 'InternetExplorer'
},
/**
* Driver defaults, what should the driver be able to access.
*
* @property driverDefaults
* @type object
*/
driverDefaults: {
viewport: true,
status: true,
sessionInfo: true
},
/**
* Path to the IEDriverServer.exe file
*
* @property path
* @type string
* @default /
*/
path: '/',
/**
* Child process instance of the IEDriverServer
*
* @property spawned
* @type null|Object
*/
spawned: null,
/**
* IE processes that are running on startup,
* and therefor shouldn`t be closed
*
* @property openProcesses
* @type array
* @default []
*/
openProcesses: [],
/**
* Resolves the driver port
*
* @method getPort
* @return {integer} port WebDriver server port
*/
getPort: function () {
return this.port;
},
/**
* Returns the driver host
*
* @method getHost
* @return {string} host WebDriver server hostname
*/
getHost: function () {
return this.host;
},
/**
* Resolves the maximum range for the driver port
*
* @method getMaxPort
* @return {integer} port Max WebDriver server port range
*/
getMaxPort: function () {
return this.maxPort;
},
/**
* Launches the driver
* (the driver takes care of launching the browser)
*
* @method launch
* @return {object} promise Browser promise
*/
launch: function (configuration, events, config) {
var deferred = Q.defer();
// store injected configuration/log event handlers
this.reporterEvents = events;
this.configuration = configuration;
this.config = config;
// check for a user set port
var browsers = this.config.get('browsers');
if (browsers && Array.isArray(browsers)) {
browsers.forEach(this._checkUserDefinedPorts.bind(this));
}
// check if the current port is in use, if so, scan for free ports
portscanner.findAPortNotInUse(this.getPort(), this.getMaxPort(), this.getHost(), this._checkPorts.bind(this, deferred));
return deferred.promise;
},
/**
* Kills the driver & browser processes
*
* @method kill
* @chainable
*/
kill: function () {
// get a list of all running processes
this._list(function(svc){
// filter out the browser process
svc.forEach(function (item, idx) {
Object.keys(item).forEach(function (key) {
if(svc[idx][key] === 'iexplore.exe') {
// kill the browser process
this._kill(svc[idx].PID, true);
}
}.bind(this));
}.bind(this));
}.bind(this),true);
// kill the driver process
this.spawned.kill('SIGTERM');
return this;
},
_checkPorts: function (deferred, err, port) {
// check if the port was blocked & if we need to switch to another port
if (this.port !== port) {
this.reporterEvents.emit('report:log:system', 'dalek-browser-ie: Switching to port: ' + port);
this.port = port;
}
// invoke the ie driver afterwards
this._startIEdriver(deferred);
return this;
},
_startIEdriver: function (deferred) {
var stream = '';
this.spawned = cp.spawn(iedriver.path, ['--port=' + this.getPort()]);
this.spawned.stdout.on('data', function (data) {
var dataStr = data + '';
stream += dataStr;
if (stream.search('Listening on port') !== -1) {
deferred.resolve();
}
});
return this;
},
/**
* Process user defined ports
*
* @method _checkUserDefinedPorts
* @param {object} browser Browser configuration
* @chainable
* @private
*/
_checkUserDefinedPorts: function (browser) {
// check for a single defined port
if (browser.ie && browser.ie.port) {
this.port = parseInt(browser.ie.port, 10);
this.maxPort = this.port + 90;
this.reporterEvents.emit('report:log:system', 'dalek-browser-ie: Switching to user defined port: ' + this.port);
}
// check for a port range
if (browser.ie && browser.ie.portRange && browser.ie.portRange.length === 2) {
this.port = parseInt(browser.ie.portRange[0], 10);
this.maxPort = parseInt(browser.ie.portRange[1], 10);
this.reporterEvents.emit('report:log:system', 'dalek-browser-ie: Switching to user defined port(s): ' + this.port + ' -> ' + this.maxPort);
}
return this;
},
/**
* Lists all running processes (win only)
*
* @method _list
* @param {Function} callback Receives the process object as the only callback argument
* @param {Boolean} [verbose=false] Verbose output
* @chainable
* @private
*/
_list: function(callback, verbose) {
verbose = typeof verbose === 'boolean' ? verbose : false;
cp.exec('tasklist /FO CSV' + (verbose === true ? ' /V' : ''), function (err, stdout) {
var pi = stdout.split('\r\n');
var p = [];
pi.forEach(function (line) {
if (line.trim().length !== 0) {
p.push(line);
}
});
var proc = [];
var head = null;
while (p.length > 1) {
var rec = p.shift();
rec = rec.replace(/\"\,/gi,'";').replace(/\"|\'/gi,'').split(';');
if (head === null){
head = rec;
for (var i=0;i<head.length;i++){
head[i] = head[i].replace(/ /gi,'');
}
if (head.indexOf('PID')<0){
head[1] = 'PID';
}
} else {
var tmp = {};
for (var j=0;j<rec.length;j++){
tmp[head[j]] = rec[j].replace(/\"|\'/gi,'');
}
proc.push(tmp);
}
}
callback(proc);
});
return this;
},
/**
* Kill a specific process (win only)
*
* @method _kill
* @param {Number} PID Process ID
* @param {Boolean} [force=false] Force close the process.
* @param {Function} [callback] Callback after process has been killed
* @chainable
* @private
*/
_kill: function(pid, force, callback) {
if (!pid || isNaN(parseInt(pid))){
throw new Error('PID is required for the kill operation.');
}
callback = callback || function(){};
if (typeof force === 'function'){
callback = force;
force = false;
}
cp.exec('taskkill /PID ' + pid + (force === true ? ' /f' : ''),callback);
return this;
}
};
// expose the module
module.exports = InternetExplorer;