[英]String Array Method not Returning the Array Object - Selenium WebDriver
請查找我的代碼,它不返回Array對象的值,它只返回一個數組對象
public String[] verify_userRole(String[] Expected_role) {
String[] actual_role = new String[4];
String first;
WebElement role_table = driver.findElement(By
.xpath("//*[@id='tblListView']/tbody[1]"));
List<WebElement> allRows = role_table.findElements(By.tagName("tr"));
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
first = cell.getText().toString();
actual_role = new String[] {first};
}
}
return actual_role;
}
變量首先包含四個值(“name”,“name1”,“name2”,“name3”)后將此字符串值轉換為數組(actual_role)然后只返回一個值(“name”)
請說明上述代碼的問題是什么
您在循環中的每個步驟重新初始化字符串數組。
你應該只做一次。
ArrayList<String> actual_role = new ArrayList<String>( )
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
first = cell.getText().toString();
actual_role.add(first);
}
}
return (String[]) actual_role.toArray( new String[ actual_role.size() ] );
順便說一下,我已經將您的示例轉換為使用中間ArrayList,因為您不知道實際的數據大小,並且它很容易在運行時重新初始化數組。
如果您正在實現的方法的簽名不是由外部框架決定的,我建議您使用List<String>
作為返回類型而不是String[]
。
你總是在循環中實例化你的數組:
actual_role = new String[] {first};
試着改為:
actual_role[i] = first;
當我是當前指數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.