appium 的 move_to 相对坐标?绝对坐标?

转载地址:https://testerhome.com/topics/4125


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'
action.press(x=D['b1']['x'], y=D['b1']['y']).wait(10000).move_to(x=D['b2']['x'], y=D['b2']['y']).release().perform()
print '3'
action.press(None,D['b1']['x'],D['b1']['y']).wait(10000).move_to(None,D['b2']['x'],D['b2']['y']).wait(800).move_to(None,D['b5']['x'],D['b5']['y']).wait(800).move_to(None,D['b8']['x'],D['b8']['y']).wait(800).move_to(None,D['b9']['x'],D['b9']['y']).release().perform()
print '4'

1和2的代码只是等待放置的位置不同,但是执行结果完全不一样,第一个取到的是绝对坐标绘制正确
2代码会提示坐标超出象限找不到,认为取的是相对坐标
3的执行结果和1一致,但是为何加了几个坐标点4就执行错误了?看到的表象和2一样,第二个点就没有找到。
有人可以解答一下吗?困惑好几天了。


========================================================================================================


我来具体解释下,可能说明的不太清楚,因为主要这里的逻辑都是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}

出现越界的情况了。

以上就是我分析的大概情况了。可能有点不太正确。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值