UNPKG

@qavajs/steps-wdio

Version:

qavajs steps to interact with webdriverio

262 lines 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.virtualPointer = exports.Keys = void 0; exports.parseCoords = parseCoords; exports.parseCoordsAsObject = parseCoordsAsObject; exports.parseKeySequence = parseKeySequence; exports.equalOrIncludes = equalOrIncludes; exports.getEventValue = getEventValue; exports.isImmediate = isImmediate; exports.dragAndDrop = dragAndDrop; const MODIFIER_KEYS = ['Control', 'Shift', 'Alt', 'Command', 'Ctrl']; /** * Parse 'x, y' string to coordinates array * @param {string} coords - 'x, y' string * @return {number[]} - coords array */ function parseCoords(coords) { return coords.split(/\s?,\s?/).map((c) => parseFloat(c !== null && c !== void 0 ? c : 0)); } /** * Parse 'x, y' string to coordinates object * @param {string} coords - 'x, y' string * @return {{x: number, y: number}} - coords object */ function parseCoordsAsObject(coords) { const [x, y] = coords.split(/\s?,\s?/).map((c) => parseFloat(c !== null && c !== void 0 ? c : 0)); return { x, y }; } function getKey(key) { if (key in Keys) { // @ts-ignore return Keys[key]; } return key; } function parseKeySequence(sequence) { // @ts-ignore if (sequence in Keys) return [Keys[sequence]]; if (Array.isArray(sequence)) return sequence; if (MODIFIER_KEYS.some(key => sequence.includes(key))) return sequence.split('+').map(getKey); return sequence.split(''); } function equalOrIncludes(value, argument) { return Array.isArray(value) ? value.includes(argument) : value === argument; } function getEventValue(entity) { return (entity === null || entity === void 0 ? void 0 : entity.event) ? entity.event : entity; } function isImmediate(validation) { return validation.includes('present') && validation.includes('not'); } function dragAndDrop(source, target) { ({ dragdrop: function (sourceElement, targetElement) { const CustomDataTransfer = function () { this.data = {}; }; CustomDataTransfer.prototype.dropEffect = `move`; CustomDataTransfer.prototype.effectAllowed = `all`; CustomDataTransfer.prototype.files = []; CustomDataTransfer.prototype.items = []; CustomDataTransfer.prototype.types = []; CustomDataTransfer.prototype.clearData = function (format) { if (format) { delete this.data[format]; const index = this.types.indexOf(format); delete this.types[index]; delete this.data[index]; } else { this.data = {}; } }; CustomDataTransfer.prototype.setData = function (format, data) { this.data[format] = data; this.items.push(data); this.types.push(format); }; CustomDataTransfer.prototype.getData = function (format) { if (format in this.data) { return this.data[format]; } return ``; }; CustomDataTransfer.prototype.setDragImage = function () { }; const sourceCoordinates = sourceElement.getBoundingClientRect(); const targetCoordinates = targetElement.getBoundingClientRect(); const mouseDownEvent = this.createEvent(`mousedown`, { clientX: sourceCoordinates.left, clientY: sourceCoordinates.top }); sourceElement.dispatchEvent(mouseDownEvent); const dragStartEvent = this.createEvent(`dragstart`, { clientX: sourceCoordinates.left, clientY: sourceCoordinates.top, // @ts-ignore dataTransfer: new CustomDataTransfer() }); sourceElement.dispatchEvent(dragStartEvent); const dragEvent = this.createEvent(`drag`, { clientX: sourceCoordinates.left, clientY: sourceCoordinates.top }); sourceElement.dispatchEvent(dragEvent); const dragEnterEvent = this.createEvent(`dragenter`, { clientX: targetCoordinates.left, clientY: targetCoordinates.top, // @ts-ignore dataTransfer: dragStartEvent.dataTransfer }); targetElement.dispatchEvent(dragEnterEvent); const dragOverEvent = this.createEvent(`dragover`, { clientX: targetCoordinates.left, clientY: targetCoordinates.top, // @ts-ignore dataTransfer: dragStartEvent.dataTransfer }); targetElement.dispatchEvent(dragOverEvent); const dropEvent = this.createEvent(`drop`, { clientX: targetCoordinates.left, clientY: targetCoordinates.top, // @ts-ignore dataTransfer: dragStartEvent.dataTransfer }); targetElement.dispatchEvent(dropEvent); const dragEndEvent = this.createEvent(`dragend`, { clientX: targetCoordinates.left, clientY: targetCoordinates.top, // @ts-ignore dataTransfer: dragStartEvent.dataTransfer }); sourceElement.dispatchEvent(dragEndEvent); const mouseUpEvent = this.createEvent(`mouseup`, { clientX: targetCoordinates.left, clientY: targetCoordinates.top }); targetElement.dispatchEvent(mouseUpEvent); }, createEvent: function (eventName, options) { const event = document.createEvent(`CustomEvent`); event.initCustomEvent(eventName, true, true, null); event.view = window; event.detail = 0; event.ctlrKey = false; event.altKey = false; event.shiftKey = false; event.metaKey = false; event.button = 0; event.relatedTarget = null; if (options.clientX && options.clientY) { event.screenX = window.screenX + options.clientX; event.screenY = window.screenY + options.clientY; } for (const prop in options) { event[prop] = options[prop]; } return event; } }).dragdrop(source, target); } var Keys; (function (Keys) { Keys["Ctrl"] = "WDIO_CONTROL"; Keys["NULL"] = "\uE000"; Keys["Cancel"] = "\uE001"; Keys["Help"] = "\uE002"; Keys["Backspace"] = "\uE003"; Keys["Tab"] = "\uE004"; Keys["Clear"] = "\uE005"; Keys["Return"] = "\uE006"; Keys["Enter"] = "\uE007"; Keys["Shift"] = "\uE008"; Keys["Control"] = "\uE009"; Keys["Alt"] = "\uE00A"; Keys["Pause"] = "\uE00B"; Keys["Escape"] = "\uE00C"; Keys["Space"] = "\uE00D"; Keys["PageUp"] = "\uE00E"; Keys["PageDown"] = "\uE00F"; Keys["End"] = "\uE010"; Keys["Home"] = "\uE011"; Keys["ArrowLeft"] = "\uE012"; Keys["ArrowUp"] = "\uE013"; Keys["ArrowRight"] = "\uE014"; Keys["ArrowDown"] = "\uE015"; Keys["Insert"] = "\uE016"; Keys["Delete"] = "\uE017"; Keys["Semicolon"] = "\uE018"; Keys["Equals"] = "\uE019"; Keys["Numpad0"] = "\uE01A"; Keys["Numpad1"] = "\uE01B"; Keys["Numpad2"] = "\uE01C"; Keys["Numpad3"] = "\uE01D"; Keys["Numpad4"] = "\uE01E"; Keys["Numpad5"] = "\uE01F"; Keys["Numpad6"] = "\uE020"; Keys["Numpad7"] = "\uE021"; Keys["Numpad8"] = "\uE022"; Keys["Numpad9"] = "\uE023"; Keys["Multiply"] = "\uE024"; Keys["Add"] = "\uE025"; Keys["Separator"] = "\uE026"; Keys["Subtract"] = "\uE027"; Keys["Decimal"] = "\uE028"; Keys["Divide"] = "\uE029"; Keys["F1"] = "\uE031"; Keys["F2"] = "\uE032"; Keys["F3"] = "\uE033"; Keys["F4"] = "\uE034"; Keys["F5"] = "\uE035"; Keys["F6"] = "\uE036"; Keys["F7"] = "\uE037"; Keys["F8"] = "\uE038"; Keys["F9"] = "\uE039"; Keys["F10"] = "\uE03A"; Keys["F11"] = "\uE03B"; Keys["F12"] = "\uE03C"; Keys["Command"] = "\uE03D"; Keys["ZenkakuHankaku"] = "\uE040"; })(Keys || (exports.Keys = Keys = {})); /** * Class represents virtual mouse pointer to support actions */ class VirtualPointer { constructor() { this.origin = 'viewport'; this.x = 0; this.y = 0; } hover(element) { this.origin = element; this.x = 0; this.y = 0; } move(x, y) { this.x = x; this.y = y; } pointer() { return { origin: this.origin, x: this.x, y: this.y }; } wheel() { return { origin: typeof this.origin !== 'string' ? this.origin : undefined, x: this.x, y: this.y }; } } exports.virtualPointer = new VirtualPointer(); //# sourceMappingURL=utils.js.map