Home > other >  java.lang.NumberFormatException error in Selenium Java
java.lang.NumberFormatException error in Selenium Java

Time:12-07

I'm newcomer to java selenium. I'm trying to automate and I have failed. I need to verify the unit price of the product I added to the cart and the price that will occur when I add 1 more of the same product, but I get the error "Exception in thread"main" java.lang.NumberFormatException".

That's my code:

//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));

I get the error on the last line.

I tried to multiply the price of the product by 2 to verify the price of 2 of the same product. I converted string to int value. But it gave an "Exception in thread "main" java.lang.NumberFormatException" error.

CodePudding user response:

You have to convert the totalPrice to int and also multiply the unitPrice as below:

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

If unitPrice and totalPrice return float:

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