siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
250 lines (249 loc) • 11.2 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var types = require("../../../generic/simulator/Types");
var ServerEndPoint_js_1 = require("../../channel/websocket/ServerEndPoint.js");
var Point_js_1 = require("../../../generic/util/Point.js");
var Delay_js_1 = require("../../../generic/util/helper/Delay.js");
var SimulatorServerPuppeteer = /** @class */ (function (_super) {
__extends(SimulatorServerPuppeteer, _super);
function SimulatorServerPuppeteer() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.mouseMovePrecision = 3;
// see https://github.com/octalmage/robotjs/issues/347
_this.afterActionDelay = 100;
_this.currentPosition = [0, 0];
return _this;
}
SimulatorServerPuppeteer.prototype.doProcessRawChannelMessage = function (message, envelop) {
};
SimulatorServerPuppeteer.prototype.doDispatchEnvelop = function (envelop) {
var _this = this;
var cmd = envelop.payload;
var action;
switch (cmd.type) {
case 'move_pointer':
action = this.doMovePointer(cmd);
break;
case 'set_pointer_state':
action = this.doSetPointerState(cmd);
break;
case 'pointer_click':
action = this.doPointerClick(cmd);
break;
case 'pointer_double_click':
action = this.doPointerDoubleClick(cmd);
break;
case 'mouse_wheel':
action = this.doMouseWheel(cmd);
break;
case 'pointer_drag':
action = this.doPointerDrag(cmd);
break;
case 'type':
action = this.doType(cmd);
break;
case 'reset':
action = this.doSimulationReset(cmd);
break;
case 'set_key_state':
action = this.doSetKeyState(cmd);
break;
}
action.then(function (result) { return _this.replyWith(envelop, result); }, function (reason) {
_this.info("Simulator command failed: " + reason);
_this.replyWith(envelop, reason, true);
});
};
SimulatorServerPuppeteer.prototype.pageMouseMove = function (x, y, options) {
var _this = this;
if (options === void 0) { options = undefined; }
return this.page.mouse.move(x, y, options).then(function () {
_this.currentPosition[0] = x;
_this.currentPosition[1] = y;
});
};
SimulatorServerPuppeteer.prototype.siestaKeyToSimulatorKey = function (key) {
if (key.length == 1)
return key;
return siestaToPuppeteerKeys[key];
};
SimulatorServerPuppeteer.prototype.toggleModifierKeys = function (modifierKeys, state) {
var _this = this;
if (modifierKeys === void 0) { modifierKeys = []; }
var cont = Promise.resolve();
modifierKeys.forEach(function (modKey) {
cont = cont.then(function () { return _this.page.keyboard[state](_this.siestaKeyToSimulatorKey(modKey)); });
});
return cont;
};
SimulatorServerPuppeteer.prototype.doWithModifierKeys = function (action, modifierKeys) {
var _this = this;
if (modifierKeys === void 0) { modifierKeys = []; }
return this.toggleModifierKeys(modifierKeys, types.KeyState.Down).then(function () { return action(); }).then(function () { return _this.toggleModifierKeys(modifierKeys, types.KeyState.Up); });
};
SimulatorServerPuppeteer.prototype.doMovePointer = function (command) {
var _this = this;
return this.doWithModifierKeys(function () {
if (command.moveKind == 'instant')
return _this.page.mouse.move(command.x, command.y);
else if (command.moveKind == 'smooth') {
return _this.moveMouseTo(command.x, command.y, command.mouseMovePrecision);
}
}, command.modifierKey);
};
SimulatorServerPuppeteer.prototype.doSetPointerState = function (command) {
var _this = this;
return this.doWithModifierKeys(function () { return _this.page.mouse[command.state]({ button: command.modifier }); }, command.modifierKey).then(function () { return Delay_js_1.delay(_this.afterActionDelay); });
};
SimulatorServerPuppeteer.prototype.doPointerClick = function (command) {
var _this = this;
var mouse = this.page.mouse;
return this.doWithModifierKeys(function () {
return mouse.down({ button: command.modifier, clickCount: 1 }).then(function () { return mouse.up({ button: command.modifier, clickCount: 1 }); });
}, command.modifierKey).then(function () { return Delay_js_1.delay(_this.afterActionDelay); });
};
SimulatorServerPuppeteer.prototype.doPointerDoubleClick = function (command) {
var _this = this;
var mouse = this.page.mouse;
return this.doWithModifierKeys(function () {
return mouse.down({ button: command.modifier, clickCount: 1 }).then(function () { return mouse.up({ button: command.modifier, clickCount: 1 }); }).then(function () { return mouse.down({ button: command.modifier, clickCount: 2 }); }).then(function () { return mouse.up({ button: command.modifier, clickCount: 2 }); });
}, command.modifierKey).then(function () { return Delay_js_1.delay(_this.afterActionDelay); });
};
SimulatorServerPuppeteer.prototype.doSimulationReset = function (command) {
var _this = this;
var cont = Promise.resolve();
Object.keys(siestaToPuppeteerKeys).forEach(function (siestaKey) {
cont = cont.then(function () { return _this.page.keyboard.up(_this.siestaKeyToSimulatorKey(siestaKey)); });
});
Object.keys(types.PointerModifier).forEach(function (button) {
if (isNaN(Number(button))) {
cont = cont.then(function () { return _this.page.mouse.up({ button: types.PointerModifier[button] }); });
}
});
cont = cont.then(function () { return _this.pageMouseMove(0, 0); }).then(function () { return Delay_js_1.delay(_this.afterActionDelay); });
return cont;
};
SimulatorServerPuppeteer.prototype.doMouseWheel = function (command) {
var _this = this;
return this.doWithModifierKeys(function () {
}, command.modifierKey).then(function () { return Delay_js_1.delay(_this.afterActionDelay); });
};
SimulatorServerPuppeteer.prototype.doPointerDrag = function (command) {
var _this = this;
var mouse = this.page.mouse;
return this.doWithModifierKeys(function () {
return _this.moveMouseTo(command.fromX, command.fromY).then(function () { return mouse.down({ button: command.modifier }); }).then(function () { return _this.moveMouseTo(command.toX, command.toY, 1); }).then(function () { return command.dragOnly ? Promise.resolve() : mouse.up({ button: command.modifier }); });
}, command.modifierKey).then(function () { return Delay_js_1.delay(_this.afterActionDelay); });
};
SimulatorServerPuppeteer.prototype.doSetKeyState = function (command) {
var keyboard = this.page.keyboard;
return keyboard[command.state](this.siestaKeyToSimulatorKey(command.key));
};
SimulatorServerPuppeteer.prototype.doType = function (command) {
var _this = this;
var text = command.text;
var cont = Promise.resolve();
var _loop_1 = function (i) {
cont = cont.then(function () { return _this.typeSingleChar(text[i], command.modifierKey); });
};
for (var i = 0; i < text.length; i++) {
_loop_1(i);
}
return cont.then(function () { return Delay_js_1.delay(_this.afterActionDelay); });
};
SimulatorServerPuppeteer.prototype.typeSingleChar = function (key, modifierKey) {
var _this = this;
if (modifierKey === void 0) { modifierKey = []; }
var keyboard = this.page.keyboard;
if (key.length > 1)
return this.doWithModifierKeys(function () { return keyboard.press(_this.siestaKeyToSimulatorKey(key)); }, modifierKey);
else {
// seems no special processing is needed for Puppeteer, see analogous RobotJS method however
return this.doWithModifierKeys(function () { return keyboard.press(_this.siestaKeyToSimulatorKey(key)); }, modifierKey);
}
};
SimulatorServerPuppeteer.prototype.moveMouseTo = function (x, y, mouseMovePrecision) {
var _this = this;
if (mouseMovePrecision === void 0) { mouseMovePrecision = null; }
var path = Point_js_1.getPathBetweenPoints(this.currentPosition, [x, y]);
path = Point_js_1.filterPathAccordingToPrecision(path, mouseMovePrecision || this.mouseMovePrecision);
var cont = Promise.resolve();
path.forEach(function (step) {
cont = cont.then(function () { return _this.pageMouseMove(step[0], step[1]); });
});
return cont;
};
return SimulatorServerPuppeteer;
}(ServerEndPoint_js_1.ServerEndPoint));
exports.SimulatorServerPuppeteer = SimulatorServerPuppeteer;
var siestaToPuppeteerKeys = {
'BACKSPACE': 'Backspace',
'TAB': 'Tab',
'RETURN': 'Enter',
'ENTER': 'Enter',
//special
'SHIFT': 'Shift',
'CTRL': 'Control',
'ALT': 'Alt',
'CMD': 'Meta',
// //weird
'PAUSE-BREAK': 'Pause',
'CAPS': 'CapsLock',
'ESCAPE': 'Escape',
'ESC': 'Escape',
'NUM-LOCK': 'NumLock',
'SCROLL-LOCK': 'ScrollLock',
'PRINT': 'Print',
//navigation
'PAGE-UP': 'PageUp',
'PAGE-DOWN': 'PageDown',
'END': 'End',
'HOME': 'Home',
'LEFT': 'ArrowLeft',
'ARROWLEFT': 'ArrowLeft',
'UP': 'ArrowUp',
'ARROWUP': 'ArrowUp',
'RIGHT': 'ArrowRight',
'ARROWRIGHT': 'ArrowRight',
'DOWN': 'ArrowDown',
'ARROWDOWN': 'ArrowDown',
'INSERT': 'Insert',
'DELETE': 'Delete',
//NORMAL-CHARACTERS, NUMPAD
'NUM0': 'Numpad0',
'NUM1': 'Numpad1',
'NUM2': 'Numpad2',
'NUM3': 'Numpad3',
'NUM4': 'Numpad4',
'NUM5': 'Numpad5',
'NUM6': 'Numpad6',
'NUM7': 'Numpad7',
'NUM8': 'Numpad8',
'NUM9': 'Numpad9',
'F1': 'F1',
'F2': 'F2',
'F3': 'F3',
'F4': 'F4',
'F5': 'F5',
'F6': 'F6',
'F7': 'F7',
'F8': 'F8',
'F9': 'F9',
'F10': 'F10',
'F11': 'F11',
'F12': 'F12'
};
exports.SimulatorServer = SimulatorServerPuppeteer;