hirosht
hirosht

Reputation: 972

Verify the table column order from selenium

Requirement is to verify the column order of the following table is in the correct order from its column header names.

Therefore, I have written down a method to cover up my requirement, which accepts an array list [String] of the table header names in the required order.

But it apparently does not do my requirement through the soft-Asserts as 'isDisplayed()' method is always returning 'false'.

Can anyone suggest me kindly further points to modify it and to get it fixed?

HTLM Code for the table :

<table id="examMarkEntryExamList" class="display table table-bordered table-striped dynamic-table display_header_class">
    <thead>
        <tr>
            <th class="text-center sortable sorted order1">Academic Year</th>
            <th class="text-center sortable sorted order1">Curriculum</th>
            <th class="text-center sortable sorted order1">Grade</th>
            <th class="text-center sortable sorted order1">Semester/Term</th>
            <th class="text-center sortable sorted order1">
                <a class="pagination-cuser-point">Exam Code</a>
            </th>
            <th class="text-center sortable sorted order1">Actions</th>
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
            <td>2016-2016</td>
            <td>LOCAL</td>
            <td>GRADE11</td>
            <td>2nd Term</td>
            <td>G11SecondTerm</td>
            <td>
        </tr>
    </tbody>
</table>

Selenium [Java] method to verify the column order

public void verifyColumnOrder(WebDriver driver, String tableId, ArrayList<String> columnHeaderList) {

        SoftAssert softassert = new SoftAssert();
        String relativeXpath = "//table[contains(@id,'"+tableId+"')]/";
        for (String columnHeader : columnHeaderList) {

           relativeXpath = relativeXpath + "/following-sibling::th[contains(.,'" + columnHeader + "')]";
             softassert.assertTrue(driver.findElement(By.xpath(relativeXpath)).isDisplayed());
    }
    softassert.assertAll();
}

Upvotes: 0

Views: 8661

Answers (2)

Florent B.
Florent B.

Reputation: 42528

An other solution would be to directly get the text of thead with getText(). It will return a concatenation of all the visible titles which will be easier to compare:

public void verifyColumnOrder(WebDriver driver, String tableId, ArrayList<String> columnHeaderList) {

    String expectedHeaders = String.join(" ", columnHeaderList);
    String visibleHeaders = driver.findElement(By.cssSelector("[id='" + tableId + "'] thead")).getText();

    Assert.assertEquals(visibleHeaders, expectedHeaders);
}

Upvotes: 1

Grasshopper
Grasshopper

Reputation: 9058

The xpath you are using is incorrect. th is not a sibling of table tag. It is 2 levels inside.

Try this CSS locator - "table[id='examMarkEntryExamList'] th". You can use it directly by using By.cssSelector or xpath "//table[@id='examMarkEntryExamList']//th".

Use this locator to get a List of webelements. From this get the list of text in the th tags.

List<WebElement> thelem = driver.findElements(By.xpath....);
List<String> thText = thelem.stream().map(e -> e.getText()).collect(toList());

Just assert -- `softassert.assertTrue(thText.equals(columnheaders));

Please check actual syntax.

Upvotes: 0

Related Questions