索引越界异常 - 比较两 (2) 个字符串列表时
Index out of Bounds Exception - when comparing two(2) lists of Strings
我正在比较两个字符串列表,它们比较成功,但之后,我得到一个 -
java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at java.util.Collections$UnmodifiableList.get(Collections.java:1309)
at com.cucumber.CucumberWebGui.StepDefinitions.tableHeaders(StepDefinitions.java:254)
at ✽.Then table with id "content" has header values of(tableHeader.feature:9)
我从黄瓜特征文件中传入的第一个列表。我从本网站的 table headers 收集的第二个 - “http://toolsqa.com/automation-practice-table/”
我试过更改 for 循环,但没有用。我已经在 Stack Overflow 上阅读了其他人的相同问题,但我无法解决。
我不知道该怎么办。
这是代码和功能文件-
代码-
@SuppressWarnings("deprecation")
@Then("^table with id \"([^\"]*)\" has header values of$")
public void tableHeaders(String id, DataTable table) {
java.util.List<java.util.List<String>> expectedHeaders = table.raw();
WebElement container = driver.findElement(By.id(id));
List<WebElement> allHeaders = container.findElements(By.tagName("th"));
List<String> actualHeaders = new ArrayList<String>();
for (WebElement header : allHeaders) {
actualHeaders.add(header.getText());
}
for (int i = 0; i < actualHeaders.size(); i++) {
Assert.assertEquals(expectedHeaders.get(i).get(0), actualHeaders.get(i));
}
}
专题文件 -
Scenario: Test Table Header assertion
Then table with id "content" has header values of
| Structure |
| Country |
| City |
| Height |
| Built |
| Rank |
| … |
可能是因为 expectedHeaders
的元素少于 actualHeaders
。
我正在比较两个字符串列表,它们比较成功,但之后,我得到一个 -
java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at java.util.Collections$UnmodifiableList.get(Collections.java:1309)
at com.cucumber.CucumberWebGui.StepDefinitions.tableHeaders(StepDefinitions.java:254)
at ✽.Then table with id "content" has header values of(tableHeader.feature:9)
我从黄瓜特征文件中传入的第一个列表。我从本网站的 table headers 收集的第二个 - “http://toolsqa.com/automation-practice-table/”
我试过更改 for 循环,但没有用。我已经在 Stack Overflow 上阅读了其他人的相同问题,但我无法解决。 我不知道该怎么办。
这是代码和功能文件-
代码-
@SuppressWarnings("deprecation")
@Then("^table with id \"([^\"]*)\" has header values of$")
public void tableHeaders(String id, DataTable table) {
java.util.List<java.util.List<String>> expectedHeaders = table.raw();
WebElement container = driver.findElement(By.id(id));
List<WebElement> allHeaders = container.findElements(By.tagName("th"));
List<String> actualHeaders = new ArrayList<String>();
for (WebElement header : allHeaders) {
actualHeaders.add(header.getText());
}
for (int i = 0; i < actualHeaders.size(); i++) {
Assert.assertEquals(expectedHeaders.get(i).get(0), actualHeaders.get(i));
}
}
专题文件 -
Scenario: Test Table Header assertion
Then table with id "content" has header values of
| Structure |
| Country |
| City |
| Height |
| Built |
| Rank |
| … |
可能是因为 expectedHeaders
的元素少于 actualHeaders
。