Saturday 13 July 2019

Selenium - Validate table in web page

Selenium - Validate table in web page


Often in Selenium we come across scenarios where we have to validate a web table for a entry or click a cell based on the row value. Not clear?

let me place this in easiest way, before starting please see other post and understand about selenium webdriver and Tables in html from here - Selenium Webdriver

As we all know we have scenarios in selenium webdriver where we have to validate data in web table or click on a row based on the column value of particular row. For such scenarios it is very difficult to validate and get the result.It become even more painful when we have to repeat the process frequently for different table in the web page.Below are the things that we can struggle with

  • Identifying particular table 
  • Identifying row of the table or row count in web table selenium webdriver
  • Identifying column of the table or column count in web table selenium webdriver

We can get the xpath for table, row or column but that needs to be modified if we have more than one table in the page. So we came up with below code which can be used as a generic method to validate any table in webpage. Below is the script.


Selenium webdriver table validation method:

public void ValidteTable(String DocumentsTable_Columns,String DocumentsTable_Rows, String ColumnName,String ColumnValue)
{

       int temp1=1;
for (int i=1;i<=DocumentsTable_Columns.size();i++)
{
 if(driver.findElement(By.xpath("(//table)[1]/thead/tr/th["+i+"]/div")).getText().equals("ColumnName"))

{
break;
}

temp1=temp1+1;
}

System.out.println("temp1 : " + temp1);

temp2=1;

for(int i=1;i<=DocumentsTable_Rows.size();i++)
  {
   if(driver.findElement(By.xpath("(//table)[1]/tbody/tr["+i+"]/td["+temp1+"]/div/p")).getText().equalsIgnoreCase("ColumnValue"))
   {
   break;
   }
   temp2=temp2+1;
  }
try
{

Assert.assertEquals(driver.findElement(By.xpath("(//table)[1]/tbody/tr["+temp2+"]/td["+temp1+"]/div/p")).getText(),ColumnValue);
System.out.println("##Validated "+columnvalidate+" is present");
 
}catch(AssertionError e)
{
System.out.println("Not Equal");
}
}





selenium,selenium webdriver,table in selenium,web tables in selenium,data table in selenium,selenium testing,handling web table in selenium,travelling in table in selenium,selenium training,how to get table data in selenium

0 comments: