Forum Discussion

MartinSpamer's avatar
MartinSpamer
Frequent Contributor
13 years ago

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.

    • korrrro's avatar
      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 ?

  • 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 

     

  • 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 

  • Hi Martin,

     

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

     

    Thanks

    babu

  • krogold's avatar
    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

    • denizov23's avatar
      denizov23
      Frequent Visitor

      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