cabbie-async
Version:
An asynchronous webdriver client
1,042 lines (794 loc) • 37.9 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _selectorTypes = require('./enums/selector-types');
var _selectorTypes2 = _interopRequireDefault(_selectorTypes);
var _driver = require('./driver');
var _driver2 = _interopRequireDefault(_driver);
var _options = require('./flow-types/options');
var _depd = require('depd');
var _depd2 = _interopRequireDefault(_depd);
var _url = require('url');
var _alert = require('./alert');
var _alert2 = _interopRequireDefault(_alert);
var _addDebugging = require('./add-debugging');
var _addDebugging2 = _interopRequireDefault(_addDebugging);
var _element = require('./element');
var _element2 = _interopRequireDefault(_element);
var _frame = require('./frame');
var _frame2 = _interopRequireDefault(_frame);
var _globalMouse = require('./global-mouse');
var _globalMouse2 = _interopRequireDefault(_globalMouse);
var _globalTouch = require('./global-touch');
var _globalTouch2 = _interopRequireDefault(_globalTouch);
var _navigator = require('./navigator');
var _navigator2 = _interopRequireDefault(_navigator);
var _baseWindow = require('./base-window');
var _baseWindow2 = _interopRequireDefault(_baseWindow);
var _windowHandle = require('./window-handle');
var _windowHandle2 = _interopRequireDefault(_windowHandle);
var _waitFor = require('./utils/wait-for');
var _waitFor2 = _interopRequireDefault(_waitFor);
var _flowRuntime = require('flow-runtime');
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Options = _flowRuntime2.default.tdz(function () {
return _options.Options;
}); /**
*
* This file is generated automatically, run npm run build to re-generate.
**/
var Driver = _flowRuntime2.default.tdz(function () {
return _driver2.default;
});
var SelectorType = _flowRuntime2.default.tdz(function () {
return _selectorTypes.SelectorType;
});
var deprecate = (0, _depd2.default)('cabbie');
/*
* This is an object representing the currently active window. You can access the navigation for that window,
* touch and mouse objects for interacting via screen coordinates, and (most imporatantly) getElement and getElements.
*/
var ActiveWindow = function (_BaseWindow) {
(0, _inherits3.default)(ActiveWindow, _BaseWindow);
/*
* Get the Alert object.
*/
/*
* Get the Navigator object.
*
* @private
*/
/*
* The global-touch object.
*/
function ActiveWindow(driver /*: Driver*/, options /*: Options*/) {
(0, _classCallCheck3.default)(this, ActiveWindow);
var _driverType = _flowRuntime2.default.ref(Driver);
var _optionsType = _flowRuntime2.default.ref(Options);
_flowRuntime2.default.param('driver', _driverType).assert(driver);
_flowRuntime2.default.param('options', _optionsType).assert(options);
var _this = (0, _possibleConstructorReturn3.default)(this, (ActiveWindow.__proto__ || (0, _getPrototypeOf2.default)(ActiveWindow)).call(this, driver, 'current'));
_this.touch = new _globalTouch2.default(_this.driver);
_this.mouse = new _globalMouse2.default(_this.driver);
_this.navigator = new _navigator2.default(_this.driver, options);
_this.frame = new _frame2.default(_this.driver);
_this.alert = new _alert2.default(_this.driver);
_this._options = options;
deprecate.property(_this, 'navigator', 'All properties of navigator are now directly available on the ActiveWindow object');
return _this;
}
/*
* Get a handle for the current window
*/
/*
* Get the Frame object.
*/
/*
* Get the global-mouse object.
*/
(0, _createClass3.default)(ActiveWindow, [{
key: 'getWindowHandle',
value: function getWindowHandle() /*: Promise<WindowHandle>*/ {
var _returnType, windowHandle;
return _regenerator2.default.async(function getWindowHandle$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_returnType = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref(_windowHandle2.default), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.ref(_windowHandle2.default))));
_context.next = 3;
return _regenerator2.default.awrap(this.driver.requestJSON('GET', '/window_handle'));
case 3:
windowHandle = _context.sent;
return _context.abrupt('return', _returnType.assert(new _windowHandle2.default(this.driver, windowHandle)));
case 5:
case 'end':
return _context.stop();
}
}
}, null, this);
}
/*
* Execute a script on the browser and return the result.
*
* Source should be either a function body as a string or a function.
* Keep in mind that if it is a function it will not have access to
* any variables from the node.js process.
*/
}, {
key: 'execute',
value: function execute(script /*: string | Function*/) /*: Promise<any>*/ {
var args /*: Array<any>*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var _scriptType, _argsType, _returnType2;
return _regenerator2.default.async(function execute$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_scriptType = _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.function());
_argsType = _flowRuntime2.default.array(_flowRuntime2.default.any());
_returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.any(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.any())));
_flowRuntime2.default.param('script', _scriptType).assert(script);
_flowRuntime2.default.param('args', _argsType).assert(args);
return _context2.abrupt('return', _returnType2.assert(this.driver.requestJSON('POST', '/execute', { script: codeToString(script), args: args })));
case 6:
case 'end':
return _context2.stop();
}
}
}, null, this);
}
/*
* Execute a script asynchronously on the browser.
*
* Source should be either a function body as a string or a function.
* Keep in mind that if it is a function it will not have access to
* any variables from the node.js process.
*/
}, {
key: 'asyncExecute',
value: function asyncExecute(script /*: string | Function*/) /*: Promise<void>*/ {
var args /*: Array<any>*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var _scriptType2, _argsType2, _returnType3;
return _regenerator2.default.async(function asyncExecute$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_scriptType2 = _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.function());
_argsType2 = _flowRuntime2.default.array(_flowRuntime2.default.any());
_returnType3 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_flowRuntime2.default.param('script', _scriptType2).assert(script);
_flowRuntime2.default.param('args', _argsType2).assert(args);
_context3.next = 7;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/execute_async', { script: codeToString(script), args: args }));
case 7:
case 'end':
return _context3.stop();
}
}
}, null, this);
}
/*
* Type a string of characters into the browser
*
* Note: Modifier keys is kept between calls, so mouse interactions can be performed
* while modifier keys are depressed.
*/
}, {
key: 'sendKeys',
value: function sendKeys(str /*: string | Array<string>*/) /*: Promise<void>*/ {
var _strType, _returnType4;
return _regenerator2.default.async(function sendKeys$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_strType = _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.array(_flowRuntime2.default.string()));
_returnType4 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_flowRuntime2.default.param('str', _strType).assert(str);
_context4.next = 5;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/keys', { value: Array.isArray(str) ? str : [str] }));
case 5:
case 'end':
return _context4.stop();
}
}
}, null, this);
}
/*
* Take a screenshot of the current page
*
* This returns the result as a buffer containing the binary image data
*/
}, {
key: 'takeScreenshot',
value: function takeScreenshot() /*: Promise<Buffer>*/ {
var _returnType5, base64data;
return _regenerator2.default.async(function takeScreenshot$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_returnType5 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref('Buffer'), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.ref('Buffer'))));
_context5.next = 3;
return _regenerator2.default.awrap(this.driver.requestJSON('GET', '/screenshot'));
case 3:
base64data = _context5.sent;
return _context5.abrupt('return', _returnType5.assert(Buffer.from ? Buffer.from(base64data, 'base64') : new Buffer(base64data, 'base64')));
case 5:
case 'end':
return _context5.stop();
}
}
}, null, this);
}
/*
* Get the element on the page that currently has focus
*/
}, {
key: 'getActiveElement',
value: function getActiveElement() /*: Promise<Element>*/ {
var _returnType6, elementHandle;
return _regenerator2.default.async(function getActiveElement$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_returnType6 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref(_element2.default), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.ref(_element2.default))));
_context6.next = 3;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/element/active'));
case 3:
elementHandle = _context6.sent;
return _context6.abrupt('return', _returnType6.assert(new _element2.default(this.driver, this.driver, '<active>', elementHandle)));
case 5:
case 'end':
return _context6.stop();
}
}
}, null, this);
}
/*
* Get an element via a selector.
* Will throw an error if the element does not exist.
*/
}, {
key: 'getElement',
value: function getElement(selector /*: string*/) /*: Promise<Element>*/ {
var _this2 = this;
var selectorType /*: SelectorType*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _selectorTypes2.default.CSS;
var _selectorType, _selectorTypeType, _returnType7, elementHandle;
return _regenerator2.default.async(function getElement$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_selectorType = _flowRuntime2.default.string();
_selectorTypeType = _flowRuntime2.default.ref(SelectorType);
_returnType7 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref(_element2.default), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.ref(_element2.default))));
_flowRuntime2.default.param('selector', _selectorType).assert(selector);
_flowRuntime2.default.param('selectorType', _selectorTypeType).assert(selectorType);
_context7.next = 7;
return _regenerator2.default.awrap((0, _waitFor2.default)(function () {
return _this2.driver.requestJSON('POST', '/element', { using: selectorType, value: selector });
}));
case 7:
elementHandle = _context7.sent;
return _context7.abrupt('return', _returnType7.assert(new _element2.default(this.driver, this.driver, selector, elementHandle)));
case 9:
case 'end':
return _context7.stop();
}
}
}, null, this);
}
/*
* Get an element via a selector.
* Will return null if the element does not exist
*/
}, {
key: 'tryGetElement',
value: function tryGetElement(selector /*: string*/) /*: Promise<Element | null>*/ {
var selectorType /*: SelectorType*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _selectorTypes2.default.CSS;
var _selectorType2, _selectorTypeType2, _returnType8, elementHandle;
return _regenerator2.default.async(function tryGetElement$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_selectorType2 = _flowRuntime2.default.string();
_selectorTypeType2 = _flowRuntime2.default.ref(SelectorType);
_returnType8 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.union(_flowRuntime2.default.ref(_element2.default), _flowRuntime2.default.null()), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.union(_flowRuntime2.default.ref(_element2.default), _flowRuntime2.default.null()))));
_flowRuntime2.default.param('selector', _selectorType2).assert(selector);
_flowRuntime2.default.param('selectorType', _selectorTypeType2).assert(selectorType);
_context8.prev = 5;
_context8.next = 8;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/element', { using: selectorType, value: selector }));
case 8:
elementHandle = _context8.sent;
return _context8.abrupt('return', _returnType8.assert(new _element2.default(this.driver, this.driver, selector, elementHandle)));
case 12:
_context8.prev = 12;
_context8.t0 = _context8['catch'](5);
if (!(_context8.t0.code === 'NoSuchElement' || _context8.t0.code === 'ElementNotVisible' || _context8.t0.code === 'ElementIsNotSelectable')) {
_context8.next = 16;
break;
}
return _context8.abrupt('return', _returnType8.assert(null));
case 16:
throw _context8.t0;
case 17:
case 'end':
return _context8.stop();
}
}
}, null, this, [[5, 12]]);
}
/*
* Get elements via a selector.
*/
}, {
key: 'getElements',
value: function getElements(selector /*: string*/) /*: Promise<Array<Element>>*/ {
var _this3 = this;
var selectorType /*: SelectorType*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _selectorTypes2.default.CSS;
var _selectorType3, _selectorTypeType3, _returnType9, elementHandles;
return _regenerator2.default.async(function getElements$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_selectorType3 = _flowRuntime2.default.string();
_selectorTypeType3 = _flowRuntime2.default.ref(SelectorType);
_returnType9 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.array(_flowRuntime2.default.ref(_element2.default)), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.array(_flowRuntime2.default.ref(_element2.default)))));
_flowRuntime2.default.param('selector', _selectorType3).assert(selector);
_flowRuntime2.default.param('selectorType', _selectorTypeType3).assert(selectorType);
_context9.next = 7;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/elements', { using: selectorType, value: selector }));
case 7:
elementHandles = _context9.sent;
return _context9.abrupt('return', _returnType9.assert(elementHandles.map(function (elementHandle) {
return new _element2.default(_this3.driver, _this3.driver, selector, elementHandle);
})));
case 9:
case 'end':
return _context9.stop();
}
}
}, null, this);
}
/*
* Get elements by its text content, optionally narrowed down using a selector.
*
* N.B. this is **much** slower than getting elements by ID or css selector.
*/
}, {
key: 'getElementsByTextContent',
value: function getElementsByTextContent(textContent /*: string*/) /*: Promise<Array<Element>>*/ {
var selector /*: string*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '*';
var selectorType /*: SelectorType*/ = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _selectorTypes2.default.CSS;
var _textContentType, _selectorType4, _selectorTypeType4, _returnType10, elements, elementsToReturn, i;
return _regenerator2.default.async(function getElementsByTextContent$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
_textContentType = _flowRuntime2.default.string();
_selectorType4 = _flowRuntime2.default.string();
_selectorTypeType4 = _flowRuntime2.default.ref(SelectorType);
_returnType10 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.array(_flowRuntime2.default.ref(_element2.default)), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.array(_flowRuntime2.default.ref(_element2.default)))));
_flowRuntime2.default.param('textContent', _textContentType).assert(textContent);
_flowRuntime2.default.param('selector', _selectorType4).assert(selector);
_flowRuntime2.default.param('selectorType', _selectorTypeType4).assert(selectorType);
if (selector === 'a' && (selectorType === _selectorTypes2.default.CSS || selectorType === _selectorTypes2.default.TAG)) {
selector = _selectorType4.assert(textContent);
selectorType = _selectorTypeType4.assert(_selectorTypes2.default.PARTIAL_LINK_TEXT);
}
textContent = _textContentType.assert(textContent.trim());
_context10.next = 11;
return _regenerator2.default.awrap(this.getElements(selector, selectorType));
case 11:
elements = _context10.sent;
elementsToReturn = [];
i = 0;
case 14:
if (!(i < elements.length)) {
_context10.next = 24;
break;
}
_context10.t0 = textContent;
_context10.next = 18;
return _regenerator2.default.awrap(elements[i].getText());
case 18:
_context10.t1 = _context10.sent.trim();
if (!(_context10.t0 === _context10.t1)) {
_context10.next = 21;
break;
}
elementsToReturn.push(elements[i]);
case 21:
i++;
_context10.next = 14;
break;
case 24:
return _context10.abrupt('return', _returnType10.assert(elementsToReturn));
case 25:
case 'end':
return _context10.stop();
}
}
}, null, this);
}
/*
* Get elements by its text content, optionally narrowed down using a selector.
*
* N.B. this is **much** slower than getting elements by ID or css selector.
*/
}, {
key: 'getElementByTextContent',
value: function getElementByTextContent(textContent /*: string*/) /*: Promise<Element>*/ {
var _this4 = this;
var selector /*: string*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '*';
var selectorType /*: SelectorType*/ = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _selectorTypes2.default.CSS;
var _textContentType2, _selectorType5, _selectorTypeType5, _returnType11, element, err;
return _regenerator2.default.async(function getElementByTextContent$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
_textContentType2 = _flowRuntime2.default.string();
_selectorType5 = _flowRuntime2.default.string();
_selectorTypeType5 = _flowRuntime2.default.ref(SelectorType);
_returnType11 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref(_element2.default), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.ref(_element2.default))));
_flowRuntime2.default.param('textContent', _textContentType2).assert(textContent);
_flowRuntime2.default.param('selector', _selectorType5).assert(selector);
_flowRuntime2.default.param('selectorType', _selectorTypeType5).assert(selectorType);
_context11.next = 9;
return _regenerator2.default.awrap((0, _waitFor2.default)(function () {
return _this4.tryGetElementByTextContent(textContent, selector, selectorType);
}));
case 9:
element = _context11.sent;
if (!element) {
_context11.next = 12;
break;
}
return _context11.abrupt('return', _returnType11.assert(element));
case 12:
err = new Error('Could not find an element with the text content: ' + textContent);
err.name = 'NoSuchElement';
_flowRuntime2.default.any().assert(err).code = 'NoSuchElement';
throw err;
case 16:
case 'end':
return _context11.stop();
}
}
}, null, this);
}
/*
* Get elements by its text content, optionally narrowed down using a selector.
*
* N.B. this is **much** slower than getting elements by ID or css selector.
*/
}, {
key: 'tryGetElementByTextContent',
value: function tryGetElementByTextContent(textContent /*: string*/) /*: Promise<Element | null>*/ {
var selector /*: string*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '*';
var selectorType /*: SelectorType*/ = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _selectorTypes2.default.CSS;
var _textContentType3, _selectorType6, _selectorTypeType6, _returnType12, elements, i;
return _regenerator2.default.async(function tryGetElementByTextContent$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_textContentType3 = _flowRuntime2.default.string();
_selectorType6 = _flowRuntime2.default.string();
_selectorTypeType6 = _flowRuntime2.default.ref(SelectorType);
_returnType12 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.union(_flowRuntime2.default.ref(_element2.default), _flowRuntime2.default.null()), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.union(_flowRuntime2.default.ref(_element2.default), _flowRuntime2.default.null()))));
_flowRuntime2.default.param('textContent', _textContentType3).assert(textContent);
_flowRuntime2.default.param('selector', _selectorType6).assert(selector);
_flowRuntime2.default.param('selectorType', _selectorTypeType6).assert(selectorType);
if (selector === 'a' && (selectorType === _selectorTypes2.default.CSS || selectorType === _selectorTypes2.default.TAG)) {
selector = _selectorType6.assert(textContent);
selectorType = _selectorTypeType6.assert(_selectorTypes2.default.PARTIAL_LINK_TEXT);
}
textContent = _textContentType3.assert(textContent.trim().toLowerCase());
_context12.next = 11;
return _regenerator2.default.awrap(this.getElements(selector, selectorType));
case 11:
elements = _context12.sent;
i = 0;
case 13:
if (!(i < elements.length)) {
_context12.next = 23;
break;
}
_context12.t0 = textContent;
_context12.next = 17;
return _regenerator2.default.awrap(elements[i].getText());
case 17:
_context12.t1 = _context12.sent.trim().toLowerCase();
if (!(_context12.t0 === _context12.t1)) {
_context12.next = 20;
break;
}
return _context12.abrupt('return', _returnType12.assert(elements[i]));
case 20:
i++;
_context12.next = 13;
break;
case 23:
return _context12.abrupt('return', _returnType12.assert(null));
case 24:
case 'end':
return _context12.stop();
}
}
}, null, this);
}
/*
* Does a specific element exist?
*/
}, {
key: 'hasElement',
value: function hasElement(selector /*: string*/) /*: Promise<boolean>*/ {
var selectorType /*: SelectorType*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _selectorTypes2.default.CSS;
var _selectorType7, _selectorTypeType7, _returnType13, elements;
return _regenerator2.default.async(function hasElement$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_selectorType7 = _flowRuntime2.default.string();
_selectorTypeType7 = _flowRuntime2.default.ref(SelectorType);
_returnType13 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.boolean(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.boolean())));
_flowRuntime2.default.param('selector', _selectorType7).assert(selector);
_flowRuntime2.default.param('selectorType', _selectorTypeType7).assert(selectorType);
_context13.next = 7;
return _regenerator2.default.awrap(this.getElements(selector, selectorType));
case 7:
elements = _context13.sent;
return _context13.abrupt('return', _returnType13.assert(elements.length > 0));
case 9:
case 'end':
return _context13.stop();
}
}
}, null, this);
}
/*
* Close the current window
*/
}, {
key: 'close',
value: function close() /*: Promise<void>*/ {
var _returnType14;
return _regenerator2.default.async(function close$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
_returnType14 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_context14.next = 3;
return _regenerator2.default.awrap(this.driver.requestJSON('DELETE', '/window'));
case 3:
case 'end':
return _context14.stop();
}
}
}, null, this);
}
/*
* Get the current page title
*/
}, {
key: 'getTitle',
value: function getTitle() /*: Promise<string>*/ {
var _returnType15;
return _regenerator2.default.async(function getTitle$(_context15) {
while (1) {
switch (_context15.prev = _context15.next) {
case 0:
_returnType15 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.string())));
_context15.t0 = _returnType15;
_context15.next = 4;
return _regenerator2.default.awrap(this.driver.requestJSON('GET', '/title'));
case 4:
_context15.t1 = _context15.sent;
return _context15.abrupt('return', _context15.t0.assert.call(_context15.t0, _context15.t1));
case 6:
case 'end':
return _context15.stop();
}
}
}, null, this);
}
/*
* Get the current page source
*/
}, {
key: 'getSource',
value: function getSource() /*: Promise<string>*/ {
var _returnType16;
return _regenerator2.default.async(function getSource$(_context16) {
while (1) {
switch (_context16.prev = _context16.next) {
case 0:
_returnType16 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.string())));
_context16.t0 = _returnType16;
_context16.next = 4;
return _regenerator2.default.awrap(this.driver.requestJSON('GET', '/source'));
case 4:
_context16.t1 = _context16.sent;
return _context16.abrupt('return', _context16.t0.assert.call(_context16.t0, _context16.t1));
case 6:
case 'end':
return _context16.stop();
}
}
}, null, this);
}
/*
* Navigate forwards in the browser history, if possible.
*/
}, {
key: 'goForward',
value: function goForward() /*: Promise<void>*/ {
var _returnType17;
return _regenerator2.default.async(function goForward$(_context17) {
while (1) {
switch (_context17.prev = _context17.next) {
case 0:
_returnType17 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_context17.next = 3;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/forward'));
case 3:
case 'end':
return _context17.stop();
}
}
}, null, this);
}
/*
* Navigate backwards in the browser history, if possible.
*/
}, {
key: 'goBackward',
value: function goBackward() /*: Promise<void>*/ {
var _returnType18;
return _regenerator2.default.async(function goBackward$(_context18) {
while (1) {
switch (_context18.prev = _context18.next) {
case 0:
_returnType18 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_context18.next = 3;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/back'));
case 3:
case 'end':
return _context18.stop();
}
}
}, null, this);
}
/*
* Refreshes the browser
*/
}, {
key: 'refresh',
value: function refresh() /*: Promise<void>*/ {
var _returnType19;
return _regenerator2.default.async(function refresh$(_context19) {
while (1) {
switch (_context19.prev = _context19.next) {
case 0:
_returnType19 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_context19.next = 3;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/refresh'));
case 3:
case 'end':
return _context19.stop();
}
}
}, null, this);
}
/*
* Get the current url that the browser is displaying
*/
}, {
key: 'getUrl',
value: function getUrl() /*: Promise<string>*/ {
var _returnType20;
return _regenerator2.default.async(function getUrl$(_context20) {
while (1) {
switch (_context20.prev = _context20.next) {
case 0:
_returnType20 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.string())));
_context20.t0 = _returnType20;
_context20.next = 4;
return _regenerator2.default.awrap(this.driver.requestJSON('GET', '/url'));
case 4:
_context20.t1 = _context20.sent;
return _context20.abrupt('return', _context20.t0.assert.call(_context20.t0, _context20.t1));
case 6:
case 'end':
return _context20.stop();
}
}
}, null, this);
}
/*
* Navigates the browser to the specified path
*
* - if `path` begins with a "/" it is relative to `options.base`
* - if `path` begins with "http" it is absolute
* - otherwise it is relative to the current path
*/
}, {
key: 'navigateTo',
value: function navigateTo(path /*: string*/) /*: Promise<void>*/ {
var _pathType, _returnType21, base, _base;
return _regenerator2.default.async(function navigateTo$(_context21) {
while (1) {
switch (_context21.prev = _context21.next) {
case 0:
_pathType = _flowRuntime2.default.string();
_returnType21 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_flowRuntime2.default.param('path', _pathType).assert(path);
if (!(path[0] === '/')) {
_context21.next = 10;
break;
}
base = this._options.base;
if (base) {
_context21.next = 7;
break;
}
throw new Error('You must provide a "base" option to use urls starting with "/"');
case 7:
path = _pathType.assert(base.replace(/\/$/, '') + path);
_context21.next = 17;
break;
case 10:
if (!(path.indexOf('http') !== 0)) {
_context21.next = 17;
break;
}
_context21.next = 13;
return _regenerator2.default.awrap(this.getUrl());
case 13:
_base = _context21.sent;
_context21.next = 16;
return _regenerator2.default.awrap(this.navigateTo((0, _url.resolve)(_base, path)));
case 16:
return _context21.abrupt('return', _returnType21.assert());
case 17:
_context21.next = 19;
return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/url', { url: path }));
case 19:
case 'end':
return _context21.stop();
}
}
}, null, this);
}
}]);
return ActiveWindow;
}(_baseWindow2.default);
/*
* Convert code to string before execution
*/
function codeToString(code /*: string | Function*/) /*: string*/ {
var _codeType = _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.function());
var _returnType22 = _flowRuntime2.default.return(_flowRuntime2.default.string());
_flowRuntime2.default.param('code', _codeType).assert(code);
if (typeof code === 'function') {
// $FlowFixMe: intentionally concatenating string onto end of code
return 'return (' + code + ').apply(null, arguments);';
} else {
return _returnType22.assert(code);
}
}
_flowRuntime2.default.annotate(codeToString, _flowRuntime2.default.function(_flowRuntime2.default.param('code', _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.function())), _flowRuntime2.default.return(_flowRuntime2.default.string())));
(0, _addDebugging2.default)(ActiveWindow);
exports.default = ActiveWindow;