Java 如何比较Selenium中的两个双精度数字
我在编写测试脚本时遇到了一些麻烦,我必须检查屏幕上的折扣和价格是否正确。我写了这样的东西:Java 如何比较Selenium中的两个双精度数字,java,selenium,Java,Selenium,我在编写测试脚本时遇到了一些麻烦,我必须检查屏幕上的折扣和价格是否正确。我写了这样的东西: @Test public void discountOnTheDress() { WebElement dressLink = driver.findElement(By.cssSelector("#block_top_menu > ul > li:nth-child(2) > a")); dressLink.click();
@Test
public void discountOnTheDress() {
WebElement dressLink = driver.findElement(By.cssSelector("#block_top_menu > ul > li:nth-child(2) > a"));
dressLink.click();
List<WebElement> listOfDresses = driver.findElements(By.cssSelector("div.product-container"));
for (WebElement e: listOfDresses) {
double endPrice = 0;
if(e.getText().contains("%")) {
String priceWithDollarBeforeDiscount = driver.findElement(By.xpath("//*[@id=\"center_column\"]/ul/li[3]/div/div[2]/div[1]/span[2]")).getText();
String priceWithoutDollarBeforeDiscount = priceWithDollarBeforeDiscount.replace("$", "");
double priceBeforeDiscount = Double.parseDouble(priceWithoutDollarBeforeDiscount);
String priceWithDollarAfterDiscount = driver.findElement(By.xpath("//*[@id=\"center_column\"]/ul/li[3]/div/div[2]/div[1]/span[1]")).getText();
String priceWithoutDollarAfterDiscount = priceWithDollarAfterDiscount.replace("$", "");
double priceAfterDiscount = Double.parseDouble(priceWithoutDollarAfterDiscount);
String discountWithPercent = driver.findElement(By.xpath("//*[@id=\"center_column\"]/ul/li[3]/div/div[2]/div[1]/span[3]")).getText();
String discountWithoutPercent = discountWithPercent.replaceAll("[^0-9]", "");
double discount = Double.parseDouble(discountWithoutPercent);
endPrice = ((discount / 100) * priceBeforeDiscount) + priceAfterDiscount;
Assert.assertEquals(priceAfterDiscount, endPrice);
}
}
}
@测试
公共无效折扣(D){
WebElement dressLink=driver.findElement(By.cssSelector(“#block_top_menu>ul>li:nth child(2)>a”);
dressLink.click();
List ListofDiscess=driver.findElements(By.cssSelector(“div.product-container”);
for(WebElement e:ListofAddresss){
双端价格=0;
如果(e.getText()包含(“%”){
字符串priceWithDollarBeforeDiscount=driver.findElement(By.xpath(“//*[@id=\\“center\u column\”)/ul/li[3]/div/div[2]/div[1]/span[2]”);
String priceWithoutDollarBeforeDiscount=priceWithDollarBeforeDiscount.replace(“$”,”);
double Price BeforeDiscount=double.parseDouble(折扣前不含美元的价格);
字符串priceWithDollarAfterDiscount=driver.findElement(By.xpath(“//*[@id=\\“center\u column\”)/ul/li[3]/div/div[2]/div[1]/span[1]”);
String price without dollaraafterdiscount=price withdollaraafterdiscount.replace(“$”,”);
double Price After折扣=double.parseDouble(不含美元的价格After折扣);
String discountWithPercent=driver.findElement(By.xpath(“/*[@id=\\“center\u column\”)/ul/li[3]/div/div[2]/div[1]/span[3]”);
字符串折扣WithOutpercent=折扣WithPercent.replaceAll(“^0-9]”,“”);
双倍折扣=双倍.parseDouble(折扣不超过%);
最终价格=((折扣/100)*折扣前价格)+折扣后价格;
Assert.assertEquals(折扣后价格,最终价格);
}
}
}
这不管用,我不知道怎么处理。有人知道如何正确书写吗
谢谢你的帮助。在我的脑海中,我可以想出两个简单的方法- 第一- 第二-
这个问题可能的重复并不是针对Selenium的,而是比较两个双精度或两个字符串。因为你把它作为一个字符串从页面上拉下来,你可以把它和一个字符串进行比较,在这一点上很容易。。。您可以断言这两个字符串相等。
if(priceAfterDiscount == endPrice) {
//Double values are same
}
if(Double.compare(priceAfterDiscount, endPrice) == 0) {
//Double values are same
}