java.lang.NumberFormatException error in Selenium Java



我是java selenium的新手。我在尝试自动化,但失败了。我需要验证我添加到购物车的产品的单价,以及当我再添加1件相同产品时将发生的价格,但我在线程"main"中得到错误"Exception;java.lang.NumberFormatException" .

这是我的代码:

//check the amount to be paid to the products
String unitPrice = driver.findElement(By.xpath("//span[@class='m-basket-card__price m-basket-card__price--main']")).getText();
System.out.println("single price of the product= " + unitPrice);
String totalPrice = driver.findElement(By.xpath("//sub[@class='js-card-price']")).getText();
System.out.println("total price of the cart = " + totalPrice);
Assert.assertEquals(totalPrice, Integer.parseInt(unitPrice * 2));

我在最后一行得到错误。

我尝试将产品的价格乘以2来验证相同产品的2的价格。我将字符串转换为int值。但是它在线程main中给出了一个异常。java.lang.NumberFormatException"错误。

您必须将totalPrice转换为int并将unitPrice相乘,如下所示:

Assert.assertEquals(Integer.parseInt(totalPrice),Integer.parseInt(unitPrice)*2);

如果unitPricetotalPrice返回float:

Assert.assertEquals(Float.parseFloat(totalPrice),Float.parseFloat(unitPrice)*2);

最新更新