Trying to save Value of a given Element into Variable

Hello Everyone,

I am trying to save Value of one element into a variable but everytime it is blank value.

Below is the script :

pdp_product_name_1 = WebUI.getText(findTestObject(‘3_Page_PDP/PDP_Product_Name’))

WebUI.comment(pdp_product_name_1)

pdp_product_type_1 = WebUI.getText(findTestObject(‘3_Page_PDP/PDP_Product_Type’))

WebUI.comment(pdp_product_type_1)

‘Amount’

pdp_product_price_1 = WebUI.getText(findTestObject(‘3_Page_PDP/PDP_Product_Price’))

WebUI.comment(pdp_product_price_1)

‘Amount’

pdp_product_quantity_1 = WebUI.getText(findTestObject(‘3_Page_PDP/PDP_Product_Quantity’))

WebUI.comment(pdp_product_quantity_1)

WebUI.getText() will return the text node value for an element, if any. In your case, the input element in question does not have a text node. You are actually trying to get an attribute value, not a text node. Try this instead:

WebUI.getAttribute(findTestObject('3_Page_PDP/PDP_Product_Quantity'), 'value')

2 Likes

It worked.

Thanks as always !!

One more question :

If you see the screenshot above there is a price value, I used

WebUI.getText(findTestObject(‘3_Page_PDP/PDP_Product_Price’))

and it gave me the price as ‘$95.95’ with a $ . How I can only get ‘95.95’ as Product Price ?

WebUI.getText(findTestObject(‘3_Page_PDP/PDP_Product_Price’)).replace(’$’, ‘’)

Getting an error :

Script1551977086979.groovy: 70: Invalid variable name. Must start with a letter but was: ‘3_Page_PDP
. At [70:30] @ line 70, column 30.
WebUI.getText(findTestObject(‘3_Page_PDP / PDP_Product_Price’)).replace(’$’, ‘’)
^

1 error

at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:199)
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 TempTestCase1551989466024.run(TempTestCase1551989466024.groovy:21)

Ah of course, Groovy syntax. Try this one instead:

WebUI.getText(findTestObject(“3_Page_PDP/PDP_Product_Price”)).replace("\$", “”)

I think the dollar sign is a reserved character in Groovy.

Getting Same error

on :

WebUI.getText(findTestObject(“3_Page_PDP/PDP_Product_Price”)).replace("$", “”)

or

pdp_product_price_1 = WebUI.getText(findTestObject(“3_Page_PDP/PDP_Product_Price”)).replace("$", “”)

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

Script1551977086979.groovy: 70: Invalid variable name. Must start with a letter but was: “3_Page_PDP
. At [70:30] @ line 70, column 30.
WebUI.getText(findTestObject(“3_Page_PDP/PDP_Product_Price”)).replace("$", “”)
^

1 error

at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:199)
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 TempTestCase1551989831615.run(TempTestCase1551989831615.groovy:21)

Hmmmm ok then maybe it’s complaining about your TestObject id. Try renaming your test object to start with a letter instead of a number.

I found it

WebUI.getText(findTestObject(‘3_Page_PDP/PDP_Product_Price’)).replace(’$’, ‘’)

’ instead of "

That was my original suggestion:

1 Like

Yes, but it errored at that time. Don’t know why

Hmmm, oh well, at least it’s working for you :upside_down_face:

I got another question. Sorry

As you mentioned with my 1st question that I should use getAttribute. It is giving me the correct value but I wanted to verify if the Value is correct.

I used assert statement like this and it is passing but it should fail as the getAttribute Value is 2 which is correct
not 1.

pdp_product_quantity_2 = WebUI.getAttribute(findTestObject(‘3_Page_PDP/PDP_Product_Quantity’), ‘value’)

assert !(pdp_product_quantity_2.equals(1))

20%20PM

Put the ‘1’ in quotes:

assert !(pdp_product_quantity_2.equals(“1”))

It is still getting marked as pass or It is not throwing error on step 27.

My script is like this :

pdp_product_quantity_2 = WebUI.getAttribute(findTestObject(‘3_Page_PDP/PDP_Product_Quantity’), ‘value’)

assert !(pdp_product_quantity_2.equals(“1”))

This is the expected result based on your assertion, isn’t it?:

1.) pdp_product_quantity_2 should be equal to “2”, based on your screenshot
2.) therefore, your assertion is equivalent to:

assert !("2".equals("1"))

which is also equivalent to:

assert "2" != "1"

which of course would return a result of true, thus the assertion passes.

1 Like

Sorry I may be explained it wrongly. My objective here is to check if the value is equal to 2 .pdp_product_quantity_2 is giving correct value as 2 .

I wanted to verify this. I gave equals(“1”) because I wanted the script to fail because the expected quantity is 2.

Then just assert this:

assert pdp_product_quantity_2.equals("2")

If pdp_product_quantity_2 is not equal to “2”, then the assertion will fail.

1 Like

Thanks, Brandon, It worked.

Sorry, the way I explained the issue was wrong.