Ask a Question

Trying to retrieve a property based on the value of another property in the Properties test step

SOLVED
hazel_chua
Contributor

Trying to retrieve a property based on the value of another property in the Properties test step

I have 2 Properties test steps set up in one of my test suites.  And right now I need to retrieve values from the 2nd Properties test step based on a serial number from the first Properties test step.

 

So, in my groovy script, I am processing through the information from the first Properties step which is something like : 

Row 1 : 1

Serial Number 1 : 12345

Time 1 : <timestamp 1>

Row 2 : 2

Serial Number 2 : 12346

Time 2 : <timestamp 2>

Row 3 : 3

Serial Number 3 : 12345

Time 3 : <timestamp 3>

whereas in Property Step 2 it looks like :

Row 1 : 1

Serial Number 1 : 12345

Count 1 : 0

Row 2 : 2 

Serial Number 2 : 12346

Count 2 : 0

Row 3 : 3

Serial Number 3 : 12347

Count 3 : 0

 

Now, I need to update the Count value of Properties step 2 whenever I encounter the serial number in Properties step 1.  Is there any way to do that without it getting messy..??  Also, the properties, when loaded, are sorted based on other fields, not based on the serial numbers.

1 ACCEPTED SOLUTION

Accepted Solutions
hazel_chua
Contributor

Re: Trying to retrieve a property based on the value of another property in the Properties test step

eventually this is what I ended up doing : 

 

 

	if(incCount == true)
	{
		//Count the number of times an account number is used for financial transactions on the day and 
		//updated the "Count" in the Settlements property step
		def propertySettlements = testRunner.testCase.getTestStepByName("Settlements")
		paddingNum = propertySettlements.getPropertyValue("DataRowCount").length()
	
		for(def testProperty in propertySettlements.getPropertyList())
		{
			if(accnum in testProperty.value)
			{
				def padFlag = testProperty.name.substring(testProperty.name.length()-paddingNum)
				int newVal = propertySettlements.getPropertyValue("Count " + padFlag).toInteger()
				newVal++
				propertySettlements.setPropertyValue("Count " + padFlag, newVal.toString())
			}
		}
	}

 

 

View solution in original post

6 REPLIES 6
HimanshuTayal
Community Hero

Re: Trying to retrieve a property based on the value of another property in the Properties test step

@hazel_chua :

 

Hope below code will help you out in resolving your issue:

 

//For getting count from Properties step 1
def propertiesStep = testRunner.testCase.getTestStepByName("Properties")
HashMap<String,Integer> map = new HashMap<String,Integer>();
for(testProperty in propertiesStep.getPropertyList()){
	if(testProperty.name.contains("Serial")){
		if(map.containsKey(testProperty.getValue()) )
			map.put(testProperty.getValue(), map.get(testProperty.getValue())+1)
		else{
			map.put(testProperty.getValue(), 1)
		}
	}
}
//For setting count in Properties step 2
def propertiesStep1 = testRunner.testCase.getTestStepByName("Properties1")
for(testProperty in propertiesStep1.getPropertyList()){
	
	for(Map.Entry entry in map.entrySet()){
		if(entry.getKey() in testProperty.value){
			flag = testProperty.name.substring(testProperty.name.length()-1);
			propertiesStep1.setPropertyValue("Count "+flag.toString(),entry.getValue().toString())
		}
	}
}

Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
hazel_chua
Contributor

Re: Trying to retrieve a property based on the value of another property in the Properties test step

That isn't quite what I was after but it gives me something to try out.  Will update this when I get the chance to think through that and perhaps adapt the code to better suit what I need.

 

Thanks

HimanshuTayal
Community Hero

Re: Trying to retrieve a property based on the value of another property in the Properties test step

@hazel_chua : What's your use case then could you please brief where have you stuck


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
hazel_chua
Contributor

Re: Trying to retrieve a property based on the value of another property in the Properties test step

Apologies @HimanshuTayal, been busy with other parts of work to get back to this.  What I am trying to do here is that while the script iterates through the properties of PropertyStep1, it updates the matching 'Count' property of the 'Serial Number' in PropertyStep2 with the new value based on the Serial Number that is encountered in PropertyStep1

 

Edit again : I just thought of another interesting quandary.  What if the 'Count' property was added to PropertyStep2 after the property step was created, so that now PropertyStep2 looks like (as an example) :

Row 1 : 1

Serial Number 1 : 12345

Row 2 : 2

Serial Number 2 : 12346

Row 3 : 3

Serial Number 3 : 12347

Count 1 : 0

Count 2 : 0

Count 3 : 0

appliedMagic
Occasional Contributor

Re: Trying to retrieve a property based on the value of another property in the Properties test step

Hello,

this little script should do what you described.

1) It goes over every Serial Number in PropertyStep2 and checks if there is an appropriate Count entry, e.g. Serial Number 5 should have Count 5 etc.. If a Count entry is missing it will create one and set it to 0.

2) It will iterate over every Serial Number in PropertyStep1 and increase the Count variable in PropertyStep2. If a Serial Number cannot be found, in PropertyStep2, it will create the appropriate Serial Number entry and also create the Count entry.

 

Script:

def teststepa = context.testCase.getTestStepByName('Properties.A')
def teststepb = context.testCase.getTestStepByName('Properties.B')
def propertiesa = teststepa.getProperties()
def propertiesb = teststepb.getProperties()

// init, with value 0, or add properties named Count if necessary
propertiesb.findAll{it.getKey().contains("Serial")}.each {
	def num = it.getKey().replace('Serial', '').replace('Number', '').trim()

	if(propertiesb.keySet().contains('Count ' + num)) {
		def val = (propertiesb['Count ' + num].getValue())

		if(val == null || val.trim().isEmpty()) {
			teststepb.setPropertyValue('Count ' + num, '0')
		}
	} else {
		teststepb.setPropertyValue('Count ' + num, '0')
	}
}

//only needed if we miss, somehow, the appropriate Serial Number entry in properties B
def cnt = 1

// iterate all serial number properties, in properties A, and increment count in properties B
propertiesa.findAll{it.getKey().contains("Serial")}.each { pa ->
	def key = propertiesb.find { it.getValue().getValue() == pa.getValue().getValue() }?.getKey()

	if(key == null) {
		while(propertiesb.keySet().contains('Count ' + cnt) || propertiesb.keySet().contains('Serial Number ' + cnt)) {
			cnt++
		}

		teststepb.setPropertyValue('Serial Number ' + cnt, pa.getValue().getValue())
		teststepb.setPropertyValue('Count ' + cnt, '1')
	} else {
		def num = key.replace('Serial', '').replace('Number', '').trim()
		def val = 0

		//just so that we do NOT run into invalid entries
		try {
			val = Integer.valueOf(propertiesb.find { it.getKey() == 'Count ' + num }.getValue().getValue())
		} catch(Exception e) {
			val = 0
		}

		teststepb.setPropertyValue('Count ' + num, (++val).toString())
	}
}


log.info('done')

 

TestSuite setup:

screenshot.c.png

 

 Test run, before script is run:

screenshot.a.png

 

Test run, after script is run:

screenshot.b.png

hazel_chua
Contributor

Re: Trying to retrieve a property based on the value of another property in the Properties test step

eventually this is what I ended up doing : 

 

 

	if(incCount == true)
	{
		//Count the number of times an account number is used for financial transactions on the day and 
		//updated the "Count" in the Settlements property step
		def propertySettlements = testRunner.testCase.getTestStepByName("Settlements")
		paddingNum = propertySettlements.getPropertyValue("DataRowCount").length()
	
		for(def testProperty in propertySettlements.getPropertyList())
		{
			if(accnum in testProperty.value)
			{
				def padFlag = testProperty.name.substring(testProperty.name.length()-paddingNum)
				int newVal = propertySettlements.getPropertyValue("Count " + padFlag).toInteger()
				newVal++
				propertySettlements.setPropertyValue("Count " + padFlag, newVal.toString())
			}
		}
	}

 

 

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: