Ask a Question

How to write a reusable script Library

MartinSpamer
Frequent Contributor

How to write a reusable script Library

I've found the need to define a common Library of reusable Groovy Scripts for use with SOAPUI community edition, there isn't a tutorial or established examples on how this can be achieved so here is an example of how I did this.

1) Define a new TestSuite in the root of your project called Library.
2) Disable the Library TestSuite so that it doesn't get run in an uncontrolled manner.
3) Define a TestCase under Library, I name this after the module-name.
4) Define a Groovy Script, I give this the name of the Groovy Class that is going to contain my reusable code.

 

class Example {
def log
def context
def testRunner
// Class constructor with same case as Class name
def Example(logIn,contextIn,testRunnerIn) {
this.log = logIn
this.context = contextIn
this.testRunner = testRunnerIn
}
}


5) Add the reusable code as method, pass parameters as necessary.

def execute(message) {
// do some stuff to prove I've run with right context, etc.
log.info testRunner
log.info context
log.info "return "+message
return message
}


6) We need to instance of the class; add the following to the end of the Script, outside the class definition. This will place the instance in the project's context.

context.setProperty( "example", new Example( log, context, testRunner) )
log.info "Library Context:"+context


7) You can now reuse the instance in any Groovy Script, with the following Groovy code.

// get a reference to the library TestSuite
library = testRunner.testCase.testSuite.project.testSuites["Library"]

// find the module within the library
module = library.testCases["module-name"].testSteps["Example"]

// initialise the library; which places an instance of Example in the context
module.run(testRunner, context)

// get the instance of example from the context.
def example = context.example

// run the method, with parameter
log.info "example.execute() = " + example.execute("Tester")


😎 Add more modules, classes or methods as necessary.
9) The instance can be added to the Global context if you want to use it across SOAPUI projects.

30 REPLIES 30
tjdurden
Frequent Contributor

Great tutorial Martin, thanks for sharing.

Found this reply useful? Please endorse me on LinkedIn
anewbie
Occasional Contributor

Hi Martin!

Thanks from me too. You made my day
anewbie (Anya)
Sandeepss
Occasional Contributor

Hi Martin,

Thanks a lot for sharing this code.
Here i am facing some error can you any one help me out plz..

Here my flow goes

i have created as per above code a Suite and Class in that as below.... called Library File

class Example
{
def execute(message)
{
context.utils = new com.eviware.soapui.support.GroovyUtils(context)
def response = context.expand( '${GetGeoIP#Response}' )
context.holder = context.utils.getXmlHolder(response)
context.values = context.holder.getNodeValues("//ReturnCode")
return context.values
}
}


context.setProperty( "example", new Example() )




and below is my Calling Script which calls above method execute. as below

library = testRunner.testCase.testSuite.project.testSuites["Library"]

module = library.testCases["LibTestCase"].testSteps["Groovy Script"]

module.run(testRunner, context)

def example = context.example

log.info example.execute("hi")

and when try to exucute i got the below error plz let me know the solution for this and how can i resolve it..
Error as below


Tue Feb 05 11:42:23 IST 2013:ERROR:groovy.lang.MissingPropertyException: No such property: context for class: Example
groovy.lang.MissingPropertyException: No such property: context for class: Example
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:86)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at Example.execute(Script8.groovy:21)
at Example$execute.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script10.run(Script10.groovy:17)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:96)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:148)
at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:274)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
MartinSpamer
Frequent Contributor

1) You are missing the Constructor, step 5
2) check the following line, the "Groovy Script", earlier you say you called it "Library File".


module = library.testCases["LibTestCase"].testSteps["Groovy Script"]


3) If that doesn't work, add the following lines after they are assigned


assert library != null
assert module != null


When you post code use the BB Code markup to make it easier to read.
[code][/code]
Sandeepss
Occasional Contributor

Hi Martin,

Sorry for the code pasting.

here i have attached my project file have look.

Steps as below :-
Test Suite Name = LibraryFile
Test Case Name = LibTestCase
Groovy Script Library 1st File Name = LibraryScripts
GroovyScript Name Which calls methods.functions from Library 1st GScript = Groovy Script


Groovy Script Library 2nd File Name = LibraryScripts_2
GroovyScript Name Which calls methods.functions from Library 1st GScript = Groovy Script_2


Here in the 1st library file and i have added simple code just returns the value.

in second lib file there my actual need. there i am getting as above.

Please have look at the project and do the needful...


Thanks in Advance...


Regards,
Sandeep S S
MartinSpamer
Frequent Contributor

The properties and Constructor cannot be removed and must be passed into the Constructor.


class Geo
{
def log
def context
def testRunner

def Geo(logIn, contextIn, testRunnerIn)
{
this.log = logIn
this.context = contextIn
this.testRunner = testRunnerIn
}

def execute()
{
log.info "inside"
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def response = this.context.expand( '${GetGeoIP#Request}' )
def response_holder = groovyUtils.getXmlHolder(response)
def response_values = response_holder.getNodeValues("//web:IPAddress")
log.info response_values.toString()
return context.values
}
}
context.setProperty( "geo", new Geo( log, context, testRunner) )


Also the LibraryScript and the "Groovy_Script" that references it should be in separate test suites.
Sandeepss
Occasional Contributor

Hi Martin,

You are real Dude man..
Thanks a lot for your time..
this is just great... it got work for me...

Thanks again...


Regards,
Sandeep S S
ameehl
New Contributor

Martin,

Have you found a way to add parameters to the constructor?

So:

def constructor(someString, contextIn, testRunnerIn, logIn){
this.context = contextIn;
this.testRunner = testRunnerIn;
this.log = logIn;
log.info someString;
}

...

context.setProperty( "Template", new Template(someString, context, testRunner, log) ); ?

------------------

module.run("foo", testRunner, context); ?

def example = context.Template("foor"); ?

------------------

I've tried the shown method, and have had no success.

Thanks,
-Drew
saraflores
New Contributor

Hi!

I have to make a global script libary to reuse code through all projects. I tried using script folder as the User Guide, but I have problems to modify the project properties. Do anybody know how to do it?

Anyway, I'm goint to try this solution. But I dont know how can I make the step 9 (The instance can be added to the Global context if you want to use it across SOAPUI projects). Can anybody help me?

thanks in advance
cancel
Showing results for 
Search instead for 
Did you mean: