今天在论坛上看到一个朋友提问:
action.press(x=252, y=879).wait(10000).move_to(x=540, y=879).release().perform()
print '1'
action.press(x=252, y=879).move_to(x=540, y=879).wait(10000).release().perform()
print '2'
上面的代码一个执行了一个操作的是绝对坐标,一个操作了执行的是相对坐标,只是换了一个wait的位置为什么会产生这种结果呢,因为主要这里的逻辑都是Appium server进行处理的。我对node.js不太懂,今晚勉勉强强调试了好久。。。
分析
首先先看第一个操作
action.press(x=252, y=879).wait(10000).move_to(x=540, y=879).release().perform()
我们首先分析Server的log,显示的是
POST /wd/hubssion/063350c4-bd8c-4a7c-ac0c-96e35075f1d2/touch/perform {"sessionId":"063350c4-bd8c-4a7c-ac0c-96e35075f1d2","actions":[{"action":"press","options":{"y":879,"x":252}},{"action":"wait","options":{"ms":10000}},{"action":"moveTo","options":{"y":879,"x":540}},{"action":"release","options":{}}]}
我们首先看 controller.js 312
这里Server会解析出一个 gestures的数组里面包含了前面的4个action。
if (gestures.length === 4 &&
gestures[0].action === 'press' &&
gestures[1].action === 'wait' &&
gestures[2].action === 'moveTo' &&
gestures[3].action === 'release') {
return exports.mobileSwipe(req, res, gestures);
}
上面这个代码就可以看出 我们的脚本直接转换成了swipe的操作了。下来看看swipe的操作 497行
var touchCount = req.body.touchCount || 1
, startX = getCoordDefault(gestures[0].options.x)
, startY = getCoordDefault(gestures[0].options.y)
, endX = getCoordDefault(gestures[2].options.x)
, endY = getCoordDefault(gestures[2].options.y)
, duration = _getSwipeTouchDuration(gestures[1])
, element = gestures[0].options.element
, destElement = gestures[2].options.element || gestures[0].options.element;
这里就能够看出操作的是绝对坐标
那再来我们看看第二个操作。
还是一样 看看Server的输出吧
{"sessionId":"dfc437da-3ad5-49c1-8944-ed408fbbb73c","actions":[{"action":"press","options":{"y":879,"x":252}},{"action":"moveTo","options":{"y":879,"x":540}},{"action":"wait","options":{"ms":10000}},{"action":"release","options":{}}]}
因为这个顺序是不满足swipe的所以直接走的是
android-controller.js 中的 865 行 performTouch
这边Server的大体流程是会先将所有的命令,action都解析然后逐个进行操作。
performTouch中会执行下面的parseTouch
// fix release action then perform all actions
fixRelease(function (err) {
if (err) return cb(err);
this.parseTouch(gestures, false, function (err, fixedGestures) {
if (err) return cb(err);
async.eachSeries(fixedGestures, performGesture, cb);
});
}.bind(this));
所以我们还得看下parseTouch做了什么。
这里的代码很长我就不贴出来了,这里还是前面说的解析命令 将每个action的命令解析结果都存储在touchStateObjects这个数组里面。
touchStateObjects.push(touchStateObject);
之后就是遍历这个数组,逐个执行了。
var prevPos = null,
_.each(touchStateObjects, function (state) {
if (typeof state.options.x === 'undefined' && typeof state.options.x === 'undefined') {
// this happens with wait
state.options.x = prevPos.x;
state.options.y = prevPos.y;
}
if (state.options.offset && prevPos) {
// the current position is an offset
state.options.x += prevPos.x;
state.options.y += prevPos.y;
}
delete state.options.offset;
prevPos = state.options;
....
});
仔细看看就能够知道,首先执行press操作,这个时候的prevPos是为null的 所以x,y 坐标不会改变,接着会执行 prevPos = state.options
,所以到下一个moveTo的时候 就会执行到
state.options.x += prevPos.x;
state.options.y += prevPos.y;
所以MoveTo的坐标就变成了x=792.0, y=1758.0
所以就会出现Server的log如下
Returning result: {"value":"Coordinate [x=792.0, y=1758.0] is outside of element rect: [0,0][768,1184]","status":29}
出现越界的情况了。
结论
所以确实换了一个位置,确实很产生很大的影响。这个确实得注意才行。