In this post we will discuss working with tables using Selenium.
Basically, all the tables rendered in web pages are html tables (other than some third party controls such as telerik or infragistic controls tables)
In automation of application, we will encounter places to
Read the table
Get a cell value
Compare the table value with database
Hence, its very important for anyone to test tables.
Simple table
Simple table we are going to work with for this demonstration will look something like this
Simple algorithm to read the table
Here is the complete video of the above discussion
Here is the complete code from the above video
Read Table
static List _tableDatacollections = new List();
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,
});
//Move to next column
colIndex++;
}
rowIndex++;
}
}
Read Cell
public static string ReadCell(string columnName, int rowNumber)
{
var data = (from e in _tableDatacollections
where e.ColumnName == columnName && e.RowNumber == rowNumber
select e.ColumnValue).SingleOrDefault();
return data;
}
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