Unable to create variables by compiler at runtime

Description :

I m trying to create a boolean variable runtime under a if…else condition, but while debugging the variable is not created in the memory though control reaches the variable line and exception is thrown with messege groovy.lang.MissingPropertyException: No such property: expectedValue for class: Script1539691120865

Environment info :

Katalon Studio

Version: 5.8.0

Build: 1
Windows 10

Steps to reproduce :

1. Copy paste the below code and try to execute:

if(true)

boolean actualValue = false;

else

boolean actualValue = true;

if(false)

boolean expectedValue = true;

else

boolean expectedValue = false;

if(expectedValue == actualValue)

println "Status Check Pass"

else

println "Status Check Fail, Please log a bug for this."  

Expected Result : “Status Check Pass” should get printed in the console.

Actual Result : Get exception :

Test Cases/Admin/Extention Engine/ERPConnectorSample/Add/TC003_VerifySaveAndValidateSavedData FAILED because (of) (Stack trace: groovy.lang.MissingPropertyException: No such property: expectedValue for class: Script1539691120865

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)

at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)

at Script1539691120865.VerifyStatus(Script1539691120865.groovy:81)

at Script1539691120865$VerifyStatus$4.callCurrent(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)

at Script1539691120865.VerifyDataEntry(Script1539691120865.groovy:63)

at Script1539691120865$VerifyDataEntry$1.callCurrent(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)

at Script1539691120865.run(Script1539691120865.groovy:38)

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

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

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

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

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

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

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

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

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

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)

at TempTestCase1539765892933.run(TempTestCase1539765892933.groovy:22)

at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)

at groovy.lang.GroovyShell.run(GroovyShell.java:518)

at groovy.lang.GroovyShell.run(GroovyShell.java:507)

at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)

at groovy.ui.GroovyMain.run(GroovyMain.java:384)

at groovy.ui.GroovyMain.process(GroovyMain.java:370)

at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)

at groovy.ui.GroovyMain.main(GroovyMain.java:109)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)

at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

)

Test Cases/Admin/Extention Engine/ERPConnectorSample/Add/TC003_VerifySaveAndValidateSavedData.VerifyStatus:81

Test Cases/Admin/Extention Engine/ERPConnectorSample/Add/TC003_VerifySaveAndValidateSavedData.VerifyDataEntry:63

Test Cases/Admin/Extention Engine/ERPConnectorSample/Add/TC003_VerifySaveAndValidateSavedData.run:38

define those variables outside if statement. in your example, you defined them as local variables in if statements
check this out:

boolean actualValueboolean expectedValueif(true)   actualValue =  false;else   actualValue =  true; if(false)   expectedValue = true;else   expectedValue = false; if(expectedValue == actualValue)    println "Status Check Pass"else    println "Status Check Fail, Please log a bug for this."

console:

10-17-2018 01:37:31 PM - [START]  - Start Test Case : Test Cases/__Sandbox/New Test Case 910-17-2018 01:37:31 PM - [INFO]   - Evaluating variables for test case10-17-2018 01:37:32 PM - [START]  - Start action : Statement - actualValue = <not implemented yet for class: org.codehaus.groovy.ast.expr.EmptyExpression>10-17-2018 01:37:32 PM - [END]    - End action : Statement - actualValue = <not implemented yet for class: org.codehaus.groovy.ast.expr.EmptyExpression>10-17-2018 01:37:32 PM - [START]  - Start action : Statement - expectedValue = <not implemented yet for class: org.codehaus.groovy.ast.expr.EmptyExpression>10-17-2018 01:37:32 PM - [END]    - End action : Statement - expectedValue = <not implemented yet for class: org.codehaus.groovy.ast.expr.EmptyExpression>10-17-2018 01:37:32 PM - [START]  - Start action : Statement - If (true)10-17-2018 01:37:32 PM - [END]    - End action : Statement - If (true)10-17-2018 01:37:32 PM - [START]  - Start action : Statement - If (false)10-17-2018 01:37:32 PM - [END]    - End action : Statement - If (false)10-17-2018 01:37:32 PM - [START]  - Start action : Statement - Else10-17-2018 01:37:32 PM - [START]  - Start action : Statement - expectedValue = false10-17-2018 01:37:32 PM - [END]    - End action : Statement - expectedValue = false10-17-2018 01:37:32 PM - [END]    - End action : Statement - Else10-17-2018 01:37:32 PM - [START]  - Start action : Statement - If (expectedValue == actualValue)Status Check Pass10-17-2018 01:37:32 PM - [END]    - End action : Statement - If (expectedValue == actualValue)10-17-2018 01:37:32 PM - [PASSED] - Test Cases/__Sandbox/New Test Case 910-17-2018 01:37:32 PM - [END]    - End Test Case : Test Cases/__Sandbox/New Test Case 9

Or you can also leave out the type definition:

if(true)
 actualValue =  false;
else
 actualValue =  true;
if(false)
 expectedValue = true;
else
 expectedValue = false;
if(expectedValue == actualValue)
 println "Status Check Pass"
else
 println "Status Check Fail, Please log a bug for this."

Result:

10-17-2018 03:49:18 PM - [START]  - Start action : Statement - If (true)
10-17-2018 03:49:18 PM - [END]    - End action : Statement - If (true)
10-17-2018 03:49:18 PM - [START]  - Start action : Statement - If (false)
10-17-2018 03:49:18 PM - [END]    - End action : Statement - If (false)
10-17-2018 03:49:18 PM - [START]  - Start action : Statement - Else
10-17-2018 03:49:18 PM - [START]  - Start action : Statement - expectedValue = false
10-17-2018 03:49:18 PM - [END]    - End action : Statement - expectedValue = false
10-17-2018 03:49:18 PM - [END]    - End action : Statement - Else
10-17-2018 03:49:18 PM - [START]  - Start action : Statement - If (expectedValue == actualValue)
Status Check Pass
10-17-2018 03:49:18 PM - [END]    - End action : Statement - If (expectedValue == actualValue)

Thanks @Gerard van der Winkel I know about this workaround, but I hope a fix is required

aman daharwal said:

Thanks @Gerard van der Winkel I know about this workaround, but I hope a fix is required

i was thinking that it’s an defined behaviour of groovy language … what you want to fix?

@Andrej Podhajský I know very well about the behaviour of groovy language, but it should work other behaviour boolean actualValue = false;

aman said:

@Andrej Podhajský I know very well about the behaviour of groovy language, but it should work other behaviour `boolean actualValue = false;`

Nope…you explicitely declare your variable within the then/else block, which results in the variable to become a local variable…only visible within that block of code, never outside that block. Standard behavior of Groovy language.

It is important to understand that you need to use an undeclared variable if you want to write into the binding. Using def or an explicit type like in your example below would fail because you would then create a local variable:

1 Like

Gerard van der Winkel said:

aman said:

@Andrej Podhajský I know very well about the behaviour of groovy language, but it should work other behaviour `boolean actualValue = false;`

Nope…you explicitely declare your variable within the then/else block, which results in the variable to become a local variable…only visible within that block of code, never outside that block. Standard behavior of Groovy language.

It is important to understand that you need to use an undeclared variable if you want to write into the binding. Using def or an explicit type like in your example below would fail because you would then create a local variable:

yup, that’s what i posted on begening +1