Forum Discussion

richie's avatar
richie
Community Hero
7 years ago

Looping Test Case - write out REST response value from each loop for use later

Hi,

 

I'm struggling to actually describe what I need - it is a property transfer - but I want the values to persist after each loop to be used later.

 

I have a test case that sources .json files from a directory datasource - has a couple of POSTs in it, then loops around to use the next file in the directory.  A value in the first POST is used in the second POST - which is straightforward property transfer - this I can do.

 

Test Case hierarchy is as follows:

 

Datasource (Directorytype) sourcing multiple .json files (fileContents property)
POST1 (${DataSource #fileContents}
PropertyTransfer (${POST1#phonecallid})
Properties (phonecallid = 1234567)
POST2 (${Properties#phonecallid})
Datasource Loop

 

HOWEVER -as well as the phonecallid, there is another unique reference (entityid) that I need to grab from the POST1 request each time it executes for later use (I've simplified the test case quite a bit), so that if I have 10 .json files in my Datasource, I get 10 entityid values written somewhere.  If I do this as a propertytransfer, the value will be overwritten each loop iteration.

 

I need some way to grab the entityid value from each loop and save it somewhere for later (properties step, testsuite property, maybe a flat file) - but as I say - if do just a normal property transfer - the value will be overwritten each loop iteration.

 

have I just said the same thing twice?  told you I was struggling to explain!

 

I hope I've explained myself clearly!

 

thanks to all for any advice/guidance/tips/help anyone can provide!

 

richie!

  • richie, ideal solution is to store enityids in a test suite property as a comma separated values. I will update the post if I find a better solution.

     

    For now,  you could store entity id values in a flat file. 

     

    import net.sf.*
    import net.sf.json.*
    import groovy.json.JsonSlurper
    
    //## Get Directories ##//
    def projectPath = context.expand('${projectDir}')
    
    //Parse json response
    def response = context.expand( '${RESTRequest#Response}' )
    def json = new JsonSlurper().parseText response
    
    //get entityId record from json response
    def entityId = json.entityid
    
    log.info "creating EntityID file..."
    //## Populate entityId data file ##//
    def entityIdFile = projectPath + "\\entityId.txt"
          entityData = new File(entityIdFile)
    
     log.info "entityID from the rest request is - $entityId"
     log.info "Writing entityId to datafile..."
     entityData.append("$entityId" +'\r\n')

      It will be nice to clear the file once you execute desired testcase or testSuite. (Probably in testSuite or project teardown script or a separate groovy step)

     

     

    //## Get Directories ##//
    def projectPath = context.expand('${projectDir}')
    
    log.info "Clearing EntityID file..."
    
    def entityIdFile = projectPath + "\\entityId.txt"
          entityData = new File(entityIdFile)
          entityData .delete() // Clear File Contents //

     

     

     

  • New2API's avatar
    New2API
    Frequent Contributor

    richie, ideal solution is to store enityids in a test suite property as a comma separated values. I will update the post if I find a better solution.

     

    For now,  you could store entity id values in a flat file. 

     

    import net.sf.*
    import net.sf.json.*
    import groovy.json.JsonSlurper
    
    //## Get Directories ##//
    def projectPath = context.expand('${projectDir}')
    
    //Parse json response
    def response = context.expand( '${RESTRequest#Response}' )
    def json = new JsonSlurper().parseText response
    
    //get entityId record from json response
    def entityId = json.entityid
    
    log.info "creating EntityID file..."
    //## Populate entityId data file ##//
    def entityIdFile = projectPath + "\\entityId.txt"
          entityData = new File(entityIdFile)
    
     log.info "entityID from the rest request is - $entityId"
     log.info "Writing entityId to datafile..."
     entityData.append("$entityId" +'\r\n')

      It will be nice to clear the file once you execute desired testcase or testSuite. (Probably in testSuite or project teardown script or a separate groovy step)

     

     

    //## Get Directories ##//
    def projectPath = context.expand('${projectDir}')
    
    log.info "Clearing EntityID file..."
    
    def entityIdFile = projectPath + "\\entityId.txt"
          entityData = new File(entityIdFile)
          entityData .delete() // Clear File Contents //

     

     

     

  • Datasource (Directorytype) sourcing multiple .json files (fileContents property)
    POST1 (${DataSource #fileContents}
    PropertyTransfer (${POST1#phonecallid}) ---> ADD ENTITYID TO THIS TEST STEP
    Properties (phonecallid = 1234567)
    POST2 (${Properties#phonecallid})
    -- ADD A DATASINK TEST STEP HERE Datasource Loop

    Why not just add a DataSink test step and append all your entity Ids in a text file.  You can then use the text file later as a datasource test step in subsequent tests if you need to pull up the entity ids. 

     

    The entityId is refreshed within each loop .. but you're also recording the value in the text file created in the DataSink test step each time you loop through ..

     

    Make sure to check "append to existing file" in the DataSink test step. 

     

    • richie's avatar
      richie
      Community Hero

      WooHoo!

       

      Cheers to both mpartyka & New2API

       

      playing with both solutions now!

       

      Really appreciate your help guys,

       

      richie

  • nmrao's avatar
    nmrao
    Champion Level 3
    Can you explain with respective data for better understanding?
  • Datasource (Directorytype) sourcing multiple .json files (fileContents property)
    POST1 (${DataSource #fileContents}
    PropertyTransfer (${POST1#phonecallid}) ---> ADD ENTITYID TO THIS TEST STEP
    Properties (phonecallid = 1234567)
    POST2 (${Properties#phonecallid})
    -- ADD A DATASINK TEST STEP HERE Datasource Loop

    Why not just add a DataSink test step and append all your entity Ids in a text file.  You can then use the text file later as a datasource test step in subsequent tests if you need to pull up the entity ids. 

     

    The entityId is refreshed within each loop .. but you're also recording the value in the text file created in the DataSink test step each time you loop through