Problem
Ich bin Neuling in Java Selen. Ich versuche zu automatisieren und bin gescheitert. Ich muss den Stückpreis des Produkts, das ich dem Warenkorb hinzugefügt habe, und den Preis überprüfen, der auftritt, wenn ich 1 weiteres des gleichen Produkts hinzufüge, aber ich erhalte die Fehlermeldung „Exception in thread“main“ java.lang.NumberFormatException“.
Das ist mein 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));
Ich bekomme den Fehler in der letzten Zeile.
Ich habe versucht, den Preis des Produkts mit 2 zu multiplizieren, um den Preis von 2 desselben Produkts zu überprüfen. Ich habe den String in einen Int-Wert konvertiert. Aber es gab einen Fehler “Exception in Thread “main” java.lang.NumberFormatException”.
Lösung
Sie müssen das totalPrice
in int umwandeln und auch wie folgt multiplizieren unitPrice
:
Assert.assertEquals(Integer.parseInt(totalPrice),Integer.parseInt(unitPrice)*2);
If- unitPrice
und totalPrice
Return-Float:
Assert.assertEquals(Float.parseFloat(totalPrice),Float.parseFloat(unitPrice)*2);
Beantwortet von – AbiSaran
Antwort geprüft von – Jay B. (FixError Admin)