关于 appium get_attribute 方法的坑

 

(得要学着看源码)

问题:

self.driver.find_element_by_id("id").get_attribute("content-desc"), 会报NoSuchElement异常,经过验证是get_attribute的问题

问题原因探究:需要学会分析排插定位问题的根源,最关键还是要理解业务、底层代码、调用库实现流程的原理

 

appium server 在 android 原生应用上获取 attribute 的大致流程为:

  1. 从 client 收到获取 attribute 的请求
  2. 把请求转发给在手机上运行的 bootstrap
  3. bootstrap 调用相关方法进行实际操作
  4. bootstrap 返回结果给 appium server
  5. appium server 把结果返回给 client

通过排查各部分的代码发现,错误是在 bootstrap 产生的(排查过程涉及代码有点多,所以这里就不解释了),所以看看 bootstrap 相关源码:

 

其中`getStringAttribute``getBoolAttribute`源码如下:

 

总结:

1、获取 content-desc 的方法为 get_attribute("name") ,而且还不能保证返回的一定是 content-desc (content-desc 为空时会返回 text 属性值)
2、get_attribute 方法不是我们在 uiautomatorviewer 看到的所有属性都能获取的(此处的名称均为使用 get_attribute 时使用的属性名称):

可获取的:

字符串类型:

  • name(返回 content-desc 或 text)
  • text(返回 text)
  • className(返回 class,只有 API=>18 才能支持)
  • resourceId(返回 resource-id,只有 API=>18 才能支持)

布尔类型(如果无特殊说明, get_attribute 里面使用的属性名称和 uiautomatorviewer 里面的一致):

获取不到,但会显示在 uiautomatorviewer 中的属性:

    • index
    • package
    • password
    • bounds(可通过 get_position 来获取其中部分内容)

 

 

posted @ 2019-03-01 11:27  那种意境  阅读(242)  评论(0编辑  收藏  举报