Scraping dynamic web tables use assertion on dynamic values

Viewed 37

URL - https://www.etmoney.com/tools-and-calculators/ppf-calculator

pre-requisite - deposit amount ₹10,000

//Task 1: Verify if the Year end Balance of the last row is matching with the maturity amount approximately.

#Year end Balance of the last row
for(int r=1;r<noOfRows.size()+1;r++) {
            String data = driver.findElement(By.xpath("//table[@id='ppfTable']/tbody/tr["+r+"]/td[4]")).getText();
            String data1 = data.split("₹")[1];
            System.out.println(data1.replaceAll(",", ""));  
        }
#maturity amount
wait = new WebDriverWait(driver,Duration.ofSeconds(10));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[normalize-space()='2.71 Lacs']")));
        WebElement maturityAmount1 = driver.findElement(By.xpath("//span[@id='maturityAmount']/span"));
        System.out.println(maturityAmount1.getText());

My question is that how do I match Year end Balance of the last row(271214) and maturity amount(2.71 Lacs)

If I had to make an assertion based on above values how do I achieve it.

//Task 2: Verify if the table is populated with 20 rows and the year end balance of last row is matching with the maturity amount approximately.

Same as above stated Year end Balance of the last row(4,43,886) maturity amount(4.44 Lacs) use assertion.

1 Answers

The maturity amount is round off value. To assert that you need round off the Year end Balance of the last row amount.

To get the last row Year end Balance no need to iterate through all rows. You can find using single xpath

Once you get the text you need to do string manipulation and then round off the amount.

Code:

String yearEndBalance = driver.findElement(By.xpath("//table[@id='ppfTable']//tbody//tr[last()]//td[4]")).getText();
String yearEndBalance1 = yearEndBalance.split("₹")[1];

String yEndAmount=yearEndBalance1.replaceAll(",", ""); 
yEndAmount = new StringBuilder(yEndAmount).insert(3, ".").toString();
float yEndBalance =Float.parseFloat(yEndAmount);
float roundoffYearEndBal=Math.round(yEndBalance);

System.out.println((int)roundoffYearEndBal); 


WebElement maturityAmount1 = driver.findElement(By.xpath("//span[@id='maturityAmount']/span"));
String maturityAmount="maturityAmount1.getText().split(" ")[0].replace(".", "");

System.out.println(maturityAmount);



if ((int)roundoffYearEndBal==Integer.parseInt(maturityAmount))
{
  System.out.println("Matches");
}
else
{
  System.out.println("Does not Matches");
}
Related