Forum Discussion

GMSoapUI's avatar
GMSoapUI
Contributor
12 years ago

[Resolved] Unable to get centralised script working.

I am having difficulty in centralising a small routine for padding a string with a set number of characters.

It is called from a TestRunListener.afterStep event handler as follows:

log.info( soapui.demo.stringPad.fncPadString( " ", 7 ) + " TEST RESULT" );


My global property for scripts is set at the default of C:\Program Files (x86)\SmartBear\SoapUI-Pro-4.6.2\bin\scripts.

Within this folder location I have a folder 'soapui' and within that a folder called 'demo' (as per the default installation of SoapUI Pro). Within the 'demo' folder I have a .groovy file called 'stringPad' and it contains the following code:

package soapui.demo

class stringPad
{
def strChar
def intLength

stringPad( strCharacterToPadWith, intLengthToPadBy)
{
strChar = strCharacterToPadWith;
intLength = intLengthToPadBy;
}

def static fncPadString( strCharacterToPadWith, intLengthToPadBy )
{
strPadString = "";
for( int intCount = 0; intCount < intLengthToPadBy; intCount++ )
{
strPadString = strPadString + strPadCharacter;
}
return strPadString;
}
}


However, when I run the code that calls this code the SoapUI Log reports that soapui is an unknown property of the class. I followed the instructions here: http://www.soapui.org/Scripting-Properties/scripting-and-the-script-library.html but just cannot seem to get this working. What have I missed please?
  • Hi,

    This a syntax problem with the script you posted. Certain variables were not declared and the class was not instantiated in the event handler.

    I am able to use C:\Program Files (x86)\SmartBear\SoapUI-Pro-4.6.2\bin\scripts as the global script folder and run the script you posted with some modifications.


    package soapui.demo

    class stringPad
    {
    def strChar
    def intLength

    /*stringPad( strCharacterToPadWith, intLengthToPadBy)
    {
    strChar = strCharacterToPadWith;
    intLength = intLengthToPadBy;
    }*/
    static fncPadString( strCharacterToPadWith, intLengthToPadBy )
    {
    def strPadString = "";
    for( int intCount = 0; intCount < intLengthToPadBy; intCount++ )
    {
    strPadString = strPadString + strCharacterToPadWith;
    }
    return strPadString;
    }
    }


    Event handler TestRunListener.afterStep:

    def greet = new soapui.demo.Greet( "Ole", log )
    greet.salute()
    def sp = new soapui.demo.stringPad()
    log.info sp.fncPadString( "a ", 7 )


    Should produce the following in the script log:
    Thu Dec 12 11:22:14 EST 2013:INFO:Hello Ole
    Thu Dec 12 11:22:14 EST 2013:INFO:a a a a a a a


    As an FYI we do not support custom code solutions.



    Regards,
    Marcus
    SmartBear Support
  • Hi Marcus,

    Thanks for the reply and apologies, I didn't know you don't support custom code solutions. This seems slightly strange though since I would have thought a large number of posts to this forum would have been Groovy related. The API docs online I find quite confusing and it is difficult to grasp the applicable context and object model from these alone. Is there a 'community board' that I can direct these to in future?

    I'll try your solution and once again, thanks for the reply.

    Regards.
  • Hi,

    You can post questions like this in the Pro forum. That is fine, I'm just saying from a support agreement point of view as an FYI that we don't support custom code solutions.

    Were you able to get the script I posted yesterday working?


    Regards,
    Marcus
    SmartBear Support
  • Hi,

    Yes I was able to get it working. One thing that puzzles me a little is that in the class I have:
       def strChar
    def intLength

    Yet these are seemingly never used - why are they here at all?

    Regards.
  • Hi,

    Those variables where in the constructor (which I commented out) but wasn't being used anywhere in the script so you can remove those if you want.



    Regards,
    Marcus
    SmartBear Support
  • Presumably I didn't need the constructor as I was using the static definition? It's been a long time since I looked at OO programming. Presumably only one class can reside in a .groovy file too i.e. I couldn't, for instance, keep all my centralised functions in one .groovy file under a class called libraries and define all my bespoke functions within that class?
  • Hi,

    I removed the contstructor because I didn't think it was needed for that script. Please see this link and the section under static classes. This question falls under general groovy questions, and you should consult the groovy.codehaus.org site for more information.

    http://groovy.codehaus.org/JN2525-Classes



    Regards,
    Marcus
    SmartBear Support