Tuesday 27 May 2014

Store all rows and columns of Web table in List using Coded UI Test (For performance improvement)


There are many situations where we might need to verify all the columns and rows of one or more table in a page against Database table to check its data.

This can be done if we store all the UI table rows and columns in List<> and then compare it with any data source.

This is a very time consuming process for Coded UI Test if we write the code with the built-in function provided by Microsoft.

All we do while writing code is this.

§  First get the number of rows

§  Next get the number of columns

§  Iterate through the rows and columns

§  Using GetCell method, get the value of the cell

public static void UITableToArray(HtmlTable table)
 {
 //Declare List<> or Dictionary
 try
 {
 int rowCount = table.RowCount;
 int colCount = table.ColumnCount;
 for (int rowIndex = 1; rowIndex &lt; rowCount; rowIndex ++)
 {
 for (int colIndex = 1; colIndex &lt; colCount; colIndex ++)
 {
 if (table.GetCell(row, col).InnerText != null)
 {
 //Store in list or dictionary
 }
 }
 }
 }
 catch (Exception e)
 {
 }
 }

But the problem is, the code is very slow.

If you would like to read a table in page with 100 rows and 25 columns, then it will take you more than 20 minutes to read all the data!!!

Believe me, it’s true fact.

The reason is this

The GetCell method, the method is slow because every time it does the following

  • §  Identifies the page
  • §  Identifies the table
  • §  Identifies the Row and its column with index supplied, then reads the data

The process is fast if we read only one data, but what happen if we want to read hundreds of data, the above will surely very slow.

So, whats the solution?




































As you could see above, the table is identified ONLY ONCE and so is the row collection, which means all the rows and its related values are found in just one single shot, which is awesome.

This will eradicate the above problem faced by GetCell() method which we were discussing about earlier.

As you could see, the code will now look like one shown below.

public static void UITableToArrayList(HtmlTable table)
{
//Declare List<> or Dictionary
    try
    {
        UITestControlCollection rowcontrol = table.Rows;
        string cellval = null;
        int rowcount = 0;
        //col Names
        string[] colNames = table.GetColumnNames();
        foreach (UITestControl item in rowcontrol)
        {
            int colCount = 0;
            if (item is HtmlRow)
            {
                rowcount++;
                foreach (HtmlControl cell in item.GetChildren())
                {
                    cellval = cell.InnerText;
                    if (colNames[colCount] != null)
                    {
                    //Store it in List or Dictionary
                    }
                    colCount++;
                }
            }
        }
    }
    catch (Exception e)
    {
    }
}

No comments:

Post a Comment