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

<code><span class="n"><span style="font-size:18px; color:#FF0000"><strong>转载地址:https://testerhome.com/topics/4125</strong></span>


action</span><span class="o">.</span><span class="n">press</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">252</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">879</span><span class="p">)</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">540</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">879</span><span class="p">)</span><span class="o">.</span><span class="n">release</span><span class="p">()</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="k">print</span> <span class="s">'1'</span>
<span class="n">action</span><span class="o">.</span><span class="n">press</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">252</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">879</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">540</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">879</span><span class="p">)</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span><span class="o">.</span><span class="n">release</span><span class="p">()</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="k">print</span> <span class="s">'2'</span>
<span class="n">action</span><span class="o">.</span><span class="n">press</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">D</span><span class="p">[</span><span class="s">'b1'</span><span class="p">][</span><span class="s">'x'</span><span class="p">],</span> <span class="n">y</span><span class="o">=</span><span class="n">D</span><span class="p">[</span><span class="s">'b1'</span><span class="p">][</span><span class="s">'y'</span><span class="p">])</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">D</span><span class="p">[</span><span class="s">'b2'</span><span class="p">][</span><span class="s">'x'</span><span class="p">],</span> <span class="n">y</span><span class="o">=</span><span class="n">D</span><span class="p">[</span><span class="s">'b2'</span><span class="p">][</span><span class="s">'y'</span><span class="p">])</span><span class="o">.</span><span class="n">release</span><span class="p">()</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="k">print</span> <span class="s">'3'</span>
<span class="n">action</span><span class="o">.</span><span class="n">press</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span><span class="n">D</span><span class="p">[</span><span class="s">'b1'</span><span class="p">][</span><span class="s">'x'</span><span class="p">],</span><span class="n">D</span><span class="p">[</span><span class="s">'b1'</span><span class="p">][</span><span class="s">'y'</span><span class="p">])</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span><span class="n">D</span><span class="p">[</span><span class="s">'b2'</span><span class="p">][</span><span class="s">'x'</span><span class="p">],</span><span class="n">D</span><span class="p">[</span><span class="s">'b2'</span><span class="p">][</span><span class="s">'y'</span><span class="p">])</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="mi">800</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span><span class="n">D</span><span class="p">[</span><span class="s">'b5'</span><span class="p">][</span><span class="s">'x'</span><span class="p">],</span><span class="n">D</span><span class="p">[</span><span class="s">'b5'</span><span class="p">][</span><span class="s">'y'</span><span class="p">])</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="mi">800</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span><span class="n">D</span><span class="p">[</span><span class="s">'b8'</span><span class="p">][</span><span class="s">'x'</span><span class="p">],</span><span class="n">D</span><span class="p">[</span><span class="s">'b8'</span><span class="p">][</span><span class="s">'y'</span><span class="p">])</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="mi">800</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span><span class="n">D</span><span class="p">[</span><span class="s">'b9'</span><span class="p">][</span><span class="s">'x'</span><span class="p">],</span><span class="n">D</span><span class="p">[</span><span class="s">'b9'</span><span class="p">][</span><span class="s">'y'</span><span class="p">])</span><span class="o">.</span><span class="n">release</span><span class="p">()</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="k">print</span> <span class="s">'4'</span></code>

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


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


我来具体解释下,可能说明的不太清楚,因为主要这里的逻辑都是Appium server进行处理的。我对node.js不太懂,今晚勉勉强强调试了好久。。。
首先先看第一个

<code><span class="nx">action</span><span class="p">.</span><span class="nx">press</span><span class="p">(</span><span class="nx">x</span><span class="o">=</span><span class="mi">252</span><span class="p">,</span> <span class="nx">y</span><span class="o">=</span><span class="mi">879</span><span class="p">).</span><span class="nx">wait</span><span class="p">(</span><span class="mi">10000</span><span class="p">).</span><span class="nx">move_to</span><span class="p">(</span><span class="nx">x</span><span class="o">=</span><span class="mi">540</span><span class="p">,</span> <span class="nx">y</span><span class="o">=</span><span class="mi">879</span><span class="p">).</span><span class="nx">release</span><span class="p">().</span><span class="nx">perform</span><span class="p">()</span></code>

我们首先分析Server的log,显示的是

<code> 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":{}}]}</code>

我们首先看 controller.js 312

这里Server会解析出一个 gestures的数组里面包含了前面的4个action。

<code><span class="k">if</span> <span class="p">(</span><span class="nx">gestures</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">4</span> <span class="o">&&</span>
      <span class="nx">gestures</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">action</span> <span class="o">===</span> <span class="s1">'press'</span> <span class="o">&&</span>
      <span class="nx">gestures</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">action</span> <span class="o">===</span> <span class="s1">'wait'</span> <span class="o">&&</span>
      <span class="nx">gestures</span><span class="p">[</span><span class="mi">2</span><span class="p">].</span><span class="nx">action</span> <span class="o">===</span> <span class="s1">'moveTo'</span> <span class="o">&&</span>
      <span class="nx">gestures</span><span class="p">[</span><span class="mi">3</span><span class="p">].</span><span class="nx">action</span> <span class="o">===</span> <span class="s1">'release'</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">return</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">mobileSwipe</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">gestures</span><span class="p">);</span>
  <span class="p">}</span></code>

上面这个代码就可以看出 我们的脚本直接转换成了swipe的操作了。下来看看swipe的操作 497行

