appium 利用相对坐标解锁九宫格

今天,碰到了一个九宫格解锁的问题,查了资料,找到了一篇相关文章:http://www.cnblogs.com/tobecrazy/p/4881390.html,但是下图并不能获取某个空格的element,所以失效。。。于是就想到用坐标的方法解决

《appium 利用相对坐标解锁九宫格》

一、获取坐标位置

我用的是hm2a:手机设置–其他高级设置–开发者选项–开启指针位置

二、编程实现

利用TouchAction

final TouchAction gesture = new TouchAction(driver).press(172, 598).waitAction(1000)
				.moveTo(360, 598).moveTo(360, 770).moveTo(360, 942).moveTo(548, 942).release();
		gesture.perform();

运行后,悲剧地发现,报异常:坐标偏离了元素区域;查看log,发现appium server的node.js实现的是state.options.x +=prevPos.x和state.options.y +=prevPos.y,所以用绝对坐标不行,于是想到用相对坐标(即:偏移量)来实现:

final TouchAction gesture = new TouchAction(driver).press(172, 598).waitAction(1000)
				.moveTo(188, 0).moveTo(0, 172).moveTo(0, 172).moveTo(188, 0).release();
		gesture.perform();

终于大功告成,现在来看一下该手势的实现过程

info: [debug] Pushing command to appium work queue: ["element:touchDown",{"x":17
2,"y":598}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action"
:"element:touchDown","params":{"x":172,"y":598}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: touchDown
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][720,1280]
info: [debug] [BOOTSTRAP] [debug] Performing TouchDown using element? false x: 1
72, y: 598
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Pushing command to appium work queue: ["element:touchMove",{"x":36
0,"y":598}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action"
:"element:touchMove","params":{"x":360,"y":598}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: touchMove
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][720,1280]
info: [debug] [BOOTSTRAP] [debug] Performing TouchMove using element? false x: 3
60, y: 598
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Pushing command to appium work queue: ["element:touchMove",{"x":36
0,"y":770}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action"
:"element:touchMove","params":{"x":360,"y":770}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: touchMove
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][720,1280]
info: [debug] [BOOTSTRAP] [debug] Performing TouchMove using element? false x: 3
60, y: 770
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Pushing command to appium work queue: ["element:touchMove",{"x":36
0,"y":942}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action"
:"element:touchMove","params":{"x":360,"y":942}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: touchMove
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][720,1280]
info: [debug] [BOOTSTRAP] [debug] Performing TouchMove using element? false x: 3
60, y: 942
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Pushing command to appium work queue: ["element:touchMove",{"x":54
8,"y":942}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action"
:"element:touchMove","params":{"x":548,"y":942}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: touchMove
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][720,1280]
info: [debug] [BOOTSTRAP] [debug] Performing TouchMove using element? false x: 5
48, y: 942
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}

三、后记

九宫格坐标解锁核心是利用坐标的偏移量,但是不同手机的屏幕分辨率不一样,所以此方法不是很可取;如果可以定位空格的元素,推荐用文章刚开始的那个链接的方法,有兴趣的可以自己实践一下~~

    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/fgwvip123/article/details/51499257
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