free seo tool

Groovy Script Library in soapUI

Do you know in soapUI we can bundle all the frequently used scripts/ functions into a script library and use it where ever we want without duplicating the code? Lets take a look into this topic. Lets assume we need to use the below block of groovy script in many places like soapUI groovy test steps, soapUI assertions etc.

def soapUI_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;
    }
def soapUI_groovy_function()
{
def soapUI_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.

def myscript = 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.
8 comments:
  • Anonymous says:
    May 24, 2012

    This is one more fantastic from you.

    Only drawback I see is absolute path of the script library. There should have been easy mechanism to store script library in a relative path.

  • Admin says:
    May 31, 2012

    Thank you..

  • Swati says:
    June 07, 2012

    Hi Admin,

    I am unable to do it with non pro edition. I would really appreciate if you can explain me above mention topic with non pro edition of Soap. I have tried compiling my script into .jar file but it is giving me some error on console that expecting EOF ' '

    Please help !!!!

    Thanks,
    Swati

  • Swati says:
    June 08, 2012

    Admin Please help me I am able to paste the jar file to bin/ext of soap ui but when am creating object of that class it works fine however on calling a method like obj.newMethod () this method has log.info ("my name is swati")

    but whenever am calling this method it says there is no property for class : log it seems .... I dont know whats wrong here as groovy automatically supports log.info....Please help me admin am stuck here.... :(

  • Admin says:
    June 08, 2012

    Hi Swati,

    I'm not sure if you are passing your log object as a constructor to the class in the jar. when you create an object, you should pass the log object to the class via a constructor to access it later. See if the code above in this post, how its being called. Or post the statement here that you are using to create your object, then I might be able to guide you better.

  • Swati says:
    June 11, 2012

    Dear Admin

    Am able to log the info after passing the log object in constructor however there are lot of other objects that I want to keep in this class and use it globally like alert.showInfoMessage , getCell , getWorkbook , getContents in these cases how would I call the methods. I would really appreciate if you can start a new thread explaining all these conditions as I tried a lot calling XL methods like workbook, Cell , getContents and alert but no success :(

    Please share ur thoughts Admin!!!

    Thanks as always!!!

    Swati

  • praveen says:
    October 01, 2012

    Hi Admin,

    Before clarifying the doubt, i would appreciate your very valuable help on the SOAP UI tool. Thanks for all these help.

    I tried this script which worked well initially. later i modified the def soapUI_String = "welcome to soap UI"; and tried
    executing. still i saw the output "SOAP UI tutorial".

    please help me why it is not taking the modified input.


    Thanks
    Praveen.

  • Unknown says:
    November 14, 2015

    This works great and I'm now doing all sorts of things with Class objects we couldn't do with the freeware version. Thanks so much for the simple and clean tutorial. We will be getting our Pro licenses soon. Good stuff!

Post a Comment

This is a new website mainly for SOA Testers using soapUI. Let me know your thoughts/ suggestions.