Guna Yuvan
Guna Yuvan

Reputation: 101

How to create GlobalVariable during runtime in katalon using scriptmode?

I m new to katalon studio tool and trying to add GlobalVariable during runtime by taking "variable Name" & "Value" as inputs. I m come across block of code which will help me in this. But i m facing difficulties in understanding this piece of code,because it uses metaprogramming in Groovy.

void addGlobalVariable(String name, def value) {
    MetaClass mc = script.evaluate("internal.GlobalVariable").metaClass
    String getterName = "get" + name.capitalize()
    mc.static."$getterName" = { -> return value }
    //mc.static."$name" = value
}

i am getting "InvocationTargetException" when i m running it.and also katalon IDE doesn't recognize these keywords "script","evaluate" & static.

you looking for clarity on this block code or you can suggest me any other possible solution.

Upvotes: 2

Views: 1662

Answers (2)

graddy
graddy

Reputation: 21

After my test, the following code can achieve the effect: 1.Define a Keyword:

import com.kms.katalon.core.annotation.Keyword
package com.becelever.util
public class GlobalVariableUtils {

    @Keyword
    static void addGlobalVariable(String name, def value) {
        GroovyShell shell1 = new GroovyShell()
        MetaClass mc = shell1.evaluate("internal.GlobalVariable").metaClass
        String getterName = "get" + name.capitalize()
        mc.'static'."$getterName" = { -> return value }
        mc.'static'."$name" = value
    }
}

2.then invoke it and verify:

CustomKeywords.'com.becelever.util.GlobalVariableUtils.addGlobalVariable'('localURL', 'katalon.com')

println(GlobalVariable.localURL)
println(GlobalVariable.getLocalURL())

Note: I found that the first letter of the variable could not be capitalized. For example, if I changed it to "LocalURL", it failed.

Upvotes: 1

Dobromir Manchev
Dobromir Manchev

Reputation: 21

Auto-import the required libraries. You do this by pressing Ctrl+Shift+O.

Upvotes: 0

Related Questions