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...
  • 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.