Appium [content-description] find_element_by_accessibility_id 在 android 中的详解

恒温 · 2014年06月21日 · 最后由 思寒_seveniruby 回复于 2014年06月21日 · 2595 次阅读
本帖已被设为精华帖!

最近 Appium 引入了一个新的 find element 方法:python client 为例

def find_element_by_accessibility_id(self, id):
        """Finds an element by accessibility id.

        :Args:
         - id - a string corresponding to a recursive element search using the
         Id/Name that the native Accessibility options utilize

        :Usage:
            driver.find_element_by_accessibility_id()
        """
        return self.find_element(by=By.ACCESSIBILITY_ID, value=id)

文档里是这样说的:

Allows for elements to be found using the "Accessibility ID". The methods take a
string representing the accessibility id or label attached to a given element, e.g., for iOS the accessibility identifier and for Android the content-description. Adds the methods
driver.find_element_by_accessibility_id and find_elements_by_accessibility_id.

意思就是可以使用 "Accessibility ID" 来定位元素。对于 iOS 而言就是 accessibility identifier。
对于 Android 就是 content-description

那什么是 content-description 呢?

官方是这样说的:

http://developer.android.com/training/accessibility/accessible-app.html
Android has several accessibility-focused features baked into the platform, which make it easy to optimize your application for those with visual or physical disabilities. However, it's not always obvious what the correct optimizations are, or the easiest way to leverage the framework toward this purpose. This lesson shows you how to implement the strategies and platform features that make for a great accessibility-enabled Android application.

这个属性,主要是为了一些有残障的人士准备的,方便他们使用程序,所以这个警告前面会有一个 [Accessbility]。

举个例子,比如你有个 ImageView 放一张图片,这个图片可能包含很多物体和颜色,一些色弱或者色盲的人,他们可能会分不清这个图到底画的什么东西,所以这个时候 contentDescription 就会起作用。比如可能 Android 的一些程序可以用声音告诉使用者这个图片画的是什么,他们读的就是你 contentDescription 的内容。

这其实和 iOS 的 accessibility identifier 是一样的。

给一些需要加 contentDescription 的控件加 contentDescription 是一个良好的编程习惯,事实上 , ADT 的 Lint 检测也会提示你:

[Accessibility] Missing contentDescription attribute on image

所以给你的控件加上 android:contentDescription="@string/desc, 这样也可以让 Appium 来定位。

如何判断是否加了 android:contentDescription?

  • 第一种方法,看控件的 xml 是否加了 android:contentDescription 属性。
  • 第二种,看代码,myView.setContentDescription(desc);
  • 第三种,使用 Hierarchy Viewer

appium sample-code 里面的 android_complex.py 实际上得用 https://github.com/appium/android-apidemos 这个 project 来导出 apk 来测试。

def test_find_elements(self):
    # pause a moment, so xml generation can occur
    sleep(2)

    els = self.driver.find_elements_by_xpath('//android.widget.TextView')
    self.assertEqual('API Demos', els[0].text)

    el = self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "Animat")]')
    self.assertEqual('Animation', el.text)

    el = self.driver.find_element_by_accessibility_id("App")
    el.click()

    els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
    # there are more, but at least 10 visible
    self.assertLess(10, len(els))
    # the list includes 2 before the main visible elements
    self.assertEqual('Action Bar', els[2].text)

    els = self.driver.find_elements_by_xpath('//android.widget.TextView')
    self.assertLess(10, len(els))
    self.assertEqual('Action Bar', els[1].text)

其他

试用了下 genymotion, 非常好用,用来做 Appium 实验很棒。

使用模拟器的时候遇到了个问题:

java.lang.IllegalArgumentException: eglChooseConfig failed EGL_NOT_INITIALIZED

03-29 13:21:34.556: E/AndroidRuntime(4458): FATAL EXCEPTION: main
03-29 13:21:34.556: E/AndroidRuntime(4458): Process: com.example.news, PID: 4458
03-29 13:21:34.556: E/AndroidRuntime(4458): java.lang.IllegalArgumentException: eglChooseConfig failed EGL_NOT_INITIALIZED
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.HardwareRenderer$GlRenderer.chooseEglConfig(HardwareRenderer.java:1173)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.HardwareRenderer$GlRenderer.loadEglConfig(HardwareRenderer.java:1135)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.HardwareRenderer$GlRenderer.initializeEgl(HardwareRenderer.java:1117)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.HardwareRenderer$GlRenderer.initialize(HardwareRenderer.java:1057)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1550)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.Choreographer.doFrame(Choreographer.java:544)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.os.Handler.handleCallback(Handler.java:733)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.os.Handler.dispatchMessage(Handler.java:95)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.os.Looper.loop(Looper.java:136)
03-29 13:21:34.556: E/AndroidRuntime(4458): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 13:21:34.556: E/AndroidRuntime(4458): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 13:21:34.556: E/AndroidRuntime(4458): at java.lang.reflect.Method.invoke(Method.java:515)
03-29 13:21:34.556: E/AndroidRuntime(4458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 13:21:34.556: E/AndroidRuntime(4458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 13:21:34.556: E/AndroidRuntime(4458): at dalvik.system.NativeStart.main(Native Method)

Google 了一下,关联到了 opengl es,这个了解过,初步怀疑不是我应用的问题,最后查得居然是模拟器的问题,重启后解决问题。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 14 条回复 时间 点赞
wing 基于 RN 框架开发的 APP 如何通过 Appium 测试 中提及了此贴 07月16日 16:17

其实直接用 findElementById() 也 OK 的

写的很好, 非常赞。
这个 id,在 android 上有 3 类。

  1. resourceId (API >= 18)
  2. accessibility id (content description)
  3. strings.xml id

在 appium1.2 中会带进来全部的支持

谢谢啊, 太需要了.

学习啦。谢谢!

可不可以获取 content-desc 里面的值 来做 assertion 呢?

#7 楼 @kernel 没找到方法,而且这个 content-desc 前提就是你知道了啊。 你可以换种思路,验证下这个 content-desc 的元素存在与否

#8 楼 @lihuazhang 是的,已经这样做了。

真的是太赞了,又帮我解决了一个问题

#8 楼 @lihuazhang 是不是必须 4.4.2 系统以上?

恒温 #13 · 2015年05月19日 Author

顶贴!

#13 楼 @lihuazhang 我现在有个元素 class:android.view.View,对于的 content-desc:凡人修仙传第 1 集主播,这个我可以验证 conetne-desc 的值吗?我的写法是 viewfields = self.driver.find_elements_by_class_name("android.view.View")
text=viewfields[1].get_attribute("content-desc")
self.assertEqual(u'凡人修仙传第 1 集主播', text),报 text=viewfields[1].get_attribute("content-desc") 错误,View 对象没有这个方法吗

看看,学习下~~~~~

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