Reusable Script Library for SOAPUI

I've found the need to define a common Library of reusable Groovy Scripts with SOAPUI, there isn't a tutorial or established examples on how this can be achieved so here is an example of how I achieved 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 [b]module-name[/b].

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")

8) 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.


soapui_reusable_script_library.txt ยท Last modified: 2014/08/28 14:50 by Martin Spamer