-
Notifications
You must be signed in to change notification settings - Fork 68
/
runner.js
77 lines (69 loc) · 1.76 KB
/
runner.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
/* Module to be used by other web driver runners like protractor*/
var Metrics = require('./metrics'),
options = require('./options'),
helpers = require('./helpers'),
Q = require('q'),
debug = require('debug')('bp:selenium:runner'),
wd = require('wd');
var metrics = null;
function Runner(opts) {
this.opts = options.scrub(opts);
this.metrics = null;
this.browser = null;
};
Runner.prototype.setupMetrics_ = function() {
var me = this;
if (this.metrics === null) {
this.metrics = new Metrics(this.opts.metrics);
return this.metrics.setup(this.opts);
} else {
return Q();
}
};
Runner.prototype.attachBrowser_ = function(sessionId) {
if (this.browser === null) {
this.browser = wd.promiseRemote(this.opts.selenium);
this.browser.on('status', function(data) {
//log.debug(data);
});
this.browser.on('command', function(meth, path, data) {
if (data && typeof data === 'object') {
var data = JSON.stringify(data);
}
debug(meth, (path || '').substr(0, 70), (data || '').substr(0, 70));
});
return this.browser.attach(sessionId);
} else {
return Q();
}
};
Runner.prototype.config = function(cb) {
var me = this;
this.setupMetrics_().then(function() {
cb(null, me.opts);
}, function(err) {
cb(err);
});
}
Runner.prototype.start = function(sessionId, cb) {
var me = this;
if (typeof cb !== 'function') {
cb = function() {};
}
this.setupMetrics_().then(function() {
return me.attachBrowser_(sessionId);
}).then(function() {
me.metrics.start(me.browser).then(cb, cb);
});
};
Runner.prototype.stop = function(cb) {
var me = this;
this.metrics.teardown(this.browser).then(function() {
return me.metrics.getResults();
}).then(function(data) {
cb(undefined, data);
}, function(err) {
cb(err, null);
});
};
module.exports = Runner;