UiAutomator 各位是否有遇到过 Android 自定义 ViewGroup 在 UiAutomatorViewer 上的问题

null · 2017年09月21日 · 最后由 思寒_seveniruby 回复于 2017年09月21日 · 1493 次阅读

测试在 5.0 的系统上 dump 出来的 xml 上,自定义 ViewGroup 对应节点的 class 显示为 View,在 6.0/7.0 的系统上 dump 出的 xml 才是 ViewGroup。
同理官方的控件 ToolBar 也有类似的问题。
遇到这样的,如果没有 id 和 text 等元素如何定位到该 View 呢?
@Lihuazhang @seveniruby @DoctorQ

共收到 4 条回复 时间 点赞

没接触过,不过思路可以变通下,在 find 一层加下判断

null #3 · 2017年09月21日 Author

稍微跟了一下源码 UiAutomator 是最后调用到了一个叫做 AccessibilityNodeInfo 的类

在 android-22 中 ViewGroup 是调用父类 View 的 onInitializeAccessibilityNodeInfo 方法,所以获取 className 的时候拿到的是 View.class.getName()

void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:
......
setClassName(CharSequence),
.......

在 android-23 中 ViewGroup 的 onInitializeAccessibilityNodeInfo 方法被 @hide 掉了 看 api 文档已经没有了
而是使用的 getAccessibilityClassName 从而导致了 2 个系统版本的差异

public CharSequence getAccessibilityClassName(){
  return ViewGroup.class.getName()
}
getAccessibilityClassName
added in API level 23
CharSequence getAccessibilityClassName ()
Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

资料来源:
View 的 onInitializeAccessibilityNodeInfo
ViewGroup 的 getAccessibilityClassName
Accessibility 相关

null #2 · 2017年09月21日 Author
恒温 回复

find 层加一下判断?
这个是啥意思,能否进一步说明一下..
他这个 ClassName 从一开始就是错的

自然是万能的 xpath 了

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册