Generate date in a groovy and to make it available in the project so that can be re-use

Hello, i would like to know how we can create groovy scripts for auto-generate dates values and to make it available so that can be re-used across the project.

Could you please help me to know where this can be done?

Andrej Podhajský could you please have a look?

there is few options you have:
1. make java/groovy lib with functions for generate values
2. make custom keywords with functions for generate values

where to call it? it depends on you, but to share them between TC, @TestSuiteSetup is quite good place

image.png

Just for your interest. You can get the timestamp of a test suite run.

import java.nio.file.Pathimport java.nio.file.Pathsimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIclass TL {	    static Path reportDir    static {        // for example, reportDir = C:/Users/username/katalon-workspace/projectName/Reports/TS1/20180618_165141        reportDir = Paths.get(RunConfiguration.getReportFolder())    }    @BeforeTestSuite    def beforeTestSuite(TestSuiteContext testSuiteContext) {        def testSuiteTimestamp = reportDir.getFileName().toString()    // e.g., '20180618_165141'        WebUI.comment("${testSuiteTimestamp}")		    }}

This TestListener will display something like “20181005 170304”; this is the timestamp when a test suite is started. You can save this date into some GlobalVariable to share by test cases.

Andrej Podhajský kazurayam

How can i put this code to make it work?

import java.text.SimpleDateFormat

def call(){

def date = new Date()

sdf = new SimpleDateFormat(“MM/dd/yyyy”)

return sdf.format(date)

}

import java.text.SimpleDateFormat
def call(){
def date = new Date()
def sdf = new SimpleDateFormat("MM/dd/yyyy")
return sdf.format(date)
}
println call()

console:

10-05-2018 01:00:38 PM - [START]  - Start Test Case : Test Cases/__Sandbox/New Test Case10-05-2018 01:00:39 PM - [INFO]   - Evaluating variables for test case10-05-2018 01:00:39 PM - [START]  - Start action : Statement - println(call())10-05-2018 01:00:39 PM - [END]    - End action : Statement - println(call())10-05-2018 01:00:39 PM - [START]  - Start action : Statement - date = new java.util.Date()10-05-2018 01:00:39 PM - [END]    - End action : Statement - date = new java.util.Date()10-05-2018 01:00:39 PM - [START]  - Start action : Statement - sdf = new java.text.SimpleDateFormat(MM/dd/yyyy)10-05-2018 01:00:39 PM - [END]    - End action : Statement - sdf = new java.text.SimpleDateFormat(MM/dd/yyyy)10-05-2018 01:00:39 PM - [START]  - Start action : Statement - return sdf.format(date)10/05/201810-05-2018 01:00:39 PM - [END]    - End action : Statement - return sdf.format(date)10-05-2018 01:00:39 PM - [PASSED] - Test Cases/__Sandbox/New Test Case10-05-2018 01:00:39 PM - [END]    - End Test Case : Test Cases/__Sandbox/New Test Case

Thank you for the solution, however where should i past that code to make it like a global variable?

so :
1. create global variable in profile of your choice (e.g. default) and name it MyGlobalTimestamp
2. i assume you will set it on TestSuite level - that means 1 timestamp for all TC executed in that test suite- then open your test suite, click on sctipt tab and replace @SetUp part with following

/** * Setup test suite environment. */@SetUp(skipped = false) // Please change skipped to be false to activate this method.def setUp() {		def date = new Date()	def sdf = new SimpleDateFormat("MM/dd/yyyy")	GlobalVariable.MyGlobalTimestamp = sdf.format(date)	}

don’t forget to add

import java.text.SimpleDateFormat

to imports on beginning of that script

this will setup global variable every time you run your TC in that test suite

Thank you and i will try this and let you know…Have a nice weekend

to you too