Forum Discussion

jkrier's avatar
jkrier
Regular Contributor
7 years ago
Solved

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.

  • Radford's avatar
    Radford
    7 years ago

    groovyguy, 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.

6 Replies

  • Radford's avatar
    Radford
    Super Contributor

    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 :smileyhappy:). These events can be targeted to a limited subset of items, see the linked documentation page for details.

    • groovyguy's avatar
      groovyguy
      Champion Level 1

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

      • Radford's avatar
        Radford
        Super Contributor

        groovyguy, 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.