Add items in List with GlobalVariables

Hi all,

It’s impossible for me to add datas when running a test.

A person has already succeeded? If so, what was the solution?

I tried GlobalVariables.test.add (“test”) it does not work.

thanks in advance

Hi,
You can use

def list = []
list.add("test")
GlobalVariable.test = list

But the value will never be displayed in the profile

Thank you @HeleneB

The list saves the additions?

at each end of test cases in a test suite, I want to add a data item in my GlobalVariable list.

I then have a TestListener that will iterate on this list.

So, on each TC, use

def list = GlobalVariable.list as List
list.add("something")
GlobalVariable.list = list

@Dino for a persistent solution,which will survive across testsuites,you can also export the needed data to an external file

Yes I agree @Ibus .

However, I wanted to avoid reading and writing to an external file since there is this option with GlobalVariables.

I will soon test the method of @HeleneB and come back to you.

For information, currently I stock my datas in a GlobalVariables type “String”. In the Test Listener I parse the data with a “,”

you may want to think twice.
accumulating data in global variables means accumulating data in memory (more objects spawned).
with external file, you move the pressure on IO devices. a carefully written method will just work beautiful, grab_ that/save_that/release_mem. take care not to spawn unneeded IO requests.

just saying …

I understand what you mean. thanks @Ibus for your advices

Just try this in your test case:

List ls = (List)GlobalVariable.test
ls.add("test")

And in your Test Listener,

    @AfterTestCase
	def sampleAfterTestCase(TestCaseContext testCaseContext) {
		List ls = (List)GlobalVariable.test
		for (item in ls) {
			println "item is \'${item}\'"
		}
	}

You need to explicitly cast a GlobalVariable of type java.lang.Object into java.util.List before calling .add("test").

But why?

In the project you would find a file <projectDir>/Libs/internal/GlobalVariable.groovy.

The file would look something like this:

package internal

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.main.TestCaseMain


/**
 * This class is generated automatically by Katalon Studio and should not be modified or deleted.
 */
public class GlobalVariable {
     
    /**
     * <p></p>
     */
    public static Object test
     
...

This file was a Groovy class generated by Katalon Studio based on the “Profiles > default” settings. Please note that the GlobalVariable.test is declared as an instance of java.lang.Object; not as a java.util.List. This is the whole reason why you need to explicitly cast the GlobalVariable.test in the Test Case and the Test Listener.

1 Like

Hi,

I confirm that it works

thank you

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.