2015-11-02 70 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