Groovy Substr Method - Is it Possible To Parameterize the Left /Right Hand Value?
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'
Search when Name attribute in database does NOT contain the characters 'chi'
Search when Name attribute in database starts with the characters 'Ric'
Search when Name attribute in database ends with the characters '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"]
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.
'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
I'm not entiirely certain what 'namePropertyValue' actually contains, but if it's something like "/api/1/geographical-indication/product-group?Name=~chi", then this will get everything after the equals sign:
def namePropertyvaluePartial = namePropertyvalue.substring( namePropertyvalue.indexOf( "=" ) )