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.