cabbie-async
Version:
An asynchronous webdriver client
283 lines (206 loc) • 9.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
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 _thenRequest = require('then-request');
var _thenRequest2 = _interopRequireDefault(_thenRequest);
var _thenSleep = require('./utils/then-sleep');
var _thenSleep2 = _interopRequireDefault(_thenSleep);
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*/) /*: Promise<WebdriverResponse>*/ {
var _sessionType, _methodType, _uriType, _optionsType, _returnType, response;
return _regenerator2.default.async(function requestWithSession$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_sessionType = _flowRuntime2.default.ref(Session);
_methodType = _flowRuntime2.default.ref(HttpMethod);
_uriType = _flowRuntime2.default.string();
_optionsType = _flowRuntime2.default.object();
_returnType = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref(WebdriverResponse), _flowRuntime2.default.ref('Promise', _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);
}
_context.next = 12;
return _regenerator2.default.awrap(this.request(method, uri, options));
case 12:
response = _context.sent;
return _context.abrupt('return', _returnType.assert((0, _parseResponse2.default)(response)));
case 14:
case 'end':
return _context.stop();
}
}
}, null, this);
}
/*
* Make a request without using the current session.
*/
}, {
key: 'request',
value: function request(method /*: HttpMethod*/, uri /*: string*/, options /*:: ?: Object*/) /*: Promise<HttpResponse>*/ {
var _methodType2, _uriType2, _optionsType2, _returnType2, lastEx, maxAttempts, i, response, err;
return _regenerator2.default.async(function request$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_methodType2 = _flowRuntime2.default.ref(HttpMethod);
_uriType2 = _flowRuntime2.default.string();
_optionsType2 = _flowRuntime2.default.object();
_returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref(HttpResponse), _flowRuntime2.default.ref('Promise', _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);
}
lastEx = null;
maxAttempts = options && options.noRetries ? 1 : 10;
i = 0;
case 11:
if (!(i < maxAttempts)) {
_context2.next = 29;
break;
}
_context2.prev = 12;
this.debug.onRequest({ method: method, uri: uri, options: options });
_context2.next = 16;
return _regenerator2.default.awrap((0, _thenRequest2.default)(method, uri, options));
case 16:
response = _context2.sent;
this.debug.onResponse(response);
return _context2.abrupt('return', _returnType2.assert(response));
case 21:
_context2.prev = 21;
_context2.t0 = _context2['catch'](12);
lastEx = _context2.t0;
// sleep for 100ms, 200ms, 300ms, 400ms and so on
_context2.next = 26;
return _regenerator2.default.awrap((0, _thenSleep2.default)(i * 100 + 100));
case 26:
i++;
_context2.next = 11;
break;
case 29:
if (lastEx) {
_context2.next = 31;
break;
}
throw new Error('Unknown error');
case 31:
err = improveError(this.remote, lastEx);
_flowRuntime2.default.any().assert(err).code = _flowRuntime2.default.any().assert(lastEx).code;
throw err;
case 34:
case 'end':
return _context2.stop();
}
}
}, null, this, [[12, 21]]);
}
}]);
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;