Variables at test case level

I am trying to declare the variables at Test case level. But I need to concatenate the variable with another variables declared at script level

variable declared at script level :-

  1. formattedDate = mydate.format(‘MM/dd/yyyy HH mm ss’)
  2. String Prefix = ProductNamePrefix
  3. String ProductName = “$Prefix $formattedDate”

But Instead of above I want to use the 2 variable from the variable sheet.

How can I concatenate this variable at script level
Like I want to do
String ProductName = “$ProductNamePrefix $formattedDate”

But Script fails at taking ProductNamePrefix in this way

@Jaspreet.mangat Use the plus sign to concatenate strings together. And remove the quotes you have surrounding the variables–that would make the string equal to the variable names, not their contents.

String ProductName = $ProductNamePrefix + $formattedDate

And if you want to add a space between the two variables, then:

String ProductName = $ProductNamePrefix + ’ ’ + $formattedDate (there is a space between the quotes)

Interesting question.

I could reproduce your problem on my side. I created a test case sript like this:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String formattedDate = "20200729"
String Prefix = ProductNamePrefix
String ProductName = "$Prefix $formattedDate"
WebUI.comment("productName is $ProductName")

String ProductName2 = "$ProductNamePrefx $formattedDate"
WebUI.comment("productName2 is $ProductName2")

When I execute this I got the following message:

2020-07-29 07:57:29.336 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case (1)
2020-07-29 07:57:29.934 INFO  c.k.katalon.core.main.TestCaseExecutor   - (Default) ProductNamePrefix = smoke test
2020-07-29 07:57:30.075 INFO  com.kms.katalon.core.util.KeywordUtil    - #VisualTestingListenerImpl() Execution Profile 'default' is applied
...
2020-07-29 07:57:31.958 DEBUG testcase.New Test Case (1)               - 1: formattedDate = "20200729"
2020-07-29 07:57:31.959 DEBUG testcase.New Test Case (1)               - 2: Prefix = ProductNamePrefix
2020-07-29 07:57:31.962 DEBUG testcase.New Test Case (1)               - 3: ProductName = $Prefix $formattedDate
2020-07-29 07:57:31.965 DEBUG testcase.New Test Case (1)               - 4: comment(productName is $ProductName)

2020-07-29 07:57:31.973 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - productName is smoke test 20200729

2020-07-29 07:57:31.977 DEBUG testcase.New Test Case (1)               - 5: ProductName2 = $ProductNamePrefx $formattedDate
2020-07-29 07:57:32.009 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/New Test Case (1) FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: ProductNamePrefx for class: Script1595976811162
	at New Test Case (1).run(New Test Case (1):8)
	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:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	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 TempTestCase1595977044687.run(TempTestCase1595977044687.groovy:25)

2020-07-29 07:57:32.028 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case (1)

The important message is this: No such property: ProductNamePrefx for class: Script1595976811162

What is Script1595976811162 ? Did I write it? — No, I didn’t. But I could find the file at <projectDir>/Scripts/<TestCaseName>/Script1595976811162.groovy.

What I guess …

Katalon Studio saves the source of a test case script (a sort of Domain Specific Language) into a file named Script1595976811162.groovy. When Run, Katalon Studio parse it, transform it into a runnable (augmented) Groovy script and let the Compiler to compile it into *.class file.

Now I wrote “Katalon Studio parses and transforms a script into another language”. This technique is called Abstract Syntax Tree Transformation.

I suppose that Katalon Studio is not implemented in the way it can deal with a GString like "$ProductNamePrefix $formattedData" where ProductNamePrefix is declared as “a local variable to the test case script”. I do not support the current implementation. I think KS should be able to deal with it, though I personally do not think it is a big issue. ==> @ThanhTo

Anyway, you can take the workaround that @grylion54 proposed.

1 Like

try like this:

def ProductName = “${ProductNamePrefix} ${formattedDate}”

by not using the String type, groovy should see it as Gstring and perform the interpolation.

Otherwise if you prefer strong-typing, you have to use concatenation as already suggested (with ‘+’)

