Forum Discussion

jayantjayant's avatar
jayantjayant
New Contributor
5 years ago

Calling custom Groovy script from Script Assertion in REST request

Hi All,

 

I'm new to Soap UI and Groovy and possibly this is a noob question.

I have a requirement where I want to execute a custom groovy script that will perform some comparision and logic based on parameters passed, FROM a Script Assertion in a REST request.

 

Is there any possibility to do this? I tried to add a Groovy script with my code in the Test step and tried to write a logic to try and call the script from the Script assertion of a REST request. But I'm unable to get it to work.

  • nmrao's avatar
    nmrao
    Champion Level 3
    The question is very generic and difficult to understand unless specifics of the issue is provided with some sample data.

    You may also find previous posts to see if you can find some more ideas.

    You said tried some thing. Would you like to share that?
    • richie's avatar
      richie
      Community Hero
      Hi jayantjayant,

      Just reiterating what Rao said to provide a little perspective from someone else that has little groovyscript experience (me!) :)

      To provide helpful diagnosis of scripting issues the scripters on the forum will nearly always need to see your code (unless your query is very basic and can be resolved with a couple of lines of code).
      The scripters on this forum are always a real big help, but they need more to go on that what youve initially described, but as a quick response to your question, yes its entirely feasible to do a comparison of some of your response against something else via a script assertion.

      Cheers,

      Rich
      • sonya_m's avatar
        sonya_m
        SmartBear Alumni (Retired)

        Hi jayantjayant, while you are working with nmrao and richie on this, just some words of encouragement - don't ever be afraid to sound like a noob in the Community. We are all learning and this is perfectly normal not to know something. Sharing experience is the primary goal of this forum:smileyhappy:

    • jayantjayant's avatar
      jayantjayant
      New Contributor

      Thanks for your response Rao,

       

      I am using below groovy code as an assertion script for a test step.

       

      //get the path of the project root
      def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
      def projectPath = groovyUtils.projectPath
      log.info(" projectPath : "+projectPath)

      //get the test suite name
      def testSuiteName_appName = messageExchange.modelItem.testStep.testCase.testSuite.getName()
      log.info(" testSuiteName_appName : "+testSuiteName_appName)

      //get the test case name
      def tcaseName_dashboardName = messageExchange.modelItem.testCase.getName()
      log.info(" tcaseName_dashboardName : "+tcaseName_dashboardName)

      //get the test step name
      def testStepName = messageExchange.modelItem.testStep.getName()
      log.info(" testStepName : "+testStepName)

      if(execution_mode == "testExecution" ) {

      File file = new File("/tmp/Assertion_Files/"+testSuiteName_appName+"/"+tcaseName_dashboardName+"/"+testStepName+".txt")
      BufferedReader br = new BufferedReader(new FileReader(file));
      String fileStatement;
      def newString = "";
      while ((fileStatement = br.readLine()) != null) {
      newString = newString + fileStatement.toString();
      }

      def expected_data_in_file = newString.trim().replace(' ', '');
      expected_data_in_file = expected_data_in_file.replace("\n", "").replace("\r", "");

      def splunkRestApiResponse = context.response.toString().trim().replace(' ', '');
      splunkRestApiResponse = splunkRestApiResponse.replace("\n", "").replace("\r", "");

      log.info(" splunkRestApiResponse "+splunkRestApiResponse);
      log.info(" fileResponse trimmed "+expected_data_in_file) ;

      assert(splunkRestApiResponse.equals(expected_data_in_file));
      log.info(" Assertion Passed ") ;
      }

       

      It is working fine. As this is a generic code and all of my teststeps have same groovy assertion script. Is there a way to put this script at one place and then call it from each assertion script. This will help me to reduce my effort whenever there is a change in groovy script.

       

      • richie's avatar
        richie
        Community Hero
        Hi jayantjayant,

        I think i remember Radford sets up libraries and stuff like that so he might be able to suggest some options.

        Ive got a possiblity but i dont know if it is feasible. You "might" be able to add an event handler to do ehat you need, maybe TestRunListener.afterStep option with your script and using the 'Target' column in the events window to target specific test steps (give the teststeps in each testcase that need the assertion identical names....then you add in this teststep name into the Target column).

        That way youd only need to add your code into the event handler editable field once and you wouldnt need to add it anywhere else.

        Youd have to change your code slightly cos i think the events uses testRunner rather than messageExchange (script assertions dont have access to testRunner which is why youre using messageExchange instead).

        The only problem i can foresee with this is reporting the assertion failure. I dont know how this would/could work, but i thought id mention this option just in case it can be done with a little effort. Id be interested to see if it would work myself.

        Nice one,

        Rich