Forum Discussion

Pritish_Panda's avatar
Pritish_Panda
Contributor
7 years ago
Solved

Is there any way to get rest resource of a rest step through Groovy.

Hi,

 

I am looking to fetch resource part of a rest step through . which we provide in the resource section .

 

 

Note-: 

 

def testStepNameNew=testRunner.testCase.getTestStepByName("Login")
def endPointInfo=testStepNameNew.getProperty("Endpoint").getValue()

log.info endPointInfo

 

op-: http://IP:PORT

 

But I am looking  for the resource part  .

 

Any kind of help will be really appreciated !!!

 

Thanks 

  • Bill_In_Irvine's avatar
    Bill_In_Irvine
    7 years ago

    Yes. RequestFilter.filterRequest is the correct script and runs before the step executes.

     

    Assuming you know where to edit the requestFilter.filterRequest event (Events menu at top right, + sign to add event, select the requestFilter.filterRequest) here is the code. There is a magic number 24 in it. You need to have a different number from the 24 because it is the exact number of characters that depend on your ip address!!!

     

    log.info "\n\n\n\n"
    log.info "                Begin of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script"
    log.info "\n\n\n\n"
    
    
    
    String therequestUri = new String(context.getProperty("requestUri").toString())
    log.info "therequestUri is " + therequestUri
    log.info "length is "+ therequestUri.length()
    
    def endindex = therequestUri.length() - 1
    def beginindex = 24
    
    String resourcepath = new String(therequestUri.substring(beginindex, endindex))
    
    log.info "context.getProperty(\"requestUri\"): " + context.getProperty("requestUri").toString()
    
    log.info "resourcepath is " + resourcepath
    
    
    log.info "\n\n\n\n"
    log.info "                End of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script"
    log.info "\n\n\n\n"

    Okay now about the logs. It shows up in TestRunner because I use the log.info to print the information. 

    Now here is what I get when I go to SoapUI NG pro tab, click on it so it shows up color blue, right-click on your project, and firs save the project! That also saves the event scripts. Move to the suite level right below that project. Right-click on the test suite. Choose "Launch TestRunner" from the list. The "Launch TestRunner window shows up. For test suite it should be set to the correct test suite. For Testcase you can choose <all> if you want or you can select a particular test case. Then click the "Launch" button.

     

    A window with :"Ready! API TestRunner" opens up and that is where you see the logs. This snippet of the log I got is below.

     

    My result appears below. I modified it before posting it here because I don't want to show our company IP address, etc:

     

    09:37:51,914 INFO  [log]                 Begin of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script
    09:37:51,916 INFO  [log] 
    
    
    
    
    09:37:51,935 INFO  [log] therequestUri is https://xx.x.x.xxx:xxxxx/aaa/bbbbbbbbbbbbb
    09:37:51,936 INFO  [log] length is 42
    09:37:51,975 INFO  [log] context.getProperty("requestUri"): https://xx.x.x.xxx:xxxxx/aaa/bbbbbbbbbbbbb
                                                                                        
    09:37:51,975 INFO  [log] resourcepath is /aaa/bbbbbbbbbbbbb
    09:37:51,975 INFO  [log] 
    
    
    
    
    09:37:51,975 INFO  [log]                 End of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script
    09:37:51,975 INFO  [log] 

    thanks

     

    Bill

7 Replies

  • This prints out the IP with the resource path so you might need to take a slice of a string to get the resource path

     

    log.info context.getProperty("requestUri")

     

    [edited]

     

    You can make this in a groovy script under the SoapUI NG tab for a test cased use the TestSteps tab. I based this on context but you can also use the testCase interface and get to the particular step. In my case I have one test step per test case.

     

    String therequestUri = new String(context.getProperty("requestUri").toString())
    log.info "therequestUri is " + therequestUri
    log.info "length is "+ therequestUri.length()

    def endindex = therequestUri.length() - 1

     

    // I came up with 24 for https://xx.x.x.xxx:xxxxx - if you know the exact ip for your api then this should work for you but 

    // the number of characters for the first index will be different for you
    def beginindex = 24

    String resourcepath = new String(therequestUri.substring(beginindex, endindex))

    log.info "context.getProperty(\"requestUri\"): " + context.getProperty("requestUri").toString()
    log.info "context.getProperty(\"httpMethod\"): " + context.getProperty("httpMethod")

     

    // Display the resource - which is what you want...
    log.info "resourcepath is " + resourcepath

     

    Not sure if that helps any.

    • Bill_In_Irvine's avatar
      Bill_In_Irvine
      Contributor

      Another thing: If you have a unique REST step name you can put this in the requestFilter.beforeRequest. If you don't care about which particular REST step and get the resource path for the current REST step, it will work in that event handler.

    • Pritish_Panda's avatar
      Pritish_Panda
      Contributor

      Hi,

       

      Thanks for your response 

       

      I tried this "log.info context.getProperty("requestUri")" but it returns Null as output.

       

      If I replace Endpoint in place of requestUri then it returns the IP & PORT but I need the information which is added in Resource .

       

      Is requestUri is the correct key to fetch resource URL ?

       

      Problem here is that we are able to fetch any information of a rest step which are added in Custom property but not those which are coming in REST Request Property.

       

      • Bill_In_Irvine's avatar
        Bill_In_Irvine
        Contributor

        I think you might have to do this in the requestFilter.filterRequest event handler and not in the testStep groovy script.

         

        I was successful with the snippet of code (I posted above) in my requestFilter.filterRequest Groovy event handler. But I too, had problems with trying to get this done in a Groovy script inside the test case.

         

        The property name Endpoint omits the resource. The resource does not seem to show up in the available property names list. The RawRequest is empty. 

         

         

        For instance I put this in my test case groovy script and got null output:

        String rawrequest = new String(testCase.getTestStepByName("Request 1").getPropertyValue("RawRequest").toString())
        
        log.info "rawrequest is " + rawrequest
        10:08:48,415 INFO  [log] rawrequest is null

         

         

        However requestFilter.filterRequest - if it has a Groovy Script, executes after any test case script. The problem is it runs for each test case. I have not gone to any specific test but if you give your test step a unique name you can get the resource from the requestFilter.filterRequest. There might be a way to dig out the testRunner interface by an import statement if you want to drill down to the particular test while in the requestFilter.filterRequest script.

         

        Bill