2016-10-26 93 views
0

我想使用Appium和python自動化一個Android應用程序。我有一個列表視圖的屏幕,我想創建一個遍歷列表的函數,並返回列表中的名稱,然後將其與預期列表進行比較(字母順序很重要)。我想將這個函數用於可能具有不同長度列表的屏幕,我是否需要能夠首先確定列表的長度。如何使用python獲取ExpandableListView for Appium中的項目數量?

actual_names = [] 
expected_names = ["Abe", "Bob", "Carl"] 
for num in range(1, 4): 
    xpat = "//android.widget.ExpandableListView[1]/android.widget.FrameLayout[" + str(num) + "]/android.widget.RelativeLayout[1]/android.widget.TextView[1]" 
    text = appium_driver.find_element_by_xpath(xpat).text 
    actual_names.append(text) 

assert expected_names == actual_names 

此代碼的工作原理,但只適用於一個屏幕,只有列表中的項目的確切數量。如果列表中的項目數量發生更改,則失敗。這非常脆弱。我怎樣才能改善這一點並使其更具活力?我使用Python 3和Appium 1.5.3

回答

1
actual_names = [] 
expected_names = ["Abe", "Bob", "Carl"] 
xpat = "//android.widget.ExpandableListView/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.TextView" 
elements = appium_driver.find_elements_by_xpath(xpat) 
for element in elements: 
    text = element.text 
    actual_names.append(text) 

assert expected_names == actual_names 

這裏的區別是,我使用appium_driver.find_elements_by_xpath(),這將收集符合給定條件的所有元素,並把它們作爲列表你看看。

當你想匹配具有相似路徑的多個元素時,xpath語句不應該使用索引號,所以我將它們刪除了。

+0

這工作!非常感謝。我不知道你可以使用沒有索引號的xpaths。 – Cody

相關問題