获取 ChildObjects Listview - Appium
Getting ChildObjects Listview - Appium
我很难获得列表视图的子对象。所以,里面有多个对象,可见的和还不可见的。当我询问尺寸时,它 returns 5(这 5 个是可见的)。
我试图将列表视图从底部项目滚动到顶部项目并存储这些项目,但我面临的问题是列表视图中其他部分的项目被识别。我尝试的另一种方法是将最后一个项目向上滚动一点,直到新项目可见。但在这里我面临的问题是,当项目滚动时,整个其他项目的文本被识别。
第一种方法的代码:
在此代码中,我采用列表视图子对象,遍历它们并存储所有文本字段。当我这样做时,其他项目的一些文本字段混合在一起,导致无法使用。
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);
}
这是我试过的第二种方法的代码:
protected void swipeToNextElement(String Element) throws Exception{
列出帐户列表;
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);
}
欢迎任何帮助^^
提前致谢!
当您尝试从 Android 上的长列表中获取项目时,这很痛苦。
如果某些项目是不可见的,则很难获得完整列表,而当其中一些项目与 Appium 相同时,则更难。
所以我的建议是避免测试长列表。
我很难获得列表视图的子对象。所以,里面有多个对象,可见的和还不可见的。当我询问尺寸时,它 returns 5(这 5 个是可见的)。 我试图将列表视图从底部项目滚动到顶部项目并存储这些项目,但我面临的问题是列表视图中其他部分的项目被识别。我尝试的另一种方法是将最后一个项目向上滚动一点,直到新项目可见。但在这里我面临的问题是,当项目滚动时,整个其他项目的文本被识别。
第一种方法的代码: 在此代码中,我采用列表视图子对象,遍历它们并存储所有文本字段。当我这样做时,其他项目的一些文本字段混合在一起,导致无法使用。
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);
}
这是我试过的第二种方法的代码:
protected void swipeToNextElement(String Element) throws Exception{
列出帐户列表;
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);
}
欢迎任何帮助^^
提前致谢!
当您尝试从 Android 上的长列表中获取项目时,这很痛苦。 如果某些项目是不可见的,则很难获得完整列表,而当其中一些项目与 Appium 相同时,则更难。 所以我的建议是避免测试长列表。