Friday, June 22, 2012

Iterating through a Html Table



Some of the more common testing we do involves comparing a list of data on the page to a list of expected data from the db. I like to using the following code snippet to accomplish it.
1: for (int i = 0; i < DbTable.Rows.Count;i++ )
2: {
3: for (int j = 0; j < DbTable.Columns.Count; j++)
4: {
5: DataRow dbRrow = DbTable.Rows[i];
6: HtmlCell cellRow = new HtmlCell(contentTable);
7: cellRow.FilterProperties[HtmlCell.PropertyNames.RowIndex] = i.ToString();
8: cellRow.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = j.ToString();
9: if (!cellRow.FriendlyName.Contains(dbRrow[j].ToString()))
10: {
11: _error += dbRrow[j].ToString() + " did not match " + cellRow.FriendlyName;
12: }
13: }
14: }


In the inner loop you can always offset the row and column if you have a check box in your table or maybe an image you don't want to Compare on. -Sarah