Store TestObjects created in Script mode globally

For my project I prefer script mode above Record and Playback because of many iframes involved and object spy not able to capture some objects both in the default or parent frame and the iframe. As a result, I am creating Test Objects in the script mode and subsequently adding the object property and setting it active to be found. I need to store some objects which I created in script mode globally so that in every test case I do not have to create the same object again and again. If there is a way to store the created test objects in script mode in the object repository or global variable/object, it would be good.

Hi there,

We will later implement supports to ‘store the created test objects in script mode in the object repository’. For the other solution you are asking: ‘global variable/object’, you can quickly create a map global variables and put your object values within it. Example:

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject

TestObject to = new TestObject('btn_Login1')
to.addProperty("id", ConditionType.CONTAINS, "login")

'Put TestObject to global variables'
GlobalVariable.G_Objects.put('TestObject', to)

Vinh Nguyen said:

Hi there,

We will later implement supports to ‘store the created test objects in script mode in the object repository’. For the other solution you are asking: ‘global variable/object’, you can quickly create a map global variables and put your object values within it. Example:

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject

TestObject to = new TestObject('btn_Login1')
to.addProperty("id", ConditionType.CONTAINS, "login")

'Put TestObject to global variables'
GlobalVariable.G_Objects.put('TestObject', to)

Hi, Vinh Nguyen,

trying to use your suggestion I could not use the method put() at the GlobalVariable.G_Objects. I imported the correct packages, but it was not available. Do you know the reason? Something related to new versions of Katalon?

The created Global Variables type must be a Map to achieve this solution.

1 Like

Vinh Nguyen said:

The created Global Variables type must be a Map to achieve this solution.

Should the Map support TestObject?

Not sure if I’m doing it correctly, because it is not working yet. :expressionless:

I’m trying to do it inside a custom keyword, but I wonder it’s not the problem.

Global Variable_Map to handle TestObjects.jpg

You are doing this:

GlobalVariable.G_Objects.put('TestObject', to)

This is not right.

You should rather do:

Map m = (Map)GlobalVariable.G_Swap_TestObject
m.put('TestObject', to)

Internally, a GlobalVariable “G_Swap_TestObject” is declared as an instance of java.lang.Object. Therefore you need to explicitly cast a GlobalVariable to an instance of java.util.Map before calling its put method

1 Like

kazurayam said:

You are doing this:

GlobalVariable.G_Objects.put('TestObject', to)

This is not right.

You should rather do:

Map m = (Map)GlobalVariable.G_Swap_TestObject

m.put(‘TestObject’, to)


Internally, a GlobalVariable "G\_Swap\_TestObject" is declared as an instance of java.lang.Object. Therefore you need to explicitly cast a GlobalVariable to an instance of java.util.Map before calling its put method

  

It works, but it seems that Global Variable is not actually updated after execution.

Keyword:

@Keyworddef boolean getMyPageTable(){ 	TestObject flexibleTestObject = new TestObject()	println "[G_Swap_TestObject] Before > " + GlobalVariable.G_Swap_TestObject 	flexibleTestObject.addProperty("xpath", ConditionType.EQUALS, "//*[@id=\"resultado-pesquisa\"]/p-table/div/div[1]/table")	Map m = (Map) GlobalVariable.G_Swap_TestObject	println "[MAP] Before > " + m 	m.put('Test123', flexibleTestObject)	println "[MAP] After > " + m	GlobalVariable.G_Swap_TestObject = m	println "[G_Swap_TestObject] After > " + GlobalVariable.G_Swap_TestObject	return true}

Console Output:

[G_Swap_TestObject] Before > [:][MAP] Before > [:][MAP] After > [:, Test123:TestObject - ''][G_Swap_TestObject] After > [:, Test123:TestObject - '']

But, Global Variables:

katalon variables.png

You expect the GUI to display some new value of G_Swap_TestObject at the underlined position as follows:

No, Katalon Studio won’t do that.

In the \Profiles\default.glbl file, you will find something like this:

<?xml version=1.0" encoding="UTF-8"?>
<GlobalVariabbeEntities>
  <description></description>
   <name>default</name>
   <tag></tag>
   <defaultProfile>true</defaultProfile>
   ...
   <GlobalVariableEntity>
      <description></description>
      <initialValue>[(''):'']</initialValue>
      <name>G_Swap_TestObject</name>
   </GlobalVariableEntity>
</GlobalVariableEntities>

You find a tag name here. Just an initial value. Any runtime updates made to the GlobalVarialbe.G_Swap_TestObject will be forgotten.

One more file to look at. In the \Libs\internal\GlobalVariable.groovy, you will find something like this:

package internal
.../**
 * This class is generated automatically by Katalon Studio and should not be modified or deleted.
 */
public class GlobalVariable {
    ...
    public static Object G_Swap_TestObject    
    static {
        def allVariables = [:]        
        allVariables.put('default',             ['G_Swap_TestObject' : [:], ...])
        String profileName = RunConfiguration.getExecutionProfile()
        def selectedVariables = allVariables[profileName]
        ...        G_Swap_TestObject = selectedVariables['G_Swap_TestObject']
    }
}

You can find here a literal [:] — this is what the GUI shows.
The literal acts as an initial value of a key-value pair in a Groovy variable of type java.util.Map.

GlobalVariable is an automatically generated Groovy Class. GlobalVariable is not a file as data storage nor a database instance. Therefore any updates made runtime to a GlobalVariable will be forgotten after run.

1 Like