您的位置:首页 > 产品设计 > UI/UE

xiaocong的uiautomator有时候找不到界面元素的问题和解决

2016-05-13 00:29 429 查看

问题

最近一直在用xiaocong的uiautomator做android的自动化测试,这是一个python对android uiautomator的包装,非常好用。

不过最近经常出现一个问题,界面上明明有这个元素,但是xiaocong的uiautomator却无法发现,无法点击。

解决

经过一番折腾,我猜测解决问题的核心大概在于setCompressedLayoutHeirarchy方法。

setCompressedLayoutHeirarchy是android uiautomator的一个方法,在官网的介绍中这个方法:

public void setCompressedLayoutHeirarchy (boolean compressed)

Enables or disables layout hierarchy compression. If compression is enabled, the layout hierarchy derived from the Acessibility framework will only contain nodes that are important for uiautomator testing. Any unnecessary surrounding layout nodes that make viewing and searching the hierarchy inefficient are removed.

Parameters
compressed  true to enable compression; else, false to disable


大意是说如果启用压缩,界面层只会返回对UIAutomator测试重要的元素。Any unnecessary surrounding layout nodes that make viewing and searching the hierarchy inefficient are removed.

但是问题就是,有时候代码判定无用的元素,实际上却是有用的。

xiaocong的uiautomator提供了一个dump方法,对应到android uiautomator的方法是dumpWindowHierarchy,用于将界面提取成xml数据。这个dump方法可以设置compressed参数。

我在测试中验证过,设置了compressed=True的时候,一些元素确实就从xml数据中消失了,设置compressed=False时又出现——与此同时,这些元素也变得可被找到和点击。

查看了xiaocong的uiautomator jsonrpcserver端代码,可以确定,在做dump操作时,它会同时调用setCompressedLayoutHeirarchy方法,并且在dump结束时不会将compressed恢复为调用之前状态。

也就是说,虽然xiaocong的uiautomator没有暴露setCompressedLayoutHeirarchy方法给我,但是我可以调用一下dump(compressed=False)来间接的调用setCompressedLayoutHeirarchy(compressed=False)。

增加dump之后,后面的测试都很顺利,再也没出现看着界面大眼瞪小眼的情况了。

参考:

http://developer.android.com/reference/android/support/test/uiautomator/UiDevice.html https://github.com/xiaocong/android-uiautomator-jsonrpcserver https://github.com/xiaocong/uiautomator
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息