Curious Curious - 3 months ago 26
Java Question

Selenium Web Driver - Java - TestNG - Assert.assertEquals how to compare actual result with the range between expected result

I have test scenario where actual result is fix percentage For example 135.68% but I want to compare actual result with the Expected result which is in range For example 130.00 to 136.00. If actual result fall between the range of 130.00 to 136.00 then test is pass otherwise test is fail.

I am using following Assert statement but no luck:

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;


@Test
public class TC20042 extends BaseClass{

private SoftAssert m_assert = new SoftAssert();


public void registration() throws Exception, InterruptedException {
FileInputStream fi=new FileInputStream("C:\\File\\Book2.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(2);


try
{
for (int i = 0; i < s.getRows(); i++)
{
//Read data from excel sheet
String s1 = s.getCell(0,i).getContents();

String xPath = "//*[@id='mainForm']/div/div[3]/div/div/table/tbody/tr[1]/td[6]";
String text = driver.findElement(By.xpath(xPath)).getText();
double percentage = Double.parseDouble(text);

assertThat(percentage).isBetween(130.0, 136.0);

private Object assertThat(double percentage) {
// TODO Auto-generated method stub
return null;
}



}

}
catch(Exception e)
{
System.out.println(e);

}
}

}

Answer

I used regular testNg Assert and it worked for me as below:

String xPath = "//*[@id='mainForm']/div/div[3]/div/div/table/tbody/tr[1]/td[6]";
                String text = driver.findElement(By.xpath(xPath)).getText();
                text  = text.replace("%", "");
                double percentage = Double.parseDouble(text);   

                Assert.assertTrue(percentage > 130.67 && percentage < 135.69);