Trying to save Value of a given Element into Variable

No problem :grin:

1 Like

Hello @Brandon_Hein

I am again here seeking your help. Sorry I am posting on the same thread as this is somewhat related.

In my same test case I am doing this :

Storing Product Price in one Variable = pdp_product_price_1

Storing Quantity of the Same product in Order = shopping_cart_product_quantity_1

Then doing Binary operation like this to calculate what should be the expected price of the product with the given quantity and then asserting if it is matching with what is showing up on website.

expecetd_total_item_wise_1 = pdp_product_price_1 * shopping_cart_product_quantity_1

assert expecetd_total_item_wise_1.equals(‘total_item_wise_1’)

When I run my test case I got the below error and it looks like there is a String issue and I need to change these string values to integer before doing any calculation

=============================================

03-07-2019 04:41:48 PM expecetd_total_item_wise_1 = pdp_product_price_1 * shopping_cart_product_quantity_1

Elapsed time: 0.025s

expecetd_total_item_wise_1 = pdp_product_price_1 * shopping_cart_product_quantity_1 FAILED.

Reason:

groovy.lang.MissingMethodException: No signature of method: java.lang.String.multiply() is applicable for argument types: (java.lang.String) values: [2]

Possible solutions: multiply(java.lang.Number)

at One Big Sanity Test Case.run(One Big Sanity Test Case:259)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:331)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:322)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:301)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:293)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:227)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1552005658083.run(TempTestCase1552005658083.groovy:21)

Yes. Do that and you’ll be fine.

I tried to use this to convert string into int and got error again.

pdp_product_price_int = pdp_product_price_1.toInteger()

shopping_cart_product_quantity_int = shopping_cart_product_quantity_1.toInteger()

expecetd_total_item_wise_1 = pdp_product_price_int * shopping_cart_product_quantity_1

============

Error

03-08-2019 08:47:34 AM pdp_product_price_int = pdp_product_price_1.toInteger()

Elapsed time: 0.007s

pdp_product_price_int = pdp_product_price_1.toInteger() FAILED.

Reason:

java.lang.NumberFormatException: For input string: “95.95”

There are two things to consider here:

1.) You should not convert the price to an integer. Integers are whole numbers, while prices generally have fractions (cents).
2.) There’s no such thing as a toInteger() method.

Whenever you are dealing with fractions, and you also plan on doing some calculation using those fractions, you are safest using BigDecimal:

BigDecimal price = new BigDecimal(pdp_product_price_1);
BigDecimal quantity = new BigDecimal(shopping_cart_product_quantity_1);
BigDecimal total = price.multiply(quantity);

Unlike using double, long, or float, this will ensure that there are no rounding errors.

My bad. I was waaay too tired to be on here trying to “help” last night.

Thanks, B.

Well, no more than we’re used to having to deal with :wink:

This post sums up why you should use BigDecimal (in this case) pretty nicely:

1 Like

@Brandon_Hein Thanks for your Solution and it is working as expected

total_item_wise_1 = WebUI.getText(findTestObject(‘4_Shopping_Cart_Page/Total_Item_Wise’)).replace(’$’, ‘’)

BigDecimal total_item_wise = new BigDecimal(total_item_wise_1)

WebUI.println('Total Item wise on Shopping Cart Page is : ’ + total_item_wise)

BigDecimal pdp_product_price = new BigDecimal(pdp_product_price_1)

WebUI.println('The Price of Product on PDP page is : ’ + pdp_product_price)

BigDecimal shopping_cart_product_quantity = new BigDecimal(shopping_cart_product_quantity_1)

WebUI.println('The Quantity on Shopping Cart page is : ’ + shopping_cart_product_quantity)

BigDecimal expecetd_total_item_wise = pdp_product_price.multiply(shopping_cart_product_quantity)

assert expecetd_total_item_wise.equals(total_item_wise)