2015-11-02 42 views
1

我很难获取listview的childobjects。所以,它有多个对象,可见且不可见。当我询问尺寸时,它返回5(这5个是可见的)。 我试图将列表视图从底部项目滚动到顶部项目并存储这些项目,但比我面对的问题是来自列表视图中其他部分的项目被识别。我尝试的另一种方法是将最后一项滚动一点,直到新项目变为可见。但是在这里,我面临的问题是滚动项目时会识别整个其他项目文本。获取ChildObjects列表视图 - Appium

第一个方法的代码: 在此代码,我采取列表视图childObjects,遍历它们并存储所有文本框。当我这样做时,其他项目的某些文本字段会混合使其无法使用。

List AccountList; 

ArrayList<String> AccountArray = new ArrayList<String>(); 
String tempAccountInfo = ""; 
String AccountType=""; 
String LastItem = ""; 
String NewLastItem = "N/A"; 
List<WebElement> AccountListItems; 

AccountList = driver.findElements(getObject("ElementsWithinAccountListView")); 
AccountListItems = AccountList.get(AccountList.size() - 1).findElements(By.className("android.widget.TextView")); 

while (!NewLastItem.equals(LastItem)) { 
    for (int i=0; i<AccountList.size();i++){ 
     AccountList = driver.findElements(getObject("ElementsWithinAccountListView")); 
     AccountListItems = AccountList.get(i).findElements(By.className("android.widget.TextView")); 
     switch (AccountList.get(i).getAttribute("className")){ 
      case "android.widget.LinearLayout": 

        if (!AccountListItems.get(0).getText().contains("New ")){ 
         switch(AccountListItems.get(0).getText()){ 
         case "Current accounts": 
          AccountType = "Current accounts"; 
          break; 
         case "Savings accounts": 
          AccountType = "Saving accounts"; 
          break; 
         case "Investments": 
          AccountType = "Investments"; 
          break; 
         case "Credit cards": 
          AccountType = "Credit cards"; 
          break; 
         case "Other": 
          AccountType = "Other"; 
          break; 
         } 
        } 

       break; 
      case "android.widget.RelativeLayout": 
       AccountListItems = AccountList.get(i).findElements(By.className("android.widget.TextView")); 
       if (AccountListItems.size() == 5 || AccountListItems.size() == 4){ 
        tempAccountInfo = AccountType; 
        for(int j=0; j<AccountListItems.size(); j++){ 

         tempAccountInfo = tempAccountInfo + "|" + AccountListItems.get(j).getText(); 
         LastItem = AccountListItems.get(2).getText(); 

        } 

        switch(AccountType){ 
        case "Investments": 
         if (!(AccountListItems.size() == 5)) { 
          System.out.println(tempAccountInfo); 
          AccountArray.add(tempAccountInfo); 
         } 
         break; 
        default: 
         System.out.println(tempAccountInfo); 
         AccountArray.add(tempAccountInfo); 
         break; 
        } 
        tempAccountInfo = ""; 
       } 
       break; 
     } 
    } 

    driver.swipe(AccountList.get(AccountList.size()-1).getLocation().x + 70, AccountList.get(AccountList.size()-1).getLocation().y, AccountList.get(1).getLocation().x + 70, AccountList.get(1).getLocation().y, 3000); 
    Thread.sleep(20000); 

    AccountListItems = AccountList.get(AccountList.size() - 1).findElements(By.className("android.widget.TextView")); 
    try { 
     NewLastItem = AccountListItems.get(2).getText(); 
    } catch (Exception e){ 
     System.out.println("Not at the end of the listview"); 
     NewLastItem = "N/A"; 
    } 
    System.out.println("LastItem:" + LastItem); 
    System.out.println("NewLastItem:" + NewLastItem); 
} 

Objects viewed in Appium

这是我已经尝试了第二种方法的代码:

protected void swipeToNextElement(String Element) throws Exception{ 

列表为accountList;

String LastItem; 
String NewLastItem; 
List<WebElement> AccountListItems; 
Boolean loop = true; 


AccountList = driver.findElements(getObject(Element)); 
AccountListItems = AccountList.get(AccountList.size()-1).findElements(By.className("android.widget.TextView")); 

LastItem = AccountListItems.get(0).getAttribute("text"); 
NewLastItem = AccountListItems.get(0).getAttribute("text"); 
driver.swipe(AccountList.get(AccountList.size()-1).getLocation().x + (AccountList.get(AccountList.size()-1).getSize().width/2), AccountList.get(AccountList.size()-1).getLocation().y, AccountList.get(AccountList.size()-1).getLocation().x + (AccountList.get(AccountList.size()-1).getSize().width/2), AccountList.get(AccountList.size()-1).getLocation().y - 70, 1000); 

while (loop) { 


    if (NewLastItem.equals(LastItem)){ 
     AccountList = driver.findElements(getObject(Element)); 
     AccountListItems = AccountList.get(AccountList.size()-1).findElements(By.className("android.widget.TextView")); 
     driver.swipe(AccountList.get(AccountList.size()-1).getLocation().x + (AccountList.get(AccountList.size()-1).getSize().width/2), AccountList.get(AccountList.size()-1).getLocation().y, AccountList.get(AccountList.size()-1).getLocation().x + (AccountList.get(AccountList.size()-1).getSize().width/2), AccountList.get(AccountList.size()-1).getLocation().y - 70, 1000); 
     NewLastItem = AccountListItems.get(0).getAttribute("text"); 
    } else { 
     AccountList = driver.findElements(getObject(Element)); 
     AccountListItems = AccountList.get(AccountList.size()-1).findElements(By.className("android.widget.TextView")); 
     driver.swipe(AccountList.get(AccountList.size()-1).getLocation().x + (AccountList.get(AccountList.size()-1).getSize().width/2), AccountList.get(AccountList.size()-1).getLocation().y, AccountList.get(AccountList.size()-1).getLocation().x + (AccountList.get(AccountList.size()-1).getSize().width/2), AccountList.get(AccountList.size()-1).getLocation().y + 70, 1000); 
     NewLastItem = AccountListItems.get(0).getAttribute("text"); 
     loop = false; 
    } 
} 

System.out.println("NewLastItem: " + NewLastItem); 

}

任何帮助是值得欢迎的^^

提前感谢!

回答

0

当您尝试从Android上的长列表中获取项目时,这很痛苦。 如果某些项目不可见,并且当它们中的一些与Appium相同时更难以获得完整列表。 所以我的建议是避免测试一个长列表。

+0

是的,但我们真的需要它:) –

+0

我会建议,一旦你实现了一个通用的代码来处理不同的列表视图 - 请在github上分享它。我会帮助很多人。 :) – Vaibhav