Adding get swipe options

This commit is contained in:
moizjv
2015-08-26 16:15:34 -07:00
parent 7120f4c75d
commit 1f04ea6cf4
2 changed files with 54 additions and 3 deletions
+28
View File
@@ -7,6 +7,7 @@ import log from './logger';
import { desiredCapabilityConstraints, validator } from './desired-caps';
import B from 'bluebird';
import _ from 'lodash';
import { util } from 'appium-support';
validator.promise = B;
validator.prettify = (val) => { return val; };
@@ -187,6 +188,33 @@ class BaseDriver extends MobileJsonWireProtocol {
}
}
async getSwipeOptions (gestures, touchCount = 1) {
let startX = this.helpers.getCoordDefault(gestures[0].options.x),
startY = this.helpers.getCoordDefault(gestures[0].options.y),
endX = this.helpers.getCoordDefault(gestures[2].options.x),
endY = this.helpers.getCoordDefault(gestures[2].options.y),
duration = this.helpers.getSwipeTouchDuration(gestures[1]),
element = gestures[0].options.element,
destElement = gestures[2].options.element || gestures[0].options.element;
// there's no destination element handling in bootstrap and since it applies to all platforms, we handle it here
if (util.hasValue(destElement)) {
let locResult = await this.getLocationInView(destElement);
let sizeResult = await this.getSize(destElement);
let offsetX = (Math.abs(endX) < 1 && Math.abs(endX) > 0) ? sizeResult.width * endX : endX;
let offsetY = (Math.abs(endY) < 1 && Math.abs(endY) > 0) ? sizeResult.height * endY : endY;
endX = locResult.x + offsetX;
endY = locResult.y + offsetY;
// if the target element was provided, the coordinates for the destination need to be relative to it.
if (util.hasValue(element)) {
let firstElLocation = await this.getLocationInView(element);
endX -= firstElLocation.x;
endY -= firstElLocation.y;
}
}
// clients are responsible to use these options correctly
return {startX, startY, endX, endY, duration, touchCount, element};
}
}
for (let [cmd, fn] of _.pairs(commands)) {
+26 -3
View File
@@ -4,7 +4,7 @@ import url from 'url';
import logger from './logger';
import _fs from 'fs';
import B from 'bluebird';
import { tempDir, system, fs } from 'appium-support';
import { tempDir, system, fs, util } from 'appium-support';
import { exec } from 'teen_process';
import AdmZip from 'adm-zip';
import request from 'request-promise';
@@ -188,5 +188,28 @@ function isPackageOrBundle (app) {
return (/^([a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+)+$/).test(app);
}
export default { configureApp, downloadApp, downloadFile, copyLocalZip,
unzipApp, unzipFile, testZipArchive, isPackageOrBundle };
function getCoordDefault (val) {
// going the long way and checking for undefined and null since
// we can't be assured `elId` is a string and not an int. Same
// thing with destElement below.
return util.hasValue(val) ? val : 0.5;
}
function getSwipeTouchDuration (waitGesture) {
// the touch action api uses ms, we want seconds
// 0.8 is the default time for the operation
let duration = 0.8;
if (typeof waitGesture.options.ms !== 'undefined' && waitGesture.options.ms) {
duration = waitGesture.options.ms / 1000;
if (duration === 0) {
// set to a very low number, since they wanted it fast
// but below 0.1 becomes 0 steps, which causes errors
duration = 0.1;
}
}
return duration;
}
export default {configureApp, downloadApp, downloadFile, copyLocalZip,
unzipApp, unzipFile, testZipArchive, isPackageOrBundle,
getCoordDefault, getSwipeTouchDuration};