<code> <span class="kd">var</span> <span class="nx">touchCount</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">touchCount</span> <span class="o">||</span> <span class="mi">1</span>
    <span class="p">,</span> <span class="nx">startX</span> <span class="o">=</span>  <span class="nx">getCoordDefault</span><span class="p">(</span><span class="nx">gestures</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">options</span><span class="p">.</span><span class="nx">x</span><span class="p">)</span>
    <span class="p">,</span> <span class="nx">startY</span> <span class="o">=</span> <span class="nx">getCoordDefault</span><span class="p">(</span><span class="nx">gestures</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">options</span><span class="p">.</span><span class="nx">y</span><span class="p">)</span>
    <span class="p">,</span> <span class="nx">endX</span> <span class="o">=</span> <span class="nx">getCoordDefault</span><span class="p">(</span><span class="nx">gestures</span><span class="p">[</span><span class="mi">2</span><span class="p">].</span><span class="nx">options</span><span class="p">.</span><span class="nx">x</span><span class="p">)</span>
    <span class="p">,</span> <span class="nx">endY</span> <span class="o">=</span> <span class="nx">getCoordDefault</span><span class="p">(</span><span class="nx">gestures</span><span class="p">[</span><span class="mi">2</span><span class="p">].</span><span class="nx">options</span><span class="p">.</span><span class="nx">y</span><span class="p">)</span>
    <span class="p">,</span> <span class="nx">duration</span> <span class="o">=</span> <span class="nx">_getSwipeTouchDuration</span><span class="p">(</span><span class="nx">gestures</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
    <span class="p">,</span> <span class="nx">element</span> <span class="o">=</span> <span class="nx">gestures</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">options</span><span class="p">.</span><span class="nx">element</span>
    <span class="p">,</span> <span class="nx">destElement</span> <span class="o">=</span> <span class="nx">gestures</span><span class="p">[</span><span class="mi">2</span><span class="p">].</span><span class="nx">options</span><span class="p">.</span><span class="nx">element</span> <span class="o">||</span> <span class="nx">gestures</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">options</span><span class="p">.</span><span class="nx">element</span><span class="p">;</span></code>

这里就能够看出操作的是绝对坐标

那再来我们看看第二个。
还是一样 看看Server的输出吧

<code> {"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":{}}]}</code>

因为这个顺序是不满足swipe的所以直接走的是
android-controller.js 中的 865 行 performTouch

这边Server的大体流程是会先将所有的命令,action都解析然后逐个进行操作。

performTouch中会执行下面的parseTouch

<code> <span class="c1">// fix release action then perform all actions</span>
    <span class="nx">fixRelease</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="nx">cb</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
      <span class="k">this</span><span class="p">.</span><span class="nx">parseTouch</span><span class="p">(</span><span class="nx">gestures</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">fixedGestures</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="nx">cb</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
        <span class="nx">async</span><span class="p">.</span><span class="nx">eachSeries</span><span class="p">(</span><span class="nx">fixedGestures</span><span class="p">,</span> <span class="nx">performGesture</span><span class="p">,</span> <span class="nx">cb</span><span class="p">);</span>
      <span class="p">});</span>
    <span class="p">}.</span><span class="nx">bind</span><span class="p">(</span><span class="k">this</span><span class="p">));</span></code>

所以我们还得看下parseTouch做了什么。

这里的代码很长我就不贴出来了,这里还是前面说的解析命令 将每个action的命令解析结果都存储在touchStateObjects这个数组里面。

touchStateObjects.push(touchStateObject);

之后就是遍历这个数组,逐个执行了。

<code> <span class="kd">var</span> <span class="nx">prevPos</span> <span class="o">=</span> <span class="kc">null</span><span class="p">,</span>
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="nx">touchStateObjects</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">state</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">x</span> <span class="o">===</span> <span class="s1">'undefined'</span> <span class="o">&&</span> <span class="k">typeof</span> <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">x</span> <span class="o">===</span> <span class="s1">'undefined'</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// this happens with wait</span>
        <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="nx">prevPos</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span>
        <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">y</span> <span class="o">=</span> <span class="nx">prevPos</span><span class="p">.</span><span class="nx">y</span><span class="p">;</span>
      <span class="p">}</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">offset</span> <span class="o">&&</span> <span class="nx">prevPos</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// the current position is an offset</span>
        <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">x</span> <span class="o">+=</span> <span class="nx">prevPos</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span>
        <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">y</span> <span class="o">+=</span> <span class="nx">prevPos</span><span class="p">.</span><span class="nx">y</span><span class="p">;</span>
      <span class="p">}</span>
      <span class="k">delete</span> <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">offset</span><span class="p">;</span>
      <span class="nx">prevPos</span> <span class="o">=</span> <span class="nx">state</span><span class="p">.</span><span class="nx">options</span><span class="p">;</span>

      <span class="p">....</span>
    <span class="p">});</span></code>

仔细看看就能够知道,首先执行press操作,这个时候的prevPos是为null的 所以x,y 坐标不会改变,接着会执行 prevPos = state.options,所以到下一个moveTo的时候 就会执行到

<code>state.options.x += prevPos.x;
state.options.y += prevPos.y;</code>

所以MoveTo的坐标就变成了x=792.0, y=1758.0
所以就会出现Server的log如下

<code> Returning result: {"value":"Coordinate [x=792.0, y=1758.0] is outside of element rect: [0,0][768,1184]","status":29}</code>

出现越界的情况了。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值