UNPKG

2.79 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.touchAction = exports.validateParameters = exports.formatArgs = void 0;
7const TOUCH_ACTIONS = ['press', 'longPress', 'tap', 'moveTo', 'wait', 'release'];
8const POS_ACTIONS = TOUCH_ACTIONS.slice(0, 4);
9const ACCEPTED_OPTIONS = ['x', 'y', 'element'];
10
11const formatArgs = function (scope, actions) {
12 return actions.map(action => {
13 if (Array.isArray(action)) {
14 return formatArgs(scope, action);
15 }
16
17 if (typeof action === 'string') {
18 action = {
19 action
20 };
21 }
22
23 const formattedAction = {
24 action: action.action,
25 options: {}
26 };
27 const actionElement = action.element && typeof action.element.elementId === 'string' ? action.element.elementId : scope.elementId;
28
29 if (POS_ACTIONS.includes(action.action) && actionElement) {
30 formattedAction.options.element = actionElement;
31 }
32
33 if (isFinite(action.x)) formattedAction.options.x = action.x;
34 if (isFinite(action.y)) formattedAction.options.y = action.y;
35 if (action.ms) formattedAction.options.ms = action.ms;
36
37 if (Object.keys(formattedAction.options).length === 0) {
38 delete formattedAction.options;
39 }
40
41 return formattedAction;
42 });
43};
44
45exports.formatArgs = formatArgs;
46
47const validateParameters = params => {
48 const options = Object.keys(params.options || {});
49
50 if (params.action === 'release' && options.length !== 0) {
51 throw new Error('action "release" doesn\'t accept any options ' + `("${options.join('", "')}" found)`);
52 }
53
54 if (params.action === 'wait' && (options.includes('x') || options.includes('y'))) {
55 throw new Error('action "wait" doesn\'t accept x or y options');
56 }
57
58 if (POS_ACTIONS.includes(params.action)) {
59 for (const option in params.options) {
60 if (!ACCEPTED_OPTIONS.includes(option)) {
61 throw new Error(`action "${params.action}" doesn't accept "${option}" as option`);
62 }
63 }
64
65 if (options.length === 0) {
66 throw new Error(`Touch actions like "${params.action}" need at least some kind of ` + 'position information like "element", "x" or "y" options, you\'ve none given.');
67 }
68 }
69};
70
71exports.validateParameters = validateParameters;
72
73const touchAction = function (actions) {
74 if (!this.multiTouchPerform || !this.touchPerform) {
75 throw new Error('touchAction can be used with Appium only.');
76 }
77
78 if (!Array.isArray(actions)) {
79 actions = [actions];
80 }
81
82 const formattedAction = formatArgs(this, actions);
83 const protocolCommand = Array.isArray(actions[0]) ? this.multiTouchPerform.bind(this) : this.touchPerform.bind(this);
84 formattedAction.forEach(params => validateParameters(params));
85 return protocolCommand(formattedAction);
86};
87
88exports.touchAction = touchAction;
\No newline at end of file