I tried def.

Still I got the same result.

Reason:

groovy.lang.MissingPropertyException: No such property: ProductNamePrefx for class: Script1595976811162

I do not think this understanding right. The type of left-hand side of = (def or String) does not affect to the meaing of the right-hand side of =.

See the Groovy documention https://groovy-lang.org/syntax.html#_string_interpolation :

4.4. Double-quoted string
Double-quoted strings are a series of characters surrounded by double quotes:

"a double-quoted string"

Double-quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.

@kazurayam strange. it may be a bug.
I am not using currently Katalon (or groovy) so I cannot check, but the def usually worked for me
(even without def should work if the code is in a groovy script, in a class the def is mandatory)

No such property: ProductNamePrefx

are you sure you don’t have a typo in your code? (prefx instead of prefix)

in a script, the variables are discovered by groovy at compile time as script properties (through Binding, if I remember it right)

I think, the Test Case script in Katalon Studio is NOT pure Groovy.
The Test Case script is a sort of Domain Specific Language for Katalon Studio based on the Groovy Syntax. Katalon Studio interprets the DSL to runnable Groovy, compile and run it.
I think Katalon Studio has a small bug while interpeting a notation “${name}”, and failed to make runnable Groovy code.

What do I mean Test Case script is a DSL in Katalon Studio? You should remember the syntax of Test Case script when you call Custom Keywords. For example, you are required to write like this:

CustomKeywords.'my.very.special.keyword.Hello'("World")

From a Groovy-syntax fundamentalist’s point of view, this is VERY SPECIAL syntax.

What is CustomKeywords? Is it a package name? — no, it isn’t. Is it a Class? Well, may be. But how come it is available without explicit import statement? — this doubt can only be answered by a perception: a Test Case script is not a pure Groovy.

by using strong-typing (with String), I believe you are forcing the variable to java.lang.String (so interpolation no longer works). Same will happen if you are using def but on the right side the expression is inside single quotes, or single tripe quotes.
Interpolation will work only with double, or double triple quotes.
You can try to declare two variables using both forms and print for each the variable.getClass()

Now … i know for sure in 6.x, interpolation worked, in both script (testcase) or class (customkeyword).
I was using it very often.
If is not working anymore … it’s a pity.

In general, GString interpolation works in 7.x of course.

The problem @Jaspreet.mangat addressed is a bit more specific. He pointed out that string interpolation “$name” does not work only when the name is defined at test case level.

Yeah, i think you are right.
Unfortunately I cannot check it, because lately I am using only linux (and not Ubuntu) … and Katalon GUI miserably fail to work on a decent linux distro, because is still using an ancient gtk framework.

How brave you are!

Hi Grylion54 ,
Thanks for the response. It doesn’t throw an error If I use
String UpdatedFirstName = “$UpdatedUserName + $formattedDate”

But then , Name is printed with +

println(UpdatedFirstName)

Test automation + 07/29/2020 12 58 31

@Jaspreet.mangat
You still have the quotes surrounding the variables. Remove the quotes.

String UpdatedFirstName = $UpdatedUserName + $formattedDate

Should become:

String UpdatedFirstName = $UpdatedUserName + $formattedDate

there is no need to use $ when concatenating. should be:

String UpdatedFirstName = UpdatedUserName + ' ' + formattedDate

@Jaspreet.mangat I have no idea what’s going on your end. Here’s a test I did:

image

Am I missing something?

1 Like

@Russ_Thomas please try also:

String strong_var = "${FirstName} - ${lastName}"

And:

gstring_var = "${FirstName} - ${lastName}"

And again:

def another_gstring_var = "${FirstName} - ${lastName}"

with the setup you did, and print all of them, to bring some light.

1 Like

great!
this show interpolation works even in in the case of String blah and with combined variables (local and public for the testcase, aka in ‘variables sheet’).
i was expecting the string blah variant to raise error or fake result, looks like groovy is smarter than we think.
It is another proof that sometime ‘the error is between the chair and the keyboard’