返回boolean类型的值脚本报错
请大家帮我看看这段脚本问题出在哪里呢?
public boolean checkSearchEndty(String keyword, String SearchBy) throws InterruptedException {
Library.menuBar(driver, "entity");
//select searchBy and input the keyword, click Search
new Select(driver.findElement(By.id("searchfield"))).selectByVisibleText(SearchBy);
lib.waitForElementDisplayed(driver, 3, By.id("view"));
driver.findElement(By.id("searchphrase")).sendKeys(keyword);
driver.findElement(By.linkText("Search")).click();
Thread.sleep(5000);
//verify search result found, if no search result found then stop
boolean isDisplayed = lib.waitForElementDisplayed(driver, 20, By.id("ListTable"));
boolean searchResult = false;
if (isDisplayed == false) {
searchResult = true;
driver.close();
return searchResult;
}
//verify search result found, if the search results are found then check the result
else {
//get the td number of SearchBy in the search result table,
List<WebElement> ths = driver.findElements(By.xpath("//table[@id='ListTable']/tbody/tr/th"));
int totalCol = ths.size();
System.out.println("totalCol=" + totalCol);
int tdNum = 1;
for (int i = 1; i < totalCol; i++) {
if (ths.get(i).getText().trim().equals(SearchBy)) {
tdNum = i+1;
break;
}
else {
}
}
//based on the td number, verify each result in the select td for each row.
List<WebElement> trs = driver.findElements(By.xpath("//table[@id='ListTable']/tbody/tr"));
int totalRow = trs.size();
for (int i = 2; i <= totalRow; i++) {
String actualText = trs.get(i).findElement(By.xpath("td["+tdNum+"]")).getText();
searchResult = Library.assertWaitForTrue(2, actualText.equals(keyword));
if (searchResult == false) {
break;
}
}
}
return searchResult;
}
}
报错
i <= totalRow这个是不是会造成越界? 原因分析:Java.lang.IndexOutOfBoundException为数组下标越界的错误
解决: int totalCol = ths.size(); int totalRow = trs.size();两个取值后循环中不是从0开始:for (int i = 1; i < totalCol; i++)for (int i = 2; i <= totalRow; i++)
估计是这里越界,建议楼主把totalCol, totalRow两个值打印出来看看,是不是这里的问题 joykao 发表于 2015-7-15 09:39
i
是的,这里错了
for (int i = 0; i < totalRow; i++) jingzizx 发表于 2015-7-15 10:34
原因分析:Java.lang.IndexOutOfBoundException为数组下标越界的错误
解决: int totalCol = ths.size(); in ...
打印出来就查到问题了
页:
[1]