Can we create a dynamic Global Variable

Hi Community,
Just wanted to understand if it is possible to have a dynamic Global Variable in Profile.

I need to use a Global Variable for CurrentDateTimeStamp in multiple places in my script.
Date and Time will keep changing with every passing day.
So is it feasible to have some logic in Global Variable, so that without touching the Global Variable it will get updated by itself before running any scripts?

1 Like

No and yes.

There is no such magic. If a Global Variable is updated without any explicit instruction, we would rather call it a bug.

But you can write a statement that updates the GlobalVariable in a Test Listener rather than in the Test Case scripts.

In a Test Listener, you want to write a single line of code that updates the Global Variable as CurrentDateTimeStamp. Then the updated value will be observable for every Test Case scripts.

Thank you @kazurayam.
I have never used listeners before but on reading the documentation, it looks interesting.
For a start can you please help me with the above date timestamp code?

put the below code in your custom keyword

package customKeywords

import com.kms.katalon.core.annotation.Keyword
import java.text.SimpleDateFormat

class DynamicVariables {
    @Keyword
    static String getCurrentDateTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
    }
}

after this , use the below to call above in your test case

import customKeywords.DynamicVariables as DynamicVars

String currentTimestamp = DynamicVars.getCurrentDateTime()
println("Current Timestamp: " + currentTimestamp)
1 Like

TestListener

import java.text.SimpleDateFormat

import com.kms.katalon.core.annotation.BeforeTestSuite
import com.kms.katalon.core.context.TestSuiteContext

import internal.GlobalVariable

class MyTestListener {
	
	@BeforeTestSuite
	def sampleBeforeTestSuite(TestSuiteContext testSuiteContext) {
		GlobalVariable.CurrentDateTimeStamp = getCurrentDateTime()
	}
	
	static String getCurrentDateTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
	}
	
}

TC1:

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

import internal.GlobalVariable as GlobalVariable

WebUI.comment("TC1: ${GlobalVariable.CurrentDateTimeStamp}")

TC2:

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

import internal.GlobalVariable

WebUI.comment("TC2: ${GlobalVariable.CurrentDateTimeStamp}")

You want to create a Test Suite TS1 which comprises with TC1 and TC2.

Run the TS1:

Please find the same timestamp value is displayed by TC1 and TC2

1 Like

Thank you so much @kazurayam.
I was just wondering if we can do it at Test case level also.
Your current script ran and gave the same Timestamp for both the scripts because it was enabled at Test Suite level I guess.
The actual Timestamp when running the second Test Case might be different.
Will the Timestamp automatically update if we do it at testcase level?
Like TC1 Time stamp 14:01
TC2 Timestamp 14:05

Also, if there is any possibility to get A.M/P.M format based on the actual Time?

Thanks @Monty_Bagati.
Really appreciate your help.

I don’t understand what you mean by “automatically”. Nothing could happen “automatically”.

Yes, Please try.

… in that case, you don’t need TestListener at all.

Maybe you don’t need any GlobalVariable. TC1 and TC2 could have local variables as timestamp inside each.

1 Like

Yes.

Read the following article and try:

You should ask Google first.

1 Like

Hi @kazurayam ,

I did try the Test Listener as advised.

When i try to run this in Test case, I am getting below error:

Please advise.

If you look at @kazurayam’s image of his program above in the very first line, there is an import statement that you need as well.

import java.text.SimpleDateFormat