1

I have keyword groovy which allowed me to generate a dynamic unique ID for test data purpose.

package kw
import java.text.SimpleDateFormat

import com.kms.katalon.core.annotation.Keyword


class dynamicId {

 //TIME STAMP
 String timeStamp() {
  return new SimpleDateFormat('ddMMyyyyhhmmss').format(new Date())
 }

 //Generate Random Number
 Integer getRandomNumber(int min, int max) {
  return ((Math.floor(Math.random() * ((max - min) + 1))) as int) + min
 }

 /**
  * Generate a unique key and return value to service
  */
 @Keyword
 String getUniqueId() {
  String prodName = (Integer.toString(getRandomNumber(1, 99))) + timeStamp()

  return prodName
 }
}

Then I have a couple of API test cases as below:

test case 1:

POST test data by calling the keyword. this test case works well.

the dynamic unique ID is being posted and stored in the Database.

partial test case


//test data using dynamic Id
NewId = CustomKeywords.'kw.dynamicId.getUniqueId'()
println('....DO' + NewId)

GlobalVariable.DynamicId = NewId

//test data to simulate Ingest Service sends Dispense Order to Dispense Order Service.
def incomingDOInfo = '{"Operation":"Add","Msg":{"id":"'+GlobalVariable.DynamicId+'"}'

now, test case 2 served as a verification test case.

where I need to verify the dynamic unique ID can be retrieved by GET API (GET back data by ID, this ID should matched the one being POSTED).

how do I store the generated dynamic unique ID once generated from test case 1?

i have the "println('....DO' + NewId)" in Test Case 1, but i have no idea how to use it and put it in test case 2.

which method should I use to get back the generated dynamic unique ID?

updated Test Case 2 with the suggestion, it works well.

def dispenseOrderId = GlobalVariable.DynamicId
'Check data'
getDispenseOrder(dispenseOrderId)



def getDispenseOrder(def dispenseOrderId){
 def response = WS.sendRequestAndVerify(findTestObject('Object Repository/Web Service Request/ApiDispenseorderByDispenseOrderIdGet', [('dispenseOrderId') : dispenseOrderId, ('SiteHostName') : GlobalVariable.SiteHostName, , ('SitePort') : GlobalVariable.SitePort]))
 println(response.statusCode)
 println(response.responseText)
 WS.verifyResponseStatusCode(response, 200)
 
 println(response.responseText)
 
 //convert to json format and verify result
 def dojson = new JsonSlurper().parseText(new String(response.responseText))
 println('response text: \n' + JsonOutput.prettyPrint(JsonOutput.toJson(dojson)))
 
 assertThat(dojson.dispenseOrderId).isEqualTo(dispenseOrderId)
 assertThat(dojson.state).isEqualTo("NEW")
}

====================

updated post to try #2 suggestion, works

TC2

//retrieve the dynamic ID generated at previous test case
def file = new File("C:/DynamicId.txt")


//Modify this to match test data at test case "IncomingDOFromIngest"
def dispenseOrderId = file.text
'Check posted DO data from DO service'
getDispenseOrder(dispenseOrderId)



def getDispenseOrder(def dispenseOrderId){
 def response = WS.sendRequestAndVerify(findTestObject('Object Repository/Web Service Request/ApiDispenseorderByDispenseOrderIdGet', [('dispenseOrderId') : dispenseOrderId, ('SiteHostName') : GlobalVariable.SiteHostName, , ('SitePort') : GlobalVariable.SitePort]))
 println(response.statusCode)
 println(response.responseText)
 WS.verifyResponseStatusCode(response, 200)
 
 println(response.responseText)
 
}
user2201789
  • 1,083
  • 2
  • 20
  • 45

1 Answers1

1

There are multiple ways of doing that that I can think of.

1. Store the value od dynamic ID in a GlobalVariable

If you are running Test Case 1 (TC1) and TC2 in a test suite, you can use the global variable for inter-storage.

You are already doing this in the TC1:

GlobalVariable.DynamicId = NewId

Now, this will only work if TC1 and TC2 are running as a part of the same test suite. That is because GlobalVariables are reset to default on the teardown of the test suite or the teardown of a test case when a single test case is run.

Let us say you retrieved the GET response and put it in a response variable.

assert response.equals(GlobalVariable.DynamicId)

2. Store the value od dynamic ID on the filesystem

This method will work even if you run the test cases separately (i.e. not in a test suite).

You can use file system for permanently storing the ID value to a file. There are various Groovy mehods to help you with that.

Here's an example on how to store the ID to a text file c:/path-to/variable.txt:

def file = new File("c:/path-to/variable.txt")
file.newWriter().withWriter { it << NewID }
println file.text

The TC2 needs this assertion (adjust according to your needs):

def file = new File("c:/path-to/variable.txt")
assert response.equals(file.text)

Make sure you defined file in TC2, as well.

3. Return the ID value at the end of TC1 and use it as an input to TC2

This also presupposes TC1 and TC2 are in the same test suite. You return the value of the ID with

return NewId

and then use as an input parameter for TC2.

4. Use test listeners

This is the same thing as the first solution, you just use test listeners to create a temporary holding variable that will be active during the test suite run.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • suggestion 1 works. thanks. can i know more details about storing it permanently? or how about katalon test listener? as i afraid that tester might run test cases without the test suite and report failed case. – user2201789 Nov 14 '19 at 09:55
  • I will try to expand on #2 and #4 later in the day. – Mate Mrše Nov 14 '19 at 10:04
  • @user12158726 I added an explanation on #2. – Mate Mrše Nov 14 '19 at 11:19
  • i understand that Katalon has the function /DataFiles . can i use that path ? to store the file variable.txt? @Mate Mrše – user2201789 Nov 15 '19 at 01:50
  • You read the contents of a file with `file.text`. You forgot the `.text` part. Data files is not meant for this kind of stuff. You would usually use it for external data tables and such. – Mate Mrše Nov 15 '19 at 07:36
  • one more question, how do i bind the local file path to Katalon path, so the git repo or docker should work? – user2201789 Nov 15 '19 at 09:11
  • See [here](https://stackoverflow.com/questions/57010304/how-to-get-test-case-name-in-katalon-studio) on RunConfigurations class, it that is what you need. You might try `RunConfiguration.getProjectDir()`. Or use `System.getProperty("user.dir")`. – Mate Mrše Nov 15 '19 at 09:19