appium簡明教程(10)——控件定位基礎


狹義上講,UI級的自動化測試就是讓機器代替人去點來點去的過程。

但機器去點什么(點上面還是點左邊),怎么點(是長按還是輕觸),這些東西是必須由代碼的編寫者所指示清楚的。

控件定位就是解決機器點什么的問題的。

一般說來,我們可以這樣告訴機器:去點登陸按鈕

機器很笨,它並不知道什么是登陸按鈕。因為登陸按鈕是自然語言的描述。

如果你讓一個人去點登陸按鈕,那么他其實也是要經過一系列的腦補以后才可以做這件事的。

這個腦補的過程還原如下:

 

這個一定是個按鈕

這個按鈕一定在被測的應用上

這個按鈕大概上面有登陸這個文字信息

嗯,還真有一個,那么點吧。

 

這就是人探索性測試的一個簡單過程。一般來說,如果你給出的信息不太充分,人類還是可以通過一系列的探索性思維去理解你的描述的。這個屬於心理學的問題,不展開解釋。

但是機器並不是人,如果你給出的描述不精確的話,機器是不會自發性的進行探索和腦補的。

因此控件定位就是精確的描述控件特征並告訴機器的過程。

本文版權歸乙醇所有,歡迎轉載,但請注明作者與出處,嚴禁用於任何商業用途

控件的特征就是控件的屬性,我們可以通過上一講中的uiautomatorviewer去獲取。

在appium的定位世界里,下面這些方法是可以為我們使用的。也就是說,我們通過下面幾個約定好的方式,按照webdriver和appium的DSL(自行搜索並理解)進行控件特征的描述和定位。

 

繼承自webdriver的方法,也就是通過這3個特征可以定位控件

  • find by "class" (i.e., ui component type,andorid上可以是android.widget.TextView)
  • find by "xpath" (i.e., an abstract representation of a path to an element, with certain constraints,由於appium的xpath庫不完備的原因,這個不太推薦)
  • find by "id"(android上是控件的resource id)

Mobile JSON Wire Protocol 協議中定義的方法,更適合移動設備上的控件定位

  • -ios uiautomation: a string corresponding to a recursive element search using the UIAutomation library (iOS-only)
  • -android uiautomator: a string corresponding to a recursive element search using the UiAutomator Api (Android-only)
  • accessibility id: a string corresponding to a recursive element search using the Id/Name that the native Accessibility options utilize.

在appium 的client對Mobile JSON Wire Protocol中定義的方法進行了封裝,使其調用起來更加方便

 

ruby篇

find_element :accessibility_id, 'Animation'
find_elements :accessibility_id, 'Animation'
find_element :uiautomator, 'new UiSelector().clickable(true)'
find_elements :uiautomator, 'new UiSelector().clickable(true)'

當然了,你也可以使用原生的webdriver方法

find_element id: 'resource_id'

另外,ruby lib里提供了一些非常好用的簡便方法來進行控件的定位,好寫,好讀。

  • text(value_or_index) :Find the first TextView that contains value or by index. If int then the TextView at that index is returned.
  • button(value_or_index):Find the first button that contains value or by index. If int then the button at that index is returned

 

更多請看這里

 

python篇

el = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
self.assertIsNotNone(el)
els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
self.assertIsInstance(els, list)

el = self.driver.find_element_by_accessibility_id('Animation')
self.assertIsNotNone(el)
els = self.driver.find_elements_by_accessibility_id('Animation')
self.assertIsInstance(els, list)

總的來說就是在driver里增加了

  • find_element_by_accessibility_id
  • find_elements_by_accessibility_id
  • find_element_by_android_uiautomator
  • find_element_by_android_uiautomator

等方法

 

java篇

前面也講過了,新增了這些方法

findElementByAccessibilityId()
findElementsByAccessibilityId()
findElementByIosUIAutomation()
findElementsByIosUIAutomation()
findElementByAndroidUIAutomator()
findElementsByAndroidUIAutomator()

 

討論:從上面可以看出來,python 和 java client對移動端控件定位的封裝是比較初級的。ruby lib中封裝了很多方便和簡潔的方法,因此可以看出,使用ruby lib是優於python和java的選擇。當然,如果忽略性能的話。

 

下一節我們開始具體看下如何用resource id去定位控件。

本文版權歸乙醇所有,歡迎轉載,但請注明作者與出處,嚴禁用於任何商業用途


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM