Forum Discussion

mrarkapravo's avatar
mrarkapravo
Contributor
3 years ago

how delete multiple generated ids one by one using delete endpoint

I am creating 5 records using Post end points, then after creating those ids I want to delete them one by one using delete end point just for clean up , so how can I achieve it?

 

So here, I cannot delete multiple IDs in a single request.

 

While creating the records, storing them in a property as object as below-

{"id1":"124","id2":"234","id3":"345" }

 

Tried it with groovy, where I am reading those IDs from the property using "Data Source" and then trying to use them for delete using loop, but problem is the loop is going infinitive and for delete always taking only 1st ID i.e "id1":"124".

It would be great if anyone can give me solution .

 

  • ChrisAdams's avatar
    ChrisAdams
    3 years ago

    Hi,

    Regarding your specific questions...

    - "The second step (Prep for Delete) is where I get the values and call the delete step for each...step" - Yes, exactly that.

    - "when I am trying to delete there how will it pick the ID ?" - You can alter the URL and/or payload for a step dynamically.

     

    Using your example of https://xx.xx.xxxx.com/{id}?permanent=true, we can change this per iteration in the "Prep for Delete - Groovy Script"

     

    E.g. you can change the whole endpoint by doing....

     

    def dynamicUrl = "https://xx.xx.xxxx.com/myId?permanent=true";
    testStep.properties['Endpoint'].value = dynamicUrl;

     

    Or, as I probably think more appropriate, you can just adjust the request parameters...

    testStep.setPropertyValue(paramName, paramValue);

     

    Which when applied to my earlier script looks like...

    import groovy.json.*;
    
    def ids = testRunner.testCase.getPropertyValue( "idsCreated");
    
    // Use JSON Slurper to create objects.
    def jsonSlurper = new JsonSlurper();
    def idsObj = jsonSlurper.parseText( ids.toString() );
    
    // We now have an object, but we don't know the keys.  Let's get them...
    def idsObjKeys = idsObj.keySet();
    log.info(idsObjKeys);
    
    // Iterate over the keys...
    for ( key in idsObjKeys) {
    	
    	def value = idsObj[key];
    	log.info("key ${key}.  value ${value}");
    
    	// Get the step.
    	def testStep = testRunner.testCase.getTestStepByName("Delete - REST Request");
    
    	// Set the Id property to the current 'value' in loop.
    	testStep.setPropertyValue("id", value);
    
    	// Run the step with update params
    	testStep.run(testRunner, context);
    	log.info("Ran delete for key ${key} using id ${value}.");
    }
    
    // Let's clear the Custom Property for future runs...
    testRunner.testCase.setPropertyValue( "idsCreated", "");
    
    

     

    You should now be able to update your own Delete test step.

     

    • richie's avatar
      richie
      Community Hero
      Hey mrarkapravo,

      You dont need groovy to do this.

      You can use dataSource loop to handle this. Om typing this on my phone, but i'll provide basic test step hierarchy with explanation when on my laptop

      Cheers

      Rich
      • mrarkapravo's avatar
        mrarkapravo
        Contributor

        Hi richie ,

         

        Thank you for your reply ,could you please explain ?

        dataSource loop use to loop from and to, but how will it help to loop in picking IDs one by one from property (here I am storing the generated ids as key and value pair as explained above) and send it to delete endpoint?

  • Holden's avatar
    Holden
    Occasional Visitor

    Thanks for this post i was willing to post it and i found this I am creating 4 records using Post end points and i had the same question.

    Myloweslife