appium源码分析(十一)-getSize,getLocation
来源:互联网 发布:网络证据保全公证 编辑:程序博客网 时间:2024/05/16 06:48
摘要
为什么需要将这两个放到一块儿来讲呢,因为他们两实际上都是依靠UiAutomator的Bounds属性值的,并且如果说我需要获取到元素的中点坐标的话,我就需要依赖这两个方法才能够解决
正文
好的 我们先来看看geiSize吧,从字面的意思就是获取到它的大小,即元素的宽高了。
public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { if (command.isElementCommand()) { // Only makes sense on an element final JSONObject res = new JSONObject(); try { final AndroidElement el = command.getElement(); final Rect rect = el.getBounds(); res.put("width", rect.width()); res.put("height", rect.height()); } catch (final UiObjectNotFoundException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final Exception e) { // handle NullPointerException return getErrorResult("Unknown error"); } return getSuccessResult(res); } else { return getErrorResult("Unable to get text without an element."); } }
现在看这些代码实际上都很简单了,还是一样 获取到AndroidElement的元素
再来获取到bounds。这个是Rect的类型。再来分别将其的长宽分别放到一个JsonObject对象里面。将该对象返回
下来看看getLocation
public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { if (!command.isElementCommand()) { return getErrorResult("Unable to get location without an element."); } try { final JSONObject res = new JSONObject(); final AndroidElement el = command.getElement(); final Rect bounds = el.getBounds(); res.put("x", bounds.left); res.put("y", bounds.top); return getSuccessResult(res); } catch (final Exception e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } }
这里是将Bounds的坐顶点的X坐标以及Y坐标分别赋值到JsonObject的x,y上
所以如果说你想要获取到某个元素在屏幕的中点坐标的话,你就需要这两个方法的帮助了。
element = self.find_element(loc) startX = int(element.location['x']) startY = int(element.location['y']) endX = int(element.size['width'])+startX endY = int(element.size['height'])+startY centerX = (startX+endX)/2 centerY = (startY+endY)/2
以上就能够获取到元素中点的坐标了。
0 0
- appium源码分析(十一)-getSize,getLocation
- bootstrap之GetName&&GetAttribute&&GetDeviceSize&&GetSize&&GetLocation&&GetDataDir
- Appium源码分析(四)-swipe
- Appium源码分析(五)-drag
- Appium源码分析(七)-click
- Appium源码分析(六)-find(上)
- appium源码分析(六)-find(下)
- appium源码分析(八)-touch
- appium源码分析(九)-getText,setText
- appium源码分析(十)-GetAttribute
- appium 源码分析(十二)-pressKeyCode,LongPressKeyCode
- appium源码分析(十三)-UpdateStrings
- Appium Server 源码分析(一)
- appium Bootstrap UiSelectorParser源码分析
- Caffe源码(十一):io.cpp 分析
- spark源码学习(十一)---cacheManager分析
- Caffe源码(十一):io.cpp 分析
- 20muduo_base库源码分析(十一)
- C++泛型算法
- CSS中 zoom:1 的作用
- 使用Xcode上传代码至Github
- 数据结构之线性表
- instancetype和id的异同
- appium源码分析(十一)-getSize,getLocation
- 关于string::size_type
- Eclipse 不能finish android工程
- Divide Two Integers C#
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- mybatis快速入门
- Linux下定时执行脚本/crontab
- Codeforces 508D Tanya and Password
- C++ Dynamic_cast