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
rakeshpqaeng
New Member

Good article... Really helpful.. Thanks.. Appreciated your help

cancel
Showing results for 
Search instead for 
Did you mean: