如何比较硒中的两个双数

时间:2018-08-14 15:15:17

标签: java selenium

我在编写测试脚本时遇到了一些麻烦,我必须检查website上的折扣和价格是否正确。我写了这样的东西:

    @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);
            }
        }
    }

它不起作用,我也不知道如何处理。有人知道如何正确编写它吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

从我的头顶上,我可以想到两种简单的方法-

第一-==

if(priceAfterDiscount == endPrice) {
    //Double values are same
}

第二个-java.lang.Double.compare()

if(Double.compare(priceAfterDiscount, endPrice) == 0) {
    //Double values are same
}