# Variables at test case level

**URL:** <https://forum.katalon.com/t/variables-at-test-case-level/45981>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [July 28, 2020, 3:29pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981 "2020-07-28T15:29:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaspreet.mangat](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jaspreet.mangat](https://forum.katalon.com/u/Jaspreet.mangat)\
**Post date:** [July 28, 2020, 3:29pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/1 "2020-07-28T15:29:51Z")

</div>

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.

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/f/f56c516f6b4ea69211c28e62aee19005676810bc.png)

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

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [July 28, 2020, 7:49pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/2 "2020-07-28T19:49:09Z")

</div>

@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)

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 28, 2020, 11:03pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/3 "2020-07-28T23:03:13Z")

</div>

Interesting question.

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

```auto
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:

```auto
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`.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 29, 2020, 1:28am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/4 "2020-07-29T01:28:36Z")

</div>

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](https://groovy-lang.org/metaprogramming.html#_compile_time_metaprogramming).

I suppose that Katalon Studio is not implemented in the way it can deal with a [GString](https://groovy-lang.org/syntax.html#_string_interpolation) 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.

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [July 29, 2020, 8:59am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/5 "2020-07-29T08:59:44Z")

</div>

> [@Jaspreet.mangat](#):
>
> String ProductName = “$ProductNamePrefix $formattedDate”

try like this:

```auto
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 ‘+’)

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 29, 2020, 9:04am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/6 "2020-07-29T09:04:52Z")

</div>

> [@anon46315158](#):
>
> def ProductName = “${ProductNamePrefix} ${formattedDate}”

I tried `def`.

Still I got the same result.

```auto
Reason:

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

```

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 29, 2020, 9:11am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/7 "2020-07-29T09:11:08Z")

</div>

> [@anon46315158](#):
>
> by not using the `String` type, groovy should see it as Gstring and perform the interpolation.

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 [The Apache Groovy programming language - Syntax](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:
> 
> ```auto
> "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.

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [July 29, 2020, 9:11am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/8 "2020-07-29T09:11:33Z")

</div>

@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)

```auto
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)

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 29, 2020, 9:17am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/9 "2020-07-29T09:17:20Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [July 29, 2020, 9:21am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/10 "2020-07-29T09:21:51Z")

</div>

> [@kazurayam](#):
>
> The type of left-hand side of `=` ( `def` or `String` ) does not affect to the meaing of the right-hand side of `=` .

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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 29, 2020, 9:33am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/11 "2020-07-29T09:33:03Z")

</div>

> [@anon46315158](#):
>
> i know for sure in 6.x, interpolation worked,

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_.

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [July 29, 2020, 9:39am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/12 "2020-07-29T09:39:59Z")

</div>

> [@kazurayam](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 29, 2020, 9:46am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/13 "2020-07-29T09:46:41Z")

</div>

> [@anon46315158](#):
>
> because is still using an ancient gtk framework.

How brave you are!

---

<div class="post-metadata">

**Author:** ![Jaspreet.mangat](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jaspreet.mangat](https://forum.katalon.com/u/Jaspreet.mangat)\
**Post date:** [July 29, 2020, 11:02am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/14 "2020-07-29T11:02:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [July 29, 2020, 2:55pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/16 "2020-07-29T14:55:06Z")

</div>

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

String UpdatedFirstName = **“** $UpdatedUserName + $formattedDate **”**

Should become:

String UpdatedFirstName = $UpdatedUserName + $formattedDate

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [July 29, 2020, 4:08pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/17 "2020-07-29T16:08:44Z")

</div>

> [@grylion54](#):
>
> String UpdatedFirstName = $UpdatedUserName + $formattedDate

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

```auto
String UpdatedFirstName = UpdatedUserName + ' ' + formattedDate

```

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [July 29, 2020, 4:40pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/18 "2020-07-29T16:40:38Z")

</div>

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

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/e/e730be2cf657ff4c717806ecf25942a8e5d52ad2.png)

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/d/df013ea1f3f11c2c201b3d57f6520349b93fa9a7.png)

Am I missing something?

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [July 29, 2020, 8:02pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/19 "2020-07-29T20:02:23Z")

</div>

@Russ_Thomas please try also:

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

```

And:

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

```

And again:

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

```

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

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [July 29, 2020, 9:25pm UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/20 "2020-07-29T21:25:45Z")

</div>

![image](https://europe1.discourse-cdn.com/katalon/original/2X/6/6a8c750c76d5a2d209984dfbc8c3762629f8135c.png)

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [July 30, 2020, 11:51am UTC](https://forum.katalon.com/t/variables-at-test-case-level/45981/21 "2020-07-30T11:51:11Z")

</div>

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’

[Next page](https://forum.katalon.com/t/variables-at-test-case-level/45981.md?page=2)
