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去定位控件。

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章