-1

I am comparing two List of Strings, which finish comparing successfully, but then after, I get a -

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)

The first list I pass in from a cucumber feature file. The second I collect from the table headers at this website - "http://toolsqa.com/automation-practice-table/"

I have tried changing the for loop, but it doesn't help. I have read other people's same issue on Stack Overflow, but I cannot solve it. I don't know what to do.

Here is the code and feature file -

Code -

@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));
    }
}

Feature File -

Scenario: Test Table Header assertion 
Then table with id "content" has header values of 

    | Structure |
    | Country | 
    | City |
    | Height | 
    | Built | 
    | Rank |
    | … |
snikt
  • 581
  • 4
  • 11
  • 27

1 Answers1

1

Probably because expectedHeaders has less elements than actualHeaders.

Turing85
  • 18,217
  • 7
  • 33
  • 58
SF23
  • 174
  • 1
  • 12
  • If you inspect the page that contains the table, the count of tags is 7. My table in the feature file is also 7 – snikt Nov 05 '17 at 17:11
  • But you access index 7 which is the 8th element because indexes are null based. – SF23 Nov 05 '17 at 17:15