Forum Discussion

Pritish_Panda's avatar
Pritish_Panda
Contributor
8 years ago

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") ...
  • Bill_In_Irvine's avatar
    Bill_In_Irvine
    8 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