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

J 今天的主题是讲一下在使用过程中遇到的一个问题,如何在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]

  

  xpath的格式定义出来了之后,我们就开始一层一层去遍历,很简单通过斜杠/来分隔出一个class数组,然后依次去查找这些class对应的元素,通过父子关系拼接起来,直到最后一个class,存在就返回对应的对象,不存在就返回null。

  由于时间关系,这一次就是初探,只实现了这种绝对路径(/)下的定位,其实要想完整完成这个功能,还需要支持相对路径(//)的定位,以及各种属性的组合定位,其实基于这个版本上面改改也不远了,这就留给有兴趣的童鞋去完成吧。

  实现

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

  public static UiObject2 getChild(Object root, Mapparams) { 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;

  }

  ListchildList = null; if (root instanceof UiObject2) {

  childList = ((UiObject2) root).getChildren();

  } else {

  childList = hasObjects(By.clazz(className)) ? mDevice.findObjects(By.clazz(className)) : null;

  }

  ListtempList = new ArrayList(); 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) { Mapparams = new HashMap();

  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);

猜你喜欢

转载自www.cnblogs.com/qfjavabd/p/10396683.html
今日推荐