-1

I have some trouble to write test script where I have to check if discount and prices on the website are correct. I wrote something like that:

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

It doesn't work and I don't know how to handle that. Does someone have any idea how to write it correctly?

Thanks for your help.

Sumithran
  • 6,217
  • 4
  • 40
  • 54
vott
  • 53
  • 8
  • Possible duplicate of [How to compare two double values in Java?](https://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java) – JeffC Aug 14 '18 at 15:31
  • 1
    This question isn't specific to Selenium, it's more about comparing two doubles or two strings really. Since you pull it off the page as a string, you could compare it to a string and at that point it's easy... you can just assert that the two strings are equal. – JeffC Aug 14 '18 at 15:32

1 Answers1

0

From the top of my head, I can think of two easy ways -

First - ==

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

Second - java.lang.Double.compare()

if(Double.compare(priceAfterDiscount, endPrice) == 0) {
    //Double values are same
}
Salvatore
  • 1,435
  • 1
  • 10
  • 21