cancel
Showing results for 
Search instead for 
Did you mean: 

How to run SoapUI API commands in an external script

SOLVED
Highlighted
Regular Contributor

How to run SoapUI API commands in an external script

Does anyone know how to run SoapUI commands from an external script?

 

For example if I have an external script that does an evaluation based on an XML response and if I get the wrong response I want the test to fail how do you add testRunner.fail()

 

Here is an example of a class I have sitting in my script library. I can call it and pass inputs to it but I can't get it to actually fail the test case.

 

package library.serviceAvailabilityResult

import org.apache.commons.lang.RandomStringUtils

class ServiceAvailabilityResult {

	String getServiceAvailabilityResult(String result) {

		String serviceAvailabilityResult

		if (result == "yes") {
			serviceAvailabilityResult = result
		} else if (result == "no") {
			testRunner.fail('SERVICE IS NOT AVAILABLE')
		}
	}
}

 

I have many tests that rely on the "serviceAvailabilityResult" and would rather not have to create a groovy step in each one to do this one thing in case I need to add to it or make changes in the future I would rather have only one place for maintenance purposes.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super Contributor

Re: How to run SoapUI API commands in an external script

@msiadak, you shouldn't need to import the entire library.

 

Reworking the original Class file a bit (I've made the getServiceAvailabilityResult method static for ease of calling, but I understand that this is just a cut down example so this may not be possible with the actual script):

 

package library.serviceAvailabilityResult

class ServiceAvailabilityResult {

	static String getServiceAvailabilityResult(testRunner, String result) {

		def serviceAvailabilityResult = ''

		if(result == "yes") {
			serviceAvailabilityResult = result
		}else if(result == "no"){
			serviceAvailabilityResult = 'SERVICE IS NOT AVAILABLE'
			testRunner.fail(serviceAvailabilityResult)
		}else{
			serviceAvailabilityResult = 'Unrecognised input result parameter value "' + result + '"'
		}
		
		return serviceAvailabilityResult
	}
}

 

I could then call this from a test case setup script, like so:

 

import library.serviceAvailabilityResult.ServiceAvailabilityResult

// The variable inputString must be 'yes' or 'no'
def inputString = 'no'
def returnString = ServiceAvailabilityResult.getServiceAvailabilityResult(testRunner, inputString)

log.info('ServiceAvailabilityResult.getServiceAvailabilityResult = ' + returnString)

 

with the test case being failed as expected.

 

@jkrier, if your requirement for having an external script was to just have one script to maintain I would still recomend that you investigate Ready APIs Event functionality.

View solution in original post

6 REPLIES 6
Highlighted
Super Contributor

Re: How to run SoapUI API commands in an external script

The "testRunner" object you are referring to (along with others, like log, context, etc) are provided by Ready API, to the scripts written/run within it's script editors, here is the documentation for the Groovy test step, with their details (Note: different Ready API Groovy editors will provide subtly different objects).

 

While I fully understand your requirement for a single external script, you can't just "call" these internally provided objects from external scripts.

 

I see one of two possible solutions:

 

  1. Rework your external script function (this function actually being a method of a class) to pass in a reference to the testRunner object, as I assume that you will be calling this external script from an internal location that provides the testRunner object, probably from a test case setup script.
  2. Use the Ready API Events functionality. Here you can write a single script that is triggered on an event, for example "Before a test case runs". The advantage of this is that it is an internal script that has access to the testRunner object.

Personally I use (and would recommend) Events (option 2) to achieve the single script requirement. For example I have a script that runs before each test case that validates my assertions have content (I have a habit of adding blank assertions and forgetting to complete them Smiley Happy). These events can be targeted to a limited subset of items, see the linked documentation page for details.

Highlighted
Community Hero

Re: How to run SoapUI API commands in an external script

@Radford is correct. You can't really expect to interface properly with ReadyAPI functionality without importing their entire library into your external script. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Super Contributor

Re: How to run SoapUI API commands in an external script

@msiadak, you shouldn't need to import the entire library.

 

Reworking the original Class file a bit (I've made the getServiceAvailabilityResult method static for ease of calling, but I understand that this is just a cut down example so this may not be possible with the actual script):

 

package library.serviceAvailabilityResult

class ServiceAvailabilityResult {

	static String getServiceAvailabilityResult(testRunner, String result) {

		def serviceAvailabilityResult = ''

		if(result == "yes") {
			serviceAvailabilityResult = result
		}else if(result == "no"){
			serviceAvailabilityResult = 'SERVICE IS NOT AVAILABLE'
			testRunner.fail(serviceAvailabilityResult)
		}else{
			serviceAvailabilityResult = 'Unrecognised input result parameter value "' + result + '"'
		}
		
		return serviceAvailabilityResult
	}
}

 

I could then call this from a test case setup script, like so:

 

import library.serviceAvailabilityResult.ServiceAvailabilityResult

// The variable inputString must be 'yes' or 'no'
def inputString = 'no'
def returnString = ServiceAvailabilityResult.getServiceAvailabilityResult(testRunner, inputString)

log.info('ServiceAvailabilityResult.getServiceAvailabilityResult = ' + returnString)

 

with the test case being failed as expected.

 

@jkrier, if your requirement for having an external script was to just have one script to maintain I would still recomend that you investigate Ready APIs Event functionality.

View solution in original post

Highlighted
Community Hero

Re: How to run SoapUI API commands in an external script

@Radford That's a great point, and looks like a thorough explanation! 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Regular Contributor

Re: How to run SoapUI API commands in an external script

Thanks to @msiadak and @Radford I appreciate your time and assistance with this. I am able to fail the test case now with the external script.

 

As far as using Events. I already do these fairly extensively but I have to add them to every project that I have. Doing this in an event would essentially be the same as adding a Groovy Script step wouldn't? Since I would have to add the event to every project. Not all of my test cases that use the ServiceAvailabilityResult are in the same project. We have many projects with different test cases that use this.

Highlighted
Community Hero

Re: How to run SoapUI API commands in an external script

@Radford and others,
If I think what is this method is achieving or value add that bring to the table? I see nothing unless user has some other things in mind and add some more code later to it.

In this specific case, User is passing "yes" to this method and method is returning the same result back without any business logic based on the code, at least, for the moment. It does not make sense, IMO.

 

I do not suggest / advocate something which I do not follow. For the above requirement,if I have to do it, use onliner instead of external and method and suggests the same to the community.

 

That is why, it was suggested in other thread to use something like below and avoid class in this case or tell the use in a broader way so that other idea be though of.

 

In the groovy script it self it is one line which is equivalent to call method

 

String result = 'no'

(result == 'yes') ?: testRunner.fail('Service is not running')

 

//or

assert result == 'yes', 'Service is not running'

 

 

I do promote or advocate to write classes and reuse if it suites case such as mentioned below thread(s).

 

https://community.smartbear.com/t5/SoapUI-NG/Getting-MissingMethodException-while-calling-Groovy-cla...

 

https://stackoverflow.com/questions/43254590/how-to-place-the-groovy-in-centralized-groovy-library-a...

 

https://community.smartbear.com/t5/SoapUI-NG/Call-Java-class-with-variable-in-groovy/m-p/140335/high...

 

 



Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Script Wanted
Let's start July with new challenges!

Create a script to change request body before sending it to the server for the TechCorner tag!


Scripts created in June:
- A script to generate a request body based on data from Datasource
- A script to go through all response nodes and check the value of the required field
- A Dispatching script to get responses based on the values in Request Body
- A script to add a new tag and assign it to all TestCases in a TestSuite
Top Kudoed Authors