导致assertEquals的整数的预期/实际值之间的微小差异失败

时间:2019-07-17 14:51:33

标签: java

我正在编写一个自动化测试,以确认例如SKU *数量=总计时sku / cart值正确。有时(在同一站点的特定区域版本上),测试会失败,因为十进制值Eg的差异很小

(在这种情况下,数量为20,sku值为33.92,预期总值为678.40)

java.lang.AssertionError: 预期:678.3999633789062 实际:678.4000244140625

显然,计算在某个时间点已经结束,但仍然非常接近,这对于将计算进行到2位以上的小数位也是不必要的。

在Assert.assertEquals进行单价和数量与总数量的比较之前,我尝试限制从元素获取数量和价格值的方法中使用的小数位数。主要在“获取”值方法中使用.NumberFormat

这是黄瓜/小黄瓜步骤的代码

@Then("^the SKU cart Price reflects the unit price times the quantity value$")
    public void theSKUCartPriceReflectsTheUnitPriceTimesTheQuantityValue() {
        itemsInTheBasket_page.waitForCalcResult();
System.out.println(itemsInTheBasket_page.getfirstcartQuantityValue());     System.out.println(itemsInTheBasket_page.getUnitPriceOfFirstItemInCart());
System.out.println(itemsInTheBasket_page.getfirstCartItemPriceValue());
Assert.assertEquals(itemsInTheBasket_page.getfirstcartQuantityValue() * itemsInTheBasket_page.getUnitPriceOfFirstItemInCart(), itemsInTheBasket_page.getfirstCartItemPriceValue(), 0.0);
    }
``


And the code for the for methods used above

 public void waitForCalcResult() {
        Waits.clickable(driver, costCalcResult, 10);
    }


public int getfirstcartQuantityValue() {
        Waits.visible(driver, firstCartItemQuantityField, 5);
        return Integer.parseInt(firstCartItemQuantityField.getAttribute("value"));

    }

public float getUnitPriceOfFirstItemInCart() {
        Waits.visible(driver,firstCartSKUUnitPriceValue, 5);
        String unitValue = firstCartSKUUnitPriceValue.getText();
        unitValue = unitValue.replaceFirst(",", ".");
        unitValue = unitValue.replaceFirst(" ", "");
        unitValue = unitValue.replaceFirst("€", "");
        unitValue = unitValue.replaceAll("[$]", "");
        unitValue = unitValue.replaceAll("£", "");
        return Float.valueOf(unitValue);
    }

 public float getfirstCartItemPriceValue() {
        Waits.visible(driver,firstCartItemPriceValue,5);
        String PriceValue = firstCartItemPriceValue.getText();
        PriceValue = PriceValue.replaceFirst(",", ".");
        PriceValue = PriceValue.replaceFirst(" ", "");
        PriceValue = PriceValue.replaceFirst("€", "");
        PriceValue = PriceValue.replaceAll("[$]", "");
        PriceValue = PriceValue.replaceAll("£", "");
        return Float.valueOf(PriceValue);
    }

0 个答案:

没有答案