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
NaveenKumar
Not applicable

Super Sir!!!... this helped me a lot...
korrrro
Occasional Contributor

Hope somebody still follows this post.

 

Script works fine for me when I use 1 method only in the class, but always when I try to add another one none of them work then and return java.lang.NullPointerException: Cannot invoke method on null object.

 

Any ideas/solutions ?

shalivardhan
New Contributor

Hi , 

 

i got object by step 7 in a script .. do i need to again run step 7 if i want to get object in another test step.. i tried by using context.example but it returning null in another step if i execute step 7 it returning object

 

How to set object into global context 

 

Thank you , 

Vardhan 

 

shalivardhan
New Contributor

 
shalivardhan
New Contributor

Hi MartinSpamer

 

I would like to use that object in multiple scripts without running step 7 again and again how to do that 

 

Thank you , 

vardhan 

chiragverma123
Contributor

I am guessing we cannot reuse groovy scripts in SOAPUI free version?

babusr01
Contributor

Hi Martin,

 

its really helpful , But how can we create groovy jar file and get in into soapui project.

 

Thanks

babu

@babusr01,

 

@rupert_anderson wrote a nice article abou the same here. Hope you can leverage it.



Regards,
Rao.
krogold
Regular Contributor

Hello,

 

Thanks for sharing, it works well but I have one question : how do you manage step 9 (The instance can be added to the Global context if you want to use it across SOAPUI projects.) ?

I would like to use it within one project but I don't even manage to use it between two different test steps (as the context is different), so, how can you store it in the global context so it could be reuse with minimal processing ?

 

thank you

 

Alexandre

Thank you for sharing.

 

Is there any way to use this script from the "script assertion"? 

It does not have a testRunner, only log, context, and messageExchange.

 

Thank you in advance

cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β