cabbie-sync
Version:
A synchronous webdriver client
228 lines (163 loc) • 7.86 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _httpMethod = require('./flow-types/http-method');
var _webdriverResponse = require('./flow-types/webdriver-response');
var _httpResponse = require('./flow-types/http-response');
var _sessionData = require('./flow-types/session-data');
var _debug = require('./debug');
var _debug2 = _interopRequireDefault(_debug);
var _syncRequest = require('sync-request');
var _syncRequest2 = _interopRequireDefault(_syncRequest);
var _threadSleep = require('thread-sleep');
var _threadSleep2 = _interopRequireDefault(_threadSleep);
var _parseResponse = require('./utils/parse-response');
var _parseResponse2 = _interopRequireDefault(_parseResponse);
var _flowRuntime = require('flow-runtime');
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Debug = _flowRuntime2.default.tdz(function () {
return _debug2.default;
}); /**
*
* This file is generated automatically, run npm run build to re-generate.
**/
var Session = _flowRuntime2.default.tdz(function () {
return _sessionData.Session;
});
var HttpResponse = _flowRuntime2.default.tdz(function () {
return _httpResponse.HttpResponse;
});
var WebdriverResponse = _flowRuntime2.default.tdz(function () {
return _webdriverResponse.WebdriverResponse;
});
var HttpMethod = _flowRuntime2.default.tdz(function () {
return _httpMethod.HttpMethod;
});
var Connection = function () {
/*
* The url of the selenium web-driver server
*/
function Connection(remote /*: string*/, remoteURI /*: string*/, debug /*: Debug*/) {
(0, _classCallCheck3.default)(this, Connection);
var _remoteType = _flowRuntime2.default.string();
var _remoteURIType = _flowRuntime2.default.string();
var _debugType = _flowRuntime2.default.ref(Debug);
_flowRuntime2.default.param('remote', _remoteType).assert(remote);
_flowRuntime2.default.param('remoteURI', _remoteURIType).assert(remoteURI);
_flowRuntime2.default.param('debug', _debugType).assert(debug);
this.remote = remote;
this.remoteURI = remoteURI.replace(/\/$/, '');
this.debug = debug;
}
/*
* Session request with automatic parsing for errors
*/
/*
* Reference to the debug context
*
* @private
*/
/*
* The name or url of the selenium webdriver server (e.g. 'chromedriver')
*/
(0, _createClass3.default)(Connection, [{
key: 'requestWithSession',
value: function requestWithSession(session /*: Session*/, method /*: HttpMethod*/, uri /*: string*/, options /*:: ?: Object*/) /*: WebdriverResponse*/ {
var _sessionType = _flowRuntime2.default.ref(Session);
var _methodType = _flowRuntime2.default.ref(HttpMethod);
var _uriType = _flowRuntime2.default.string();
var _optionsType = _flowRuntime2.default.object();
var _returnType = _flowRuntime2.default.return(_flowRuntime2.default.ref(WebdriverResponse));
_flowRuntime2.default.param('session', _sessionType).assert(session);
_flowRuntime2.default.param('method', _methodType).assert(method);
_flowRuntime2.default.param('uri', _uriType).assert(uri);
_flowRuntime2.default.param('options', _optionsType, true).assert(options);
if (!/^https?\:\:\/\//.test(uri)) {
uri = _uriType.assert('/session/' + session.sessionID + uri);
}
var response = this.request(method, uri, options);
return _returnType.assert((0, _parseResponse2.default)(response));
}
/*
* Make a request without using the current session.
*/
}, {
key: 'request',
value: function request(method /*: HttpMethod*/, uri /*: string*/, options /*:: ?: Object*/) /*: HttpResponse*/ {
var _methodType2 = _flowRuntime2.default.ref(HttpMethod);
var _uriType2 = _flowRuntime2.default.string();
var _optionsType2 = _flowRuntime2.default.object();
var _returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.ref(HttpResponse));
_flowRuntime2.default.param('method', _methodType2).assert(method);
_flowRuntime2.default.param('uri', _uriType2).assert(uri);
_flowRuntime2.default.param('options', _optionsType2, true).assert(options);
if (!/^https?\:\:\/\//.test(uri)) {
uri = _uriType2.assert(this.remoteURI + uri);
}
var lastEx = null;
var maxAttempts = options && options.noRetries ? 1 : 10;
for (var i = 0; i < maxAttempts; i++) {
try {
this.debug.onRequest({ method: method, uri: uri, options: options });
var response = (0, _syncRequest2.default)(method, uri, options);
this.debug.onResponse(response);
return _returnType2.assert(response);
} catch (ex) {
lastEx = ex;
// sleep for 100ms, 200ms, 300ms, 400ms and so on
(0, _threadSleep2.default)(i * 100 + 100);
}
}
if (!lastEx) {
// this code sould be unreachable
throw new Error('Unknown error');
}
var err = improveError(this.remote, lastEx);
_flowRuntime2.default.any().assert(err).code = _flowRuntime2.default.any().assert(lastEx).code;
throw err;
}
}]);
return Connection;
}();
function improveError(remote /*: string*/, err /*: Error*/) {
var _remoteType2 = _flowRuntime2.default.string();
var _errType = _flowRuntime2.default.ref('Error');
_flowRuntime2.default.param('remote', _remoteType2).assert(remote);
_flowRuntime2.default.param('err', _errType).assert(err);
switch (remote) {
case 'chromedriver':
if (err.code === 'ECONNREFUSED') {
return new Error('Could not connect to chromedriver.\r\nThe easiest way to use chromedriver is to download the latest ' + 'release from http://chromedriver.chromium.org/downloads (just look for the highest version ' + 'number), then run `./chromedriver` in a separate terminal.');
}
break;
case 'taxirank':
if (err.code === 'ECONNREFUSED') {
return new Error('Could not connect to taxirank.\r\nThe easiest way to use taxirank is to run:\r\n\r\n' + ' npm install -g taxi-rank\r\n taxi-rank\r\n\r\n' + 'in your command line/terminal application.');
}
break;
case 'saucelabs':
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') {
return new Error('Could not connect to saucelabs.\r\nMake sure you are connected to the internet. If you are connected to ' + 'the internet, you could check their status page at https://status.saucelabs.com/');
}
break;
case 'browserstack':
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') {
return new Error('Could not connect to browserstack.\r\nMake sure you are connected to the internet. If you are connected to ' + 'the internet, you could check their twitter feed at https://twitter.com/browserstack');
}
break;
case 'testingbot':
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') {
return new Error('Could not connect to testingbot.\r\nMake sure you are connected to the internet. If you are connected to ' + 'the internet, you could check their status page at https://testingbot.statuspage.io/');
}
break;
}
return err;
}
_flowRuntime2.default.annotate(improveError, _flowRuntime2.default.function(_flowRuntime2.default.param('remote', _flowRuntime2.default.string()), _flowRuntime2.default.param('err', _flowRuntime2.default.ref('Error'))));
exports.default = Connection;