About Groovy scripting

defsoapUI_String = "soapUI Tutorial";

/* Do some processing on the soapUI_String variable here */

log.info(soapUI_String);

We can make all this into a function and call it when required. Doesn't that sound good? To do this, first we need to create a class and make the above tasks into a member function. Lets create a Groovy class now.

package soapUIScripts
class Scripts
{
    def log;
    Scripts(log)
    {
      this.log = log;
     }
defsoapUI_groovy_function()
 {
defsoapUI_String = "soapUI Tutorial";
  /* Do some processing on the soapUI_String variable here */
  log.info(soapUI_String); 
 } 
}

Create a folder with the name of the package (in this case "soapUIScripts") and place the above code into a file and save inside the folder with name "Scripts.groovy" We have created a groovy class and now we need to include the above class into soapUI for us to use it. In soapUI Pro, go to "soapUI Preferences -> soapUI Pro -> Script Library" mention the path of the parent folder (name of the folder which has the folder "soapUIScripts"). We can't have just the functions inside the groovy file, it should always be attached to a class. soapUI is all set to use your custom code now. Navigate to any soapUI test case and create a groovy script test step and use the below code inside that step to call the above created function.

defmyscript = new soapUIScripts.Scripts(log);

myscript.soapUI_groovy_function();

If you are using any soapUI built in objects in your groovy class/ script, make sure you are first passing that as a constructor. The same way we passed the "log" object to our class in the above example.

If you are not using soapUI Pro you will need to compile your scripts into a standard java library (ie .jar file) and add that file to the classpath by putting it in the bin\ext folder to use the functions.