Validation of cell values of a table in the application under test is a common requirement as it is a very basic part of the functionality in regression testing. In this article, we will see how to get table cell values in LeanFT for all rows of a particular column and validate it. Here we will retrieve the values of the second column that is on the home page of the Mercury Tours demo application and validate that the price starts with the $ symbol.
The first column contains source and destination names and the second column contains the fair value.
Example of LeanFT Get Table Cell Values
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 |
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 com.hp.lft.verifications.Verify; import unittesting.UnitTestClassBase; public class ValidateTableValues extends UnitTestClassBase { @Test public void testMercury() throws GeneralLeanFtException, ReportException { try { //Open the Chrome browser. Browser browser = BrowserFactory.launch(BrowserType.CHROME); browser.navigate("http://www.newtours.demoaut.com/"); browser.sync(); Table specialOffersTable = browser.describe(Table.class,new TableDescription.Builder().tagName("TABLE").index(9).build()); //Find the rows in the Special Offers table and retrieves the currency values and check Price value starts with $ symbol for (TableRow offerTable : specialOffersTable.getRows()) { //Column Indexing starts with 0 System.out.println("Print all Row Values of 2nd column: "+offerTable.getCells().get(1).getText()); Verify.isTrue(offerTable.getCells().get(1).getText().startsWith("$"), "$ Sybmol should be present in the price", "Verify currency values are in Dollar."); } } catch(Error e){ Reporter.reportEvent("Offer Table Validation","Exception occured.Test Failed during execution",Status.Failed, e); throw e; } } } |
Code Explanation
To retrieve the specific cell value in LeanFT we can use the following syntax:
String sCellValue=tableobject.get(<Column Index>).getText();
The column indexing starts with one. In the above example, we are retrieving the text of the second column that is why an index 1 has been provided. Additionally, I have used another method startsWith() that checks the first character of the string return by the getText() method. Following this, you can write your own custom method that will return the cell value of a table by just providing a table object and column Index.