-
Notifications
You must be signed in to change notification settings - Fork 3
/
driver.js
68 lines (54 loc) · 1.5 KB
/
driver.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
var wd;
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var ChildProcessSoftError = require('forq').Errors.ChildProcessSoftError;
wd = require('wd');
var colors = require('colors');
var supportedBrowsers = [
'firefox',
'chrome',
'safari'
];
function Driver (opts) {
if (!opts) { opts = {}; }
var self = this;
EventEmitter.call(this);
this._opts = opts;
this.host = opts.host;
this.port = opts.port;
this.browser = wd.promiseChainRemote(
this.host,
this.port
);
if ( opts.browserName && _.includes(supportedBrowsers, opts.browserName) ) {
this.browserName = opts.browserName;
} else {
this.browserName = undefined;
}
this.parentProcess = opts.parentProcess;
this.verbose = opts.verbose || false;
if (this.verbose) {
this.browser.on('status', function(info) {
console.log(' > ', (info || '').magenta );
});
this.browser.on('command', function(meth, path, data) {
console.log(' > ', meth.green, path, (data || '').magenta );
});
}
function oninit(err, id, capabilities) {
if (err) {
err.name = 'Browser Start Error';
var e = new ChildProcessSoftError(err);
// broadcast browser startup error
self.emit('error', e);
} else {
self.emit('ready');
self.status = 'ready';
}
}
this.browser.init({ browserName: this.browserName }, oninit);
this.browser._wd = wd;
}
util.inherits(Driver, EventEmitter);
module.exports = Driver;