为了账号安全,请及时绑定邮箱和手机立即绑定

初探UiAutomator2.0中使用Xpath定位元素

标签:
Java

题外话

最近更新有点延迟哈,那是因为接了一个外包项目的活(就是移动端自动化相关的),忙的“外黑里焦”的,好在应该2个星期的努力已经进入尾期,项目整体功能都已经实现,后面有空给大家分享,今天的主题是讲一下在使用过程中遇到的一个问题,如何在UiAutomator2.0中使用Xpath定位元素?

背景

现在的app在打包成apk的时候都是有加固处理的,各种混淆加固,所以已经破坏了或扰乱了原本的代码变量命名形式,这就给我们要基于界面来做自动化测试带来了灾难性的阻碍,因为那些混淆过的id是不固定的,下一次再出个新版本,这一切都变了,所以这就没办法用id来定位混淆过的app元素,那还有什么好的方法吗?还记得Web自动化测试中神乎其技的xpath吗?不管什么元素都可以用它定位出来,所以我就想在UiAutomator2.0中也使用它来定位混淆的app元素,这要如何操作?UiAutomator2.0的API中并没有给出xpath这种方式,那我们只能自己去写一个了。

思路

参考UI Automator Viewer中抓取到的结构层次,不能用resource-id,又要体现出层次关系,那就只能是class属性了,这里的class可以对应web xpath中的标签,使用业界统一的斜杠/来保持层次,那么最原始状态下的xpath大概就是这个样子了:

android.view.ViewGroup/android.widget.ImageView
再加上下标
android.view.ViewGroup[2]/android.widget.ImageView[0]


webp


xpath的格式定义出来了之后,我们就开始一层一层去遍历,很简单通过斜杠/来分隔出一个class数组,然后依次去查找这些class对应的元素,通过父子关系拼接起来,直到最后一个class,存在就返回对应的对象,不存在就返回null。
由于时间关系,这一次就是初探,只实现了这种绝对路径(/)下的定位,其实要想完整完成这个功能,还需要支持相对路径(//)的定位,以及各种属性的组合定位,其实基于这个版本上面改改也不远了,这就留给有兴趣的童鞋去完成吧。


实现

1、首先要实现根据class或其他属性去找到某个元素的子元素,我这里实现了支持传入各种参数,代码如下:

public static UiObject2 getChild(Object root, Map<String,String> params) {        if (params == null || !params.containsKey("class")) {
            log.e("[Error]参数错误: 为空或未包含[class]key");            return null;
        }        String clazz = params.get("class");        String className = clazz;
        int index = 0;        if (clazz.endsWith("]") && clazz.contains("[")) { //有下标
            className = clazz.substring(0, clazz.lastIndexOf("["));            String num = clazz.substring(clazz.lastIndexOf("[") + 1, clazz.lastIndexOf("]"));
            index = num != null && !"".equals(num) ? Integer.parseInt(num) : index;
        }
        List<UiObject2> childList = null;        if (root instanceof UiObject2) {
            childList = ((UiObject2) root).getChildren();
        } else {
            childList = hasObjects(By.clazz(className)) ? mDevice.findObjects(By.clazz(className)) : null;
        }
        List<UiObject2> tempList = new ArrayList<UiObject2>();        if (childList != null && !childList.isEmpty()) {            for (UiObject2 child : childList) {
                boolean isMatch = child.getClassName().equals(className);                if (params.containsKey("pkg")) {
                    isMatch = isMatch && child.getApplicationPackage().equals(params.get("pkg"));
                }                if (params.containsKey("text")) {
                    isMatch = isMatch && child.getText().equals(params.get("text"));
                }                if (params.containsKey("desc")) {
                    isMatch = isMatch && child.getContentDescription().equals(params.get("desc"));
                }                if (isMatch) {
                    tempList.add(child);
                }
            }
        }        if(tempList.isEmpty()) {            return null;
        }        if (index >= tempList.size()) {
            log.e(String.format("[Error]查找class[%s] 下标[%d]越界[%d]", clazz, index, tempList.size()));            return null;
        }        return tempList.get(index);
    }

2、再写一个通过class获取子元素的简单实现,因为这种方式用的多:

  public static UiObject2 getChild(Object root, String clazz) {        Map<String,String> params = new HashMap<String,String>();
        params.put("class", clazz);        return getChild(root, params);
    }

3、加入解析xpath表达式的部分,将解析和查找整个过程连起来:

public static UiObject2 findObjectByXpath(UiObject2 root, String xpath) {        if (xpath == null && "".equals(xpath)) {
            log.e("[Error]xpath expression[" + xpath + "] is invalid");            return null;
        }        String[] xpaths = null;        if (xpath.contains("/")) {
            xpaths = xpath.split("/");
        } else {
            xpaths = new String[]{xpath};
        }
        UiObject2 preNode = root;        for (String path : xpaths) {
            preNode = getChild(preNode, path);            if (preNode == null) {                //log.e(String.format("按xpath[%s]查找元素失败, 未找到class[%s]对应的节点", xpath, path));
                break;
            }
        }        return preNode;
    }

4、使用演示:

String commentXpath = "android.widget.LinearLayout/android.widget.LinearLayout/android.widget.TextView[0]";
UiObject2 commentView = findObjectByXpath(root, commentXpath);



作者:测试开发栈
链接:https://www.jianshu.com/p/c2cfdd05bf35


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消