Store generated dynamic unique ID and parse to next test case

冷暖自知 提交于 2020-01-23 17:53:26

问题


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)
	
}

回答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.



来源:https://stackoverflow.com/questions/58850889/store-generated-dynamic-unique-id-and-parse-to-next-test-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!