Forum Discussion

ajay1902's avatar
ajay1902
Occasional Contributor
8 years ago

how to extract "Rest Request properties" via groovy Script from Rest Test step

I need help in trying to extract the Rest Request Properties associated with a Rest Request test step in ReadyAPI via Groovy Script.

 

Scenario -

want to extract the Resource property listed under Rest Request Properties. The Resource path property available under Rest request property then needs to be parameterized so that it can be updated at run time by using a project parameter and support two or more versions of code base by modifying one of the path parameters at run time. I have more than 30 APIs so cant go manually and change the Resource path to a parameterized.

 

Using groovy script I am able to extract the custom parameters but not able to extract the Rest Request properties. 

 

Can someone please help in guiding with a solution or pointing to an existing answer.

 

I did found lot of questions asked by other users on extracting Custom properties from the request but could not find anything related to Rest Request properties.

  • Well one of the things you requested was how to extract the REST Request properties. 

     

    You can do this in a groovy script in the RequestFilter.filterRequest event handler

     

    https://community.smartbear.com/t5/SoapUI-Pro/Is-there-any-way-to-get-rest-resource-of-a-rest-step-through/m-p/147648#M33627

     

    Now, you say you want to parameterize the resource path. You have 30 APIs. You mean in https://xxx.xxx.xxx.xxx:xxxxx form? It might make sense to use the Environment capability to switch between APIs. 

     

    You can use the Environement to set the custom project property for the resource of that API. Then in Groovy use the property expansion to set the resource My link above "gets" the resource. 

     

    From the context module you probably can use setProperty. You can also drill down to the test case to set the request when everything is all ready. I add stuff to the body of the request and do something  the following in a Groovy test step (not the event handler) 

    def json = …
    def restRequest = testRunner.testCase.getTestStepByName(“my test step name“);
    
    // Set the json for the test step request
    restRequest.setPropertyValue('Request', json.toString())
  • Well one of the things you requested was how to extract the REST Request properties. 

     

    You can do this in a groovy script in the RequestFilter.filterRequest event handler

     

    https://community.smartbear.com/t5/SoapUI-Pro/Is-there-any-way-to-get-rest-resource-of-a-rest-step-through/m-p/147648#M33627

     

    Now, you say you want to parameterize the resource path. You have 30 APIs. You mean in https://xxx.xxx.xxx.xxx:xxxxx form? It might make sense to use the Environment capability to switch between APIs. 

     

    You can use the Environement to set the custom project property for the resource of that API. Then in Groovy use the property expansion to set the resource My link above "gets" the resource. 

     

    From the context module you probably can use setProperty. You can also drill down to the test case to set the request when everything is all ready. I add stuff to the body of the request and do something  the following in a Groovy test step (not the event handler) 

    def json = …
    def restRequest = testRunner.testCase.getTestStepByName(“my test step name“);
    
    // Set the json for the test step request
    restRequest.setPropertyValue('Request', json.toString())
    • ajay1902's avatar
      ajay1902
      Occasional Contributor

      Thanks Bill_In_Irvine , I was able to solve the first part of the issue using the solution to get the Resource using URI.

       

      Coming to  your query on "You have 30 APIs. You mean in https://xxx.xxx.xxx.xxx:xxxxx form? It might make sense to use the Environment capability to switch between APIs".

       

      What I meant to say is that I have currently 2 version of codes running in my environment due to certain limitations. The only way to target those is by changing one of the path parameters. example -

      http://192.168.0.0:1000/abc/v1/api/1232

      http://192.168.0.0:1000/abc/v2/api/1232

       

      So if you look at the above URLs the only difference is in the resource V1 and V2. Since all the APIs initially didn't parameterize this variable in resource path so I need to now automate this in order to avoid heavy rework across 1000 cases setup.

      Currently trying to use the Context.setProperty method suggested to updating the URI during run time. will keep you posted on the same. Thank!

       

      Update -

      used the below code -


      log.info submit.request.getPath()
      submit.request.setPath("/product/api/v4.0/foo")
      log.info submit.request.getPath()


      from the below solution to change my path parameter as required -

      https://community.smartbear.com/t5/SoapUI-Pro/Resolved-Dynamically-set-resource-path-of-WADL-at-runtime/td-p/40930

       

      Many thanks for the help!

      • Bill_In_Irvine's avatar
        Bill_In_Irvine
        Contributor

        you're welcome.

         

        Your http://192.168.0.0:1000 is the request URI part up to “/abc” and can be fetched using getProperty(“requestURI”). If you know the exact amount of characters, (i.e. your address and port number stays constant) you can trim off the resource from it using slice or substring operations.

         

        Your endpoint in your environment under REST Services would be http://192.168.0.0.1000

         

        It seems there should be a way to read in the part of the resource path that varies. Like from a file and index it like 1 to 30.

         

        Using string concatenation you might get closer to the solution by

         

        String newresource = “/abc/“ + whichV[index] + “/api/1232”

         

        Then you might be able to reconstruct your requestURI perhaps using context.setProperty(“requestURI”)