While testing a web application we very often validate the contents of a web table. Here we will take a look at how to get row number with cell text in LeanFT ( UFT Developer ) for a particular column of a web table. For example, you are submitting a form after entering some details and when the form is submitted the records are added in the last row of a web table. Now you will have to validate all the records of the newly added row. In such a scenario finding the required row number with the specific is the most efficient way rather than iterating the whole table.
In this article, I have shown how to retrieve the row number of a table cell having a particular value in the desired column. The following is a web table having three columns. We will retrieve the row number of the cell in column number two having value Roland Mendel as highlighted in the screenshot.
Get Row Number With Cell Text in LeanFT-Example
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 |
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 com.hp.lft.sdk.web.TableRow; import unittesting.UnitTestClassBase; public class TestFindRowWithCellTextInColumn extends UnitTestClassBase { @Test public void testMercury() throws GeneralLeanFtException, ReportException { try { //Open the Chrome browser. Browser browser = BrowserFactory.launch(BrowserType.CHROME); browser.navigate("https://www.w3schools.com/html/html_tables.asp"); browser.sync(); if (browser.getTitle().equalsIgnoreCase("HTML Tables")) { Reporter.reportEvent("Page Tilte should be matched", "Page title displayed is - "+ browser.getTitle() ,Status.Passed); } else { Reporter.reportEvent("Page Tilte should did not match", "Page title displayed is - "+ browser.getTitle() ,Status.Failed); } Table companyTable = browser.describe(Table.class, new TableDescription.Builder() .accessibilityName("") .index(3) .role("") .tagName("TABLE").build()); //Looking for text 'Roland Mendel' in Column 1.Column indexing starts with 0 TableRow row = companyTable.findRowWithCellTextInColumn("Roland Mendel", 1); System.out.println("Row number of the desired text in the column 2 is: "+row.getRowIndex()); } catch(Error e){ Reporter.reportEvent("Runtime error occured","Failed during test",Status.Failed, e); throw e; } } } |
Code Explanation
We have passed two parameter values in the findRowWithCellTextInColumn method. The first is the text that we are looking for in the table and the second parameter value is the column number where the value should be searched in the web table. The column indexing starts with 0 and in the above example, we are looking for the required value in column 2 that is why we have passed column value as 1.
You can also refer to my post on UFT Developer | LeanFT Get Table Cell Values for all Rows of a Column to know how to retrieve all cell values for the specified column.