获取ChildObjects Listview - Appium

时间:2015-11-02 07:18:35

标签: android listview android-listview appium

我很难获得listview的childobjects。因此,其中有多个对象,可见且尚未可见。当我要求大小时,它返回5(这5个是可见的)。 我尝试将列表视图从底部项目滚动到顶部项目并存储这些项目,但我遇到的问题是列表视图中其他部分的项目被识别。我尝试的另一种方法是稍微向上滚动最后一项,直到新项目变得可见。但在这里,我面临的问题是,当滚动项目时,会识别整个其他项目文本。

第一种方法的代码: 在这段代码中,我使用listviews 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);

}

欢迎任何帮助^^

提前致谢!

1 个答案:

答案 0 :(得分:0)

当您尝试从Android上的长列表中获取项目时会感到很痛苦。 如果某些项目看不见,那么很难获得完整列表,而当其中一些项目与Appium相同时甚至更难。 所以我的建议是避免测试一长串清单。