New to Telerik Test Studio? Download free 30-day trial

Verify Sort Order in an HTML Table

I would like to sort a column in an HTML Table and verify the content is in the correct order.

Solution

This is possible with a coded solution. The example below is against this Telerik demo site.

Let's verify the first column, Order ID, is in sequential order:

//Get the table.
HtmlTable table = Find.ByExpression<HtmlTable>("id=Grid", "|", "tagIndex=table:1");

int r = table.Rows.Count;
List<string> list = new List<string>();

//Place the text content of the Order ID cell from each row into the string list.
for (int i = 0; i < r; i++)
{
    HtmlTableRow row = table.Rows[i];
    HtmlTableCell cell = row.Cells[0];  
    list.Add(cell.TextContent);
    Log.WriteLine(cell.TextContent);
}

//Assert each string in the list is greater than the string before it.
for (int j = 0; j < list.Count; j++)
{
    if (j+1 == list.Count)
    {
        break;
    }
    else
    {
        Assert.IsTrue(list[j+1].CompareTo(list[j]) >= 0);
    }
}
In this article