HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

CodedUi method which checks all cells in certain column

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
checksmethodallcolumncellswhichcertaincodedui

Problem

I have the following extension method which checks all cells for values in a table column with the specified header name:

public static bool AllCellsInColumnHaveValue(this HtmlTable table, string columnName)
{
    // Find the columnindex of the specified column name
    var cell = new HtmlHeaderCell(table);
    cell.SearchProperties.Add(new PropertyExpression
        (UITestControl.PropertyNames.FriendlyName, columnName, PropertyExpressionOperator.Contains));
    var columnIndex = cell.ColumnIndex;

    // Check all cells in the column if they have a value
    foreach (var row in table.Rows)
    {
        var tableCells = row.GetChildren();

        if (string.IsNullOrWhiteSpace(tableCells[columnIndex].FriendlyName))
        {
            return false;
        }
    }

    // If we get here all rows in the column contain a value
    return true;
}


Is there a way to improve this method?

Solution

Disclaimer: Never done this UITestControl stuff

Edge case

If the searched columnName isn't contained in the header of the table I would expect the returned columnIndex to be -1 and therefor the check for IsNullOrWhiteSpace() will throw an exception.

Instead of using row.GetChildren() and then access the needed cell by index, you should better use the right tool for the job and call the GetCell(int) method.

Because an extension method can be also called directly like AllCellsInColumnHaveValue(aHtmlTable, aColumnName); you should ensure that the passed in HtmlTable tableis not null.

Context

StackExchange Code Review Q#88345, answer score: 4

Revisions (0)

No revisions yet.