ExecuteAutomation

Working with complex table using Selenium

In the last post we discussed how to work with simple table using Selenium, in this post we will discuss working with complex table of html using selenium

Complex Table

The table looks something like this

Things to note in the table

This table is complex because as you can see there is

Coding Complexity

We have used little advanced coding concepts of C# in this post, since this makes lines of codes reduced a lot as well. In this post we have used
  1. LINQ
  2. Yield return
  3. Collections with custom classes
  4. Null interpolation
But, the code is clearly explained in the video below Here is the complete video of the above discuss Here is the complete code from the above video

Read Table

       public static void ReadTable(IWebElement table)
        {
            //Get all the columns from the table
            var columns = table.FindElements(By.TagName("th"));

            //Get all the rows
            var rows = table.FindElements(By.TagName("tr"));

            //Create row index
            int rowIndex = 0;

            foreach (var row in rows)
            {
                int colIndex = 0;

                var colDatas = row.FindElements(By.TagName("td"));

                foreach (var colValue in colDatas)
                {
                    _tableDatacollections.Add(new TableDatacollection
                    {
                        RowNumber = rowIndex,
                        ColumnName = columns[colIndex].Text != "" ?
                                     columns[colIndex].Text : colIndex.ToString(),
                        ColumnValue = colValue.Text,
                        ColumnSpecialValues = colValue.Text != "" ? null :
                                              colValue.FindElements(By.TagName("input"))
                    });

                    //Move to next column
                    colIndex++;
                }
                rowIndex++;
            }
        }

PerformActionOnCell

       public static void PerformActionOnCell(string columnIndex, string refColumnName, string refColumnValue,
            string controlToOperate = null)
        {
            foreach (int rowNumber in GetDynamicRowNumber(refColumnName, refColumnValue))
            {
                var cell = (from e in _tableDatacollections
                            where e.ColumnName == columnIndex && e.RowNumber == rowNumber
                            select e.ColumnSpecialValues).SingleOrDefault();


                //Need to operate on those controls
                if (controlToOperate != null && cell != null)
                {
                    var returnedControl = (from c in cell
                                           where c.GetAttribute("value") == controlToOperate
                                           select c).SingleOrDefault();
                    //ToDo: Currenly only click is supported, future is not taken care here
                    returnedControl?.Click();
                }
                else
                {
                    cell?.First().Click();
                }
            }
        }



        private static IEnumerable GetDynamicRowNumber(string columnName, string columnValue)
        {

            //dynamic row
            foreach (var table in _tableDatacollections)
            {
                if (table.ColumnName == columnName && table.ColumnValue == columnValue)
                    yield return table.RowNumber;
            }

        }
Thanks for reading the post and watching the video !!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK