Groovy Substr Method - Is it Possible To Parameterize the Left /Right Hand Value?
Hi,
I am testing a GET REST request which equates to certain db operations - hence in each of my tests i am performing a REST request with various parameters/operations and then proving the GET REST request worked correctly by following it with a JDCB step, comparing the JDBC results to the GET results to prove the GET worked correctly.
The GET request includes operators ('contains', 'not contains', 'starts with', 'ends with') to filter the GET request Query Parameters.
e.g. Say the 'Name' attribute in the database holds a record with Name value of 'Richie'
Search when Name attribute in database contains the characters 'chi'
/api/1/geographical-indication/product-group?Name=~chi
Search when Name attribute in database does NOT contain the characters 'chi'
/api/1/geographical-indication/product-group?Name=!chi
Search when Name attribute in database starts with the characters 'Ric'
/api/1/geographical-indication/product-group?Name=^Ric
Search when Name attribute in database ends with the characters 'hie'
/api/1/geographical-indication/product-group?Name=$hie
I've found a substring kindof function in groovy and I've created the following snippet which grabs a string from a property value saved in the Properties step, parses the property value and creates a substring from the original string value saved in the Properties step and then writes out this new substring value to another Properties step (Properties 1)
//next line parses the Properties step and gets the value associated with the Name property def namePropertyvalue = testRunner.testCase.getTestStepByName("Properties").getPropertyValue("Name") //parse the Name value and grab the last 5 chars and assign them to the namePropertyvaluePartial variable def namePropertyvaluePartial = namePropertyvalue[-5..-1] //write out the value associated with the namePropertyvaluePartial variable //log.info(namePropertyvaluePartial)
//define the properties step and write the updated substring value to the Properties 1 step
def propertiesStep = context.testCase.testSteps["Properties 1"]
propertiesStep.setPropertyValue("Substrvalue", namePropertyvaluePartial)
So my test case object hierarchy is as follows:
'JDBC Step' (queries the database with a filter to retrieve a single record - nmrao's script assertion writes the resultset - which is a single column's value to the 'Properties' step for later use.
'Properties' step
'Groovy Script' step (substring snippet (displayed above) parses the value in Properties step and grabs certain characters to create a substring and writes it to the following Properties 1 step)
'Properties 1' step
'Groovy Script' to build and submit a GET request based on certain parm values (script again courtesy of Rao) including the substring value set in the 'Properties 1' step
The left hand value in the substr array function [-16..-1] defines the starting position and the right hand value defines the ending position for the substring - BUT I need to know the number of characters in the value in the Properties step, to set this value correctly - if I get it wrong, I get an indexoutofbounds exception is generated.
The reason why I'm trying to parameterize this is that my tests need to work against different environnments and the different databases have different records - so the JDBC step at the start of the test which generates the value in the initial Properties step will not always be the same.
I've attached an example response .json file
Cheers,
richie