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

HTML Table Sum and Average

I would like to get the sum and the average of the values in a certain column of an HTML table.

Solution

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

HtmlTable table = Find.ByTagIndex<HtmlTable>("table", 0);

double r = table.Rows.Count;
double sum = 0;

for (int i = 0; i < r; i++)
{
    HtmlTableRow row = table.Rows[i];
    HtmlTableCell cell = row.Cells[1];

    string num = cell.TextContent.Substring(0, 2);
    int j = i + 1;
    Log.WriteLine("Row" + j.ToString() + ": " + num);

    double percent = Convert.ToDouble(num);
    sum  = sum + percent;
}

Log.WriteLine("Sum: " + sum.ToString());
double average = sum / r;
Log.WriteLine("Average: " + average.ToString());
Assert.IsTrue(sum == 90);
In this article