您的位置:首页 > 移动开发 > Android开发

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

2014-10-01 18:35 771 查看
出处:http://testerhome.com/topics/1034

最近 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,这个了解过,初步怀疑不是我应用的问题,最后查得居然是模拟器的问题,重启后解决问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