Ask a Question

Script Library

SOLVED
immudreams
Occasional Contributor

Script Library

I am trying to create library which contains the reusuable method. I am thinking of two approach 1. Either to have as part of script library file 2. Groovy class within a project.

 

Either way i need to call the script library/groovy class from certain test steps. I started doing it but i am getting an error "Cannot get property 'testCase' on null object". Below is the code:

 

Script Library code:

 

import com.eviware.soapui.model.testsuite.*

class Example {
def log
def context
def testRunner
def example
// Class constructor with same case as Class name

def execute() {
def writeMe=''
//library = testRunner.testCase.
def search_TestCase = testRunner.testCase.testSuite("CDSLS-1663 Manage Account Authorities Issue").getTestCaseByName("ACC30")
log.info "I am here" + library;
}
}

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

 

TestStep File:

 

def example = context.Example

example.execute()

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisAdams
Frequent Contributor

Re: Script Library

Hi,

 

In terms of a library 'within' SoapUI project, I don't how to do this.

 

But, what I do often do is this....

  • Create a new Groovy class that contains my checking method
  • Within the test step of interest, I add an assertion of the type 'Script Assertion'.  I then create an instance of my Groovy class and call the method.
  • Run my test step, which in turn runs the script assertion and then my groovy class.

Why this way?  Well, when I started testing this API I'm working on, there is one particular service I call a lot.  I initially wrote a script assertion, within SoapUI, which compared the response with the db to ensure values persisted correctly.  The pain was that if the service changed I needed to then update the script assertion, but seeing as I use this service a lot, I then had to use copy and paste to update every script assertion for each test step calling this service.  One day, I got up to about twenty places to change and thought there has to be a better way.  What I found was that you can have a Groovy class in a text file stored outside of SoapUI itself and in the SoapUI folder structure.  I then had to update each test step to call the class.  But, once this was done, any further changes only had to be made in one place - the Groovy class.

 

Here's a few steps on how to do this...

  • Find the script library.  Easiest way to do this is go to Preferences and find the value called Script Library, which will be a path on your machine.  Something like, "\\AppData\Local\Programs\SmartBear\ReadyAPI-2.7.0\bin\scripts".  Think of this as 'root' for you scripts.
  • In 'root' create a folder called 'groovyScripts'.  Note, you can use folders and class paths to organise your scripts, but more later on this.
  • In groovyScripts, create a file called 'MyClass.groovy' and paste in the below code.

 

package groovyScripts

class MyClass {

    def log 
    def messageExchange
    def testRunner

    public MyClass(log, messageExchange){
        this.log = log;
        this.messageExchange = messageExchange;

    }
	
    def testTheLog(){
		log.info "Reference Groovy function file";
	}
	
	def testExchange(){
		log.info(this.messageExchange);
	}
}

 

  • Back to SoapUI, go to the test step of interest and create a Script Assertion.  This will be an empty window...
  • Screenshot 2021-10-06 165824.png
  • The top half is where we usually write Groovy script to test something, but we're going to call our class from here.  The bottom half is for logging.  Note the statement at the top of the form, these available objects allow us to log and interact with the response from the service call.
  • In the top panel, type the following groovy Script...

 

// Instatiate a new object of the Groovy class we created earlier.
// This calls the constructor in our script with the log and messageExchange objects
 myObject = new groovyScripts.MyClass(log, messageExchange);

 // Let's call some methods...
 myObject.testTheLog();
 myObject.testExchange();

 

  • If you click the green 'Go' button, you should see some output like...

 

Wed Oct 06 17:07:43 BST 2021: INFO: Reference Groovy function file
Wed Oct 06 17:07:43 BST 2021: INFO: com.eviware.soapui.impl.wsdl.teststeps.RestResponseMessageExchange@b6e7014

 

 

This shows that we've managed to call our external script.  If you then run the test/test step that contains the script assertion, you should see the same messages in the log at the bottom of the screen.

 

Once you get this far, then we can look at working with the response and making assertions.

View solution in original post

3 REPLIES 3
ChrisAdams
Frequent Contributor

Re: Script Library

Hi,

 

I can help with this, but before I started, I just wanted to check whether you have worked it out or is still needed.

 

Chris

immudreams
Occasional Contributor

Re: Script Library

Nope. Still looking out for help.

ChrisAdams
Frequent Contributor

Re: Script Library

Hi,

 

In terms of a library 'within' SoapUI project, I don't how to do this.

 

But, what I do often do is this....

  • Create a new Groovy class that contains my checking method
  • Within the test step of interest, I add an assertion of the type 'Script Assertion'.  I then create an instance of my Groovy class and call the method.
  • Run my test step, which in turn runs the script assertion and then my groovy class.

Why this way?  Well, when I started testing this API I'm working on, there is one particular service I call a lot.  I initially wrote a script assertion, within SoapUI, which compared the response with the db to ensure values persisted correctly.  The pain was that if the service changed I needed to then update the script assertion, but seeing as I use this service a lot, I then had to use copy and paste to update every script assertion for each test step calling this service.  One day, I got up to about twenty places to change and thought there has to be a better way.  What I found was that you can have a Groovy class in a text file stored outside of SoapUI itself and in the SoapUI folder structure.  I then had to update each test step to call the class.  But, once this was done, any further changes only had to be made in one place - the Groovy class.

 

Here's a few steps on how to do this...

  • Find the script library.  Easiest way to do this is go to Preferences and find the value called Script Library, which will be a path on your machine.  Something like, "\\AppData\Local\Programs\SmartBear\ReadyAPI-2.7.0\bin\scripts".  Think of this as 'root' for you scripts.
  • In 'root' create a folder called 'groovyScripts'.  Note, you can use folders and class paths to organise your scripts, but more later on this.
  • In groovyScripts, create a file called 'MyClass.groovy' and paste in the below code.

 

package groovyScripts

class MyClass {

    def log 
    def messageExchange
    def testRunner

    public MyClass(log, messageExchange){
        this.log = log;
        this.messageExchange = messageExchange;

    }
	
    def testTheLog(){
		log.info "Reference Groovy function file";
	}
	
	def testExchange(){
		log.info(this.messageExchange);
	}
}

 

  • Back to SoapUI, go to the test step of interest and create a Script Assertion.  This will be an empty window...
  • Screenshot 2021-10-06 165824.png
  • The top half is where we usually write Groovy script to test something, but we're going to call our class from here.  The bottom half is for logging.  Note the statement at the top of the form, these available objects allow us to log and interact with the response from the service call.
  • In the top panel, type the following groovy Script...

 

// Instatiate a new object of the Groovy class we created earlier.
// This calls the constructor in our script with the log and messageExchange objects
 myObject = new groovyScripts.MyClass(log, messageExchange);

 // Let's call some methods...
 myObject.testTheLog();
 myObject.testExchange();

 

  • If you click the green 'Go' button, you should see some output like...

 

Wed Oct 06 17:07:43 BST 2021: INFO: Reference Groovy function file
Wed Oct 06 17:07:43 BST 2021: INFO: com.eviware.soapui.impl.wsdl.teststeps.RestResponseMessageExchange@b6e7014

 

 

This shows that we've managed to call our external script.  If you then run the test/test step that contains the script assertion, you should see the same messages in the log at the bottom of the screen.

 

Once you get this far, then we can look at working with the response and making assertions.

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: