Validating the content of web table values is a very common requirement in automation testing. If you have ever used UFT, you might be aware that it has an inbuilt function GetCellData() to retrieve table cell values where we can simply pass the desired row and column value and it returns the value for that specific cell. However, there is no such inbuilt method available in UFT Developer formerly known as LeanFT but this does not stop us to validate the content of the web table.
In this article, we will see how we can create our custom GetCellData() method to get table cell value in LeanFT for the required row and column of a table whether it is a web table or a table built on any other technology. We will also learn how to get table Column Headers in LeanFT (UFT Developer).
First of all, we will create a class named CommonMethods and inside it, we will define the GetCellData method. This method has four arguments. The first one is the browser object, the second one is the table object, the third one is the row number and the last one is the column number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.commonMethods; import java.util.List; import com.hp.lft.report.*; import com.hp.lft.sdk.web.*; public class CommonMethods { public String GetCellData(Browser browser,Table objTable,int row,int col) throws ReportException { String sCellvalue=null; try { List<TableCell> iRowRetTable=objTable.getRows().get(row-1).getCells();//List Indexing starts with 0.A 0 means row 1 sCellvalue=iRowRetTable.get(col-1).getText();//List Index Index starts with 0.A 0 means Column 1 } catch (Exception e) { Reporter.reportEvent("Table Cell value can't be retrieved", "Row Column index out of range",Status.Failed); } return sCellvalue; } } |
Example-Get Table Cell Value in LeanFT
Now create a test with the class name GetTableCellValue and call theĀ GetCellData method in it. In this example, I am retrieving cell values for the fourth row and second column.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import org.testng.annotations.Test; import com.hp.lft.sdk.*; import com.commonMethods.CommonMethods; import com.hp.lft.report.*; import com.hp.lft.sdk.web.*; import unittesting.*; public class GetTableCellValue extends UnitTestClassBase { //Creating object of class CommonMethods CommonMethods cm=new CommonMethods(); static Browser browser; @Test public void test() throws GeneralLeanFtException, ReportException, CloneNotSupportedException { browser = BrowserFactory.launch(BrowserType.CHROME); browser.navigate("http://www.newtours.demoaut.com/"); browser.sync(); Table homeTable = browser.describe(Table.class, new TableDescription.Builder() .accessibilityName("") .index(3) .role("") .tagName("TABLE").build()); //Passing cell value 4th row and 2nd column String cellval = cm.GetCellData(browser, homeTable, 4, 2); System.out.println("Required Table Cell Value is: "+cellval); } } |
How to Get Table Column Headers in LeanFT | UFT Developer
There might be instances wherein you may need to get column names of a table to validate them.
The following example shows how to retrieve column headers using LeanFT.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.util.List; import org.testng.annotations.Test; import com.hp.lft.report.ReportException; import com.hp.lft.report.Reporter; import com.hp.lft.report.Status; import com.hp.lft.sdk.GeneralLeanFtException; import com.hp.lft.sdk.web.Browser; import com.hp.lft.sdk.web.BrowserFactory; import com.hp.lft.sdk.web.BrowserType; import com.hp.lft.sdk.web.Table; import com.hp.lft.sdk.web.TableDescription; import unittesting.UnitTestClassBase; public class GetColumnNames extends UnitTestClassBase { @Test public void test() throws GeneralLeanFtException, ReportException { Browser browser = null; try { //Open the Chrome browser. browser = BrowserFactory.launch(BrowserType.CHROME); browser.navigate("https://www.w3schools.com/html/html_tables.asp"); browser.sync(); Table companyTable = browser.describe(Table.class, new TableDescription.Builder() .accessibilityName("") .index(3) .role("") .tagName("TABLE").build()); List<String> columList = companyTable.getColumnHeaders(); for (int i = 0; i < columList.size(); i++) { System.out.println("Coumn Names: "+columList.get(i)); } } catch(AssertionError ex){ Reporter.reportEvent("Exception Occured","Test Failed", Status.Failed, ex); throw ex; } finally { //Close the browser browser.close(); } } } |