appium python action的一些分析

今天在论坛上看到一个朋友提问:

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}

出现越界的情况了。

结论


所以确实换了一个位置,确实很产生很大的影响。这个确实得注意才行。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值