-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
101 lines (85 loc) · 2.31 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
/**!
* totoro-driver-base - index.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var debug = require('debug')('totoro-driver-base');
var os = require('os');
var SocketClient = require('socket.io-client');
module.exports = TotoroDriver;
function TotoroDriver(options) {
this._server = options.server;
if (this._server.indexOf('http') !== 0) {
this._server = 'http://' + this._server;
}
this._server = this._server.replace(/(\/+)$/, '');
}
var DEVICES = TotoroDriver.DEVICES = {
darwin: 'mac',
win32: 'pc',
linux: 'linux',
};
var OSNAMES = TotoroDriver.OSNAMES = {
win32: 'windows',
darwin: 'macosx',
linux: 'linux',
};
var proto = TotoroDriver.prototype;
proto.init = function (callback) {
var laborURL = this._server + '/__labor';
debug('connecting to %s', laborURL);
this.socket = SocketClient.connect(laborURL);
this.socket.on('connect', function () {
var ua = this._getUserAgent();
debug('connect totoro server with User-Agent: %j', ua);
this.socket.emit('init', ua);
if (callback) {
callback();
}
}.bind(this));
this.socket.on('add', this.onAdd.bind(this));
this.socket.on('remove', this.onRemove.bind(this));
this.socket.on('disconnect', this.onDisconnect.bind(this));
};
proto._getUserAgent = function () {
var ua = {
device: { name: DEVICES[process.platform] },
os: { name: OSNAMES[process.platform], version: os.release() },
browser: this.getBrowser(),
};
return ua;
};
proto.getBrowser = function () {
throw new Error('Child class must impl this method, return {name, version}');
};
proto.onAdd = function (data) {
/**
* structure of data
* {
* orderId: '{{orderId}}',
* laborId: '{{laborId}}',
* ua: {{specifed browser ua}},
* url: {{test runner url}}
* }
*/
throw new Error('Child class must impl this method');
};
proto.onRemove = function (data) {
// the structure is the same as 'add' event's but without the ua
throw new Error('Child class must impl this method');
};
proto.cleanup = function () {
throw new Error('Child class must impl this method');
};
proto.onDisconnect = function () {
debug('socket disconnect, cleanup()');
this.cleanup();
};