溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-06-01 17:27:23 來源:網絡 閱讀:849 作者:zhukev 欄目:移動開發

出處: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

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

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,這個了解過,初步懷疑不是我應用的問題,最后查得居然是模擬器的問題,重啟后解決問題。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女