[英]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. 我有一个测试方案,其中实际结果是固定百分比,例如135.68%,但我想将实际结果与预期结果进行比较,例如范围在130.00到136.00之间。 If actual result fall between the range of 130.00 to 136.00 then test is pass otherwise test is fail. 如果实际结果在130.00到136.00之间,则测试通过,否则测试失败。
I am using following Assert statement but no luck: 我正在使用以下Assert语句,但没有运气:
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);
}
}
}
Simply parse and check: 只需解析并检查:
String expectedPercentage = driver.findElement(By.xpath("//*[@id='app-banner']/div[1]/div/h2")).getText().replace("%", "");
double percentage = Double.parseDouble(expectedPercentage);
// Do this parsing if range is in string format. Otherwise skip this block
String range = "130.00 to 136.00";
String[] numbers = range.replaceAll(" ", "").split("to");
double min = Double.parseDouble(numbers[0]);
double max = Double.parseDouble(numbers[1]);
Assert.assertTrue(expectedPercentage + " is not between the range", min <= percentage && percentage <= max);
Parse the text and assert the result: 解析文本并声明结果:
String xPath = "//*[@id='app-banner']/div[1]/div/h2";
String text = driver.findElement(By.xpath(xPath)).getText();
double percentage = Double.parseDouble(text.replace("%", ""));
assertThat(percentage).isBetween(130.0, 136.0);
This solution uses isBetween(Double, Double)
from AssertJ as a static import . 此解决方案使用来自AssertJ的 isBetween(Double, Double)
作为静态导入 。 Alternatively, you can use the TestNG assertion I mentioned in my comment . 另外,您可以使用我在comment中提到的TestNG断言。
public static void assertEquals(double actual,
double expected,
double delta,
java.lang.String message)
Asserts that two floats are equal concerning a delta. If they are not, an AssertionError, with the given message, is thrown. If the expected value is infinity then the delta value is ignored.
assertEquals(133.00,expected,3.00,"ERROR")
I used regular testNg Assert and it worked for me as below: 我使用常规的testNg Assert,对我来说如下所示:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.