-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
686 lines (595 loc) · 16.5 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*!
*
* 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 os = require('os');
var cp = require('child_process');
var appium = require('appium/lib/server/main');
var portscanner = require('portscanner');
/**
* This module is a browser plugin for [DalekJS](//github.com/dalekjs/dalek).
* It provides all a WebDriverServer & browser launcher for Safari on iOS.
*
* At the moment this only works with the IPhone
*
* The browser plugin can be installed with the following command:
*
* ```bash
* $ npm install dalek-browser-ios --save-dev
* ```
*
* You can use the browser plugin by adding a config option to the your Dalekfile
*
* ```javascript
* "browsers": ["ios"]
* ```
*
* Or you can tell Dalek that it should test in this browser via the command line:
*
* ```bash
* $ dalek mytest.js -b ios
* ```
*
* The Webdriver Server tries to open Port 9003 by default,
* if this port is blocked, it tries to use a port between 9004 & 9093
* You can specifiy a different port from within your [Dalekfile](/pages/config.html) like so:
*
* ```javascript
* "browsers": {
* "ios": {
* "port": 5555
* }
* }
* ```
*
* It is also possible to specify a range of ports:
*
* ```javascript
* "browsers": {
* "ios": {
* "portRange": [6100, 6120]
* }
* }
* ```
*
* If you would like to test on the IPad (IPhone) emulator, you can simply apply a snd. argument,
* which defines the browser type:
*
* ```bash
* $ dalek mytest.js -b ios:ipad
* ```
*
* @module DalekJS
* @class IosDriver
* @namespace Browser
* @part iOS
* @api
*/
var IosDriver = {
/**
* Verbose version of the browser name
*
* @property longName
* @type string
* @default Mobile Safari iOS
*/
longName: 'Mobile Safari iOS (iPhone)',
/**
* Default port of the Appium WebDriverServer
* The port may change, cause the port conflict resolution
* tool might pick another one, if the default one is blocked
*
* @property port
* @type integer
* @default 4723
*/
port: 4723,
/**
* WebHook port
*
* @property webhookPort
* @type integer
* @default 9003
*/
webhookPort: 9003,
/**
* Default host of the Appium WebDriverServer
* The host may be overridden with
* a user configured value
*
* @property host
* @type string
* @default localhost
*/
host: 'localhost',
/**
* Root path of the appium webdriver server
*
* @property path
* @type string
* @default /wd/hub
*/
path: '/wd/hub',
/**
* Default desired capabilities that should be
* transferred when the browser session gets requested
*
* @property desiredCapabilities
* @type object
*/
desiredCapabilities: {
device: 'iPhone Emulator',
name: 'Safari remote via WD',
app: 'safari',
version: '6.1',
browserName: ''
},
/**
* Driver defaults, what should the driver be able to access.
*
* @property driverDefaults
* @type object
*/
driverDefaults: {
viewport: true,
status: {
os: {
arch: os.arch(),
version: os.release(),
name: 'Mac OSX'
}
},
sessionInfo: true
},
/**
* Special arguments that are needed to invoke
* appium. These are the defaults, they need to be modified later on
*
* @property appiumArgs
* @type object
*/
appiumArgs: {
app: null,
ipa: null,
quiet: true,
udid: null,
keepArtifacts: false,
noSessionOverride: false,
fullReset: false,
noReset: false,
launch: false,
log: false,
nativeInstrumentsLib: false,
safari: false,
forceIphone: false,
forceIpad: false,
orientation: null,
useKeystore: false,
address: '0.0.0.0',
nodeconfig: null,
port: null,
webhook: null
},
/**
* Different browser types (iPhone / iPad)
*
* @property browserTypes
* @type object
*/
browserTypes: {
/**
* IPad emulator
*
* @property ipad
* @type object
*/
ipad: {
name: 'iPad'
}
},
/**
* Resolves the driver port
*
* @method getPort
* @return {integer} port WebDriver server port
*/
getPort: function () {
return this.port;
},
/**
* Resolves the maximum range for the driver port
*
* @method getMaxPort
* @return {integer} port Max WebDriver server port range
*/
getMaxPort: function () {
return this.maxPort;
},
/**
* Resolves the webhook port
*
* @method getWebhookPort
* @return {integer} WebHook server port
*/
getWebhookPort: function () {
return this.webhookPort;
},
/**
* Resolves the maximum range for the webhook port
*
* @method getWebhookPort
* @return {integer} WebHook Max WebHook port
*/
getMaxWebhookPort: function () {
return this.maxWebhookPort;
},
/**
* Returns the driver host
*
* @method getHost
* @return {string} host WebDriver server hostname
*/
getHost: function () {
return this.host;
},
/**
* Launches appium & corresponding emulator or device,
* kicks off the portscanner
*
* @method launch
* @param {object} configuration Browser configuration
* @param {EventEmitter2} events EventEmitter (Reporter Emitter instance)
* @param {Dalek.Internal.Config} config Dalek configuration class
* @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 if the user wants to run the iPad emulator
if (configuration && configuration.type === 'ipad') {
this.longName = this.longName.replace('iPhone', 'iPad');
this.appiumArgs.forceIpad = true;
}
// 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 Appium Server process,
* kills simulator processses
* with a slight timeout to prevent
* appium from throwing errors
*
* @method kill
* @chainable
*/
kill: function () {
// kill appium servers
this.appiumServer.webSocket.server.close();
this.appiumServer.rest.listen().close();
// slight timeout for process killing
setTimeout(this._processes.bind(this, this._kill.bind(this)), 1000);
return this;
},
/**
* Kills the non blacklisted simulator processes & restores
* the stderr handler
*
* @method _kill
* @param {object|null} err Error or null
* @param {array} result List of currently running simulator processes
* @chainable
* @private
*/
_kill: function (err, result) {
// kill simulator processes
result.forEach(this._killProcess.bind(this));
// (re)establish stderr/stdout stream
this._reinstantiateLog();
return this;
},
/**
* Checks a blacklist & kills the process when
* not found
*
* @method _killProcess
* @param {integer} processID Process ID
* @chainable
* @private
*/
_killProcess: function (processID) {
var kill = true;
// walk through the list of processes that are
// open before the driver started
this.openProcesses.forEach(function (pid) {
if (pid === processID) {
kill = false;
}
});
if (kill === true) {
cp.spawn('kill', [processID]);
}
return this;
},
/**
* Checks & switches the appium server port,
* scans the range for the webhook port
*
* @method _listProcesses
* @param {object} deferred Promise
* @param {object|null} err Error or null
* @param {integer} port Appium server port to use
* @chainable
* @private
*/
_checkPorts: function (deferred, error, 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-ios: Switching to port: ' + port);
this.port = port;
}
// check if the current webhook port is in use, if so, scan for free ports
portscanner.findAPortNotInUse(this.getWebhookPort(), this.getMaxWebhookPort(), this.getHost(), this._launch.bind(this, deferred));
return this;
},
/**
* Checks & switches the webhook port,
* loads a list of running simulator processes
*
* @method _listProcesses
* @param {object} deferred Promise
* @param {object|null} err Error or null
* @param {integer} port Webhook port to use
* @chainable
* @private
*/
_launch: function (deferred, error, port) {
// check if the port was blocked & if we need to switch to another port
if (this.webhookPort !== port) {
this.reporterEvents.emit('report:log:system', 'dalek-browser-ios: Switching to webhook port: ' + port);
this.webhookPort = port;
}
// launch appium & the emulator
this._processes(this._listProcesses.bind(this, deferred));
return this;
},
/**
* Stores open processes,
* suppresses stdout logs,
* starts appium
*
* @method _listProcesses
* @param {object} deferred Promise
* @param {object|null} err Error or null
* @param {array} result List of currently running simulator processes
* @chainable
* @private
*/
_listProcesses: function (deferred, err, result) {
// save list of open emulator processes, before we launched it
this.openProcesses = result;
// nasty hack to surpress socket.io debug reports from appium
this._suppressAppiumLogs();
// run appium
appium.run(this._loadAppiumArgs(this.appiumArgs), this._afterAppiumStarted.bind(this, deferred));
return this;
},
/**
* Stores the appium server reference,
* restores the stdout logs
*
* @method _afterAppiumStarted
* @param {object} deferred Promise
* @param {object} appiumServer Appium server instance
* @chainable
* @private
*/
_afterAppiumStarted: function (deferred, appiumServer) {
this.appiumServer = appiumServer;
deferred.resolve();
return this;
},
/**
* Configures appium
*
* @method _loadAppiumArgs
* @param {object} appiumArgs Appium specific configuration
* @return {object} Modified appium configuration
* @private
*/
_loadAppiumArgs: function (appiumArgs) {
appiumArgs.port = this.getPort();
appiumArgs.webhook = this.getHost() + ':' + this.getWebhookPort();
return appiumArgs;
},
/**
* Process user defined ports
*
* @method _checkUserDefinedPorts
* @param {object} browser Browser configuration
* @chainable
* @private
*/
_checkUserDefinedPorts: function (browser) {
this._checkAppiumPorts(browser);
this._checkWebhookPorts(browser);
return this;
},
/**
* Process user defined appium ports
*
* @method _checkAppiumPorts
* @param {object} browser Browser configuration
* @chainable
* @private
*/
_checkAppiumPorts: function (browser) {
// check for a single defined port
if (browser.ios && browser.ios.port) {
this.port = parseInt(browser.ios.port, 10);
this.maxPort = this.port + 90;
this.reporterEvents.emit('report:log:system', 'dalek-browser-ios: Switching to user defined port: ' + this.port);
}
// check for a port range
if (browser.ios && browser.ios.portRange && browser.ios.portRange.length === 2) {
this.port = parseInt(browser.ios.portRange[0], 10);
this.maxPort = parseInt(browser.ios.portRange[1], 10);
this.reporterEvents.emit('report:log:system', 'dalek-browser-ios: Switching to user defined port(s): ' + this.port + ' -> ' + this.maxPort);
}
return this;
},
/**
* Process user defined webhook ports
*
* @method _checkWebhookPorts
* @param {object} browser Browser configuration
* @chainable
* @private
*/
_checkWebhookPorts: function (browser) {
// check for a single defined webhook port
if (browser.ios && browser.ios.webhookPort) {
this.webhookPort = parseInt(browser.ios.webhookPort, 10);
this.maxWebhookPort = this.webhookPort + 90;
this.reporterEvents.emit('report:log:system', 'dalek-browser-ios: Switching to user defined webhook port: ' + this.webhookPort);
}
// check for a webhook port range
if (browser.ios && browser.ios.webhookPortRange && browser.ios.webhookPortRange.length === 2) {
this.webhookPort = parseInt(browser.ios.webhookPortRange[0], 10);
this.maxWebhookPort = parseInt(browser.ios.webhookPortRange[1], 10);
this.reporterEvents.emit('report:log:system', 'dalek-browser-ios: Switching to user defined webhook port(s): ' + this.webhookPort + ' -> ' + this.maxWebhookPort);
}
return this;
},
/**
* Tracks running simulator processes
*
* @method _processes
* @param {function} fn Callback
* @chainable
* @private
*/
_processes: function (fn) {
var cmd = ['ps -ax', '|', 'grep "iPhone Simulator.app"'];
cp.exec(cmd.join(' '), this._transformProcesses.bind(this, fn));
return this;
},
/**
* Transforms the process list output into
* a json structure
*
* @method _transformProcesses
* @param {function} fn Callback
* @param {null|object} err Error if error, null if not
* @param {string} stdout Terminal output
* @chainable
* @private
*/
_transformProcesses: function(fn, err, stdout){
var result = [];
stdout.split('\n').forEach(this._scanProcess.bind(this, result));
fn(err, result);
return this;
},
/**
* Scans and transforms the process list
*
* @method _scanProcess
* @param {array} result Transformed result
* @param {string} line Process list entry
* @chainable
* @private
*/
_scanProcess: function (result, line){
var data = line.split(' ');
data = data.filter(this._filterProcessItem);
if (data[1] === '??') {
result.push(data[0]);
}
return this;
},
/**
* Filters process list items
*
* @method _filterProcessItem
* @param {string} item Process list entry
* @return {bool|string} Process item or false
* @private
*/
_filterProcessItem: function (item) {
if (item !== '') {
return item;
}
return false;
},
/**
* Overwrite default stdout & stderr handler
* to suppress some appium logs
*
* @method _suppressAppiumLogs
* @chainable
* @private
*/
_suppressAppiumLogs: function () {
// TODO: Check if the log level of appium can be set to 0
var _supLogs = function (data) {
if (data.search('6minfo') === -1 && data.search('33mwarn') === -1 && data.search('90mdebug') === -1) {
this.oldWrite.bind(process.stdout)(data);
}
}.bind(this);
// store old std. handler
this.oldWrite = process.stdout.write;
this.oldWriteErr = process.stderr.write;
// overwrite with ugliness
process.stdout.write = _supLogs;
process.stderr.write = _supLogs;
return this;
},
/**
* Reinstantiate stdout handler after appium has
* been started
*
* @method _reinstantiateLog
* @chainable
* @private
*/
_reinstantiateLog: function () {
setTimeout(function () {
process.stdout.write = this.oldWrite;
process.stderr.write = this.oldWriteErr;
}.bind(this), 8000);
return this;
}
};
// expose the module
module.exports = IosDriver;