2016-10-26 50 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

相关问题