Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

Insert Prefix to a Groovy Generated Substring value?

Hi,

 

I have the following groovy script step which grabs a property value in the Properties step (Properties), parses it using a substring like function and writes this substring value to another Properties step (Properties 2)

 

//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[-7..-5]  //this grabs 3 characters the 7th,8th and 9th character

//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("Name", namePropertyvaluePartial)
	

I need somehow to insert a single character BEFORE the substringed value as I'm using this to build a REST request's Query Parameters.

 

I've totally maxed out my coding skills (well - not too sure you can call them 'skills')- could anyone advise?  I need to insert either a '!' or '~' or '^' or '$' immediately BEFORE the substringed value.

 

As always, I appreciate all and any help anyone can provide!

 

I've added the ,json response file for clarity - but I'm unsure if this will help in this regard,

 

nice one,

 

richie

  • Try:

    namePropertyvaluePartial = "~" + namePropertyvaluePartial
    // or
    namePropertyvaluePartial = "~${namePropertyvaluePartial}"

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Can it be explained with an example.

    Reading Name property value of Properties test step. It was mentioned last 5 characters, but using fixed index to read. So, if it can explained with couple of sample values that would helpful.

    Again, there different values mentioned to be prefixed such as !, ^ etc., when to use what? How it should be identified?

    Could not relate this question to attached json.
    • richie's avatar
      richie
      Community Hero

      Hey nmrao 

       

      Ok - sorry - I'm still working on the project which you provided an script assertion on a JDBC test step to extract a DB table's column and it's value and insert it to a property in a Properties step.  This is also the project in which you provided me a script to build and publish a request grabbing Query Parameter and values from the Properties step.

       

       

      My requests have the following pattern:     

      /api/1/{namespace}/{dataset}?QueryParm=value

      As well as the GET request searching for a record that is filtered for a specific QueryParm value (using the syntax pattern above), there is also the requirement for the system to support wildcard searches like the following:

       

      "contains" (where QueryParm value 'contains' a specific value)

      "starts with" (where QueryParm value 'starts with' a specific value)

      "ends with" (where QueryParm value 'ends with' a specific value)

       

      Using the Name attribute (jsonpath = $.['data']['Name']) in the attached json as an example, there are 2 records in there where Name = Richard, and 2 records where the Name = nmrao.

       

      The syntax to specify a 'contains' is to specify the ~ character before the QueryParm value.

       

      SO - if the Name attribute contains records with the Name attribute value of 'Richard', I could use the following request to retrieve those records:

       

      /api/1/{namespace}/{dataset}?Name=~ich

       

      similarly the 'starts with' operator requires the ^ character before the QueryParameter value - so again using "Name":"Richard" as the example, the request would appear as 

       

      /api/1/{namespace}/{dataset}?Name=^Ric

      FinalIy the 'ends with' operator requires the $ character before the QueryParameter value - so again using "Name":"Richard" as the example, the request would appear as 

       

      /api/1/{namespace}/{dataset}?Name=$ard

      The perfect option is updating the Script Assertion you gave me that extracts the Property value from the database - but updating your script assertion was completely beyond my skills - so I thought I might be able to put a bit of groovy together to update the property value AFTER your script assertion had written it to the Properties step (essentially cos I'm trying to not annoy you with all my questions! :)) 

       

      So - currently my Test Case object hierarchy is as follows:

       

      JDBC Request: (query retrieves a single record's column and its associated value and the script assertion writes this to the Properties step)
      Properties: (contains the resultant of the script assertion)
      Groovy REST Request Step: (builds the request, grabbing the contents of the Properties step to build the Query Parameters in my request's query string)
      

      I was thinking of putting an extra groovy script together (with my attempt to grab a substring of the Property value grabbed by your script assertion) to pass this to the groovy script that builds and publishes the request so that my test case hierarchy would be as follows:

       

      JDBC Request: (with script assertion)
      Properties: (contains results of script assertion)
      Groovy step: (containing my attempt to grab a substring of the value stored in 'Properties' and write the substr value out to another Properties step (entitled 'Properties 2')
      Properties 2: (contains the substring value
      Groovy REST Request Step: (builds the request, grabbing the contents of the Properties 2 step to build the Query Parameters in my request's query string)

       

      Obviously if my REST request was using the OTB functionality, it would be easy to prefix the value sourced from a properties step with  a $ or ~ or ^ character - but I can't because I'm using the groovy to build and submit the request.

       

      I've attached an updated .json response file to reflect the Name attribute details I used above

      I've also attached the groovy step code you created to build and publish the request

      I've also attached the script assertion you gave me

       

      I hope I've been a little clearer,

       

      As always I appreciate your time,

       

      richie

  • CharlesHarold's avatar
    CharlesHarold
    Occasional Contributor

    Try:

    namePropertyvaluePartial = "~" + namePropertyvaluePartial
    // or
    namePropertyvaluePartial = "~${namePropertyvaluePartial}"
    • richie's avatar
      richie
      Community Hero

      Hey CharlesHarold 

       

      I'm an idiot - concatenation - exactly what I needed! perfect - thank you!

       

      Cheers,

       

      richie