Forum Discussion

sreelokesh's avatar
sreelokesh
Occasional Contributor
9 years ago

Updating SLA time assertion for all the test cases in a project

Hi,

 

My current project structure look like this .. 

 

Project 

   -- Test suite 1

         -- 10 Test cases

   -- Test suite 2

          -- 20 Test cases

 

....

 

I had given assertion as 2sec for all the 250 test cases, I would like to know if I can change the assertion value for all test cases using a groovy script to 5sec instead of changing assertion value for each individual test case.

 

Thanks,

Lokesh

  • Groovy Approach

    Note that though it is tested in a limited environment, have a copy of your original soapui project before running this script. This will modify the value of Response SLA assertion for most of the types which supports Response SLA i.e., HTTP, SOAP, REST and JDBC test requests

     

     

    Add a Groovy Script step in the same project, in any test case. and copy the below and run the script:

     

    Like it is mentioned earlier reply, it assigns a project level custom property RESPONSE_TIME_SLA as value of Response SLA assertion. So, you need to define the same project level property and put a value that is needed and save the project.

     

    Of course, if you are executing the project using SOAPUI_HOME/bin/testrunner.bat/.sh utility, and the there is network latency you can still increase the value from the command-line itself (without even require to modify the value in project) by passing "-PRESPONSE_TIME_SLA=500" along with testrunner command line options.

     

    You can also find it here

    https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/UpdateResponseSLA.groovy

     

    /**
     * This script updates value of Response SLA assertion in the soapui project
     * with value mentioned for 'newSLA' variable below which 
     * currently assigns project level custom property call RESPONSE_TIME_SLA and
     * you need to define it with required value for the request steps of type
     * SOAP, REST, JDBC, HTTP
     */
    import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
    import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
    import com.eviware.soapui.impl.wsdl.teststeps.JdbcRequestTestStep
    import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep
    import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.ResponseSLAAssertion
    //update the new value as needed
    def newSLA = '\${#Project#RESPONSE_TIME_SLA}'
    //Get the project object
    def project = context.testCase.testSuite.project
    
    //Closure to update the Response SLA assertion value
    def updateAssertionSLA = { assertion, sla ->
    	if (assertion instanceof ResponseSLAAssertion) {
    		log.info "Found a request step assertion with Response SLA type, and updating its value"
    		assertion.setSLA(sla)
    	}
    }
    //Actual script that traverse thru the project -> suite -> case -> step
    project.testSuiteList.each { suite ->
    	log.info "Looking into test suite: ${suite.name}"
    	suite.testCaseList.each { tCase ->
    		log.info "Looking into test case: ${tCase.name}"
    		tCase.testStepList.each { step ->
    			log.info "Looking into test step: ${step.name}"
    			if (step instanceof WsdlTestRequestStep 
    				|| step instanceof RestTestRequestStep
    				|| step instanceof JdbcRequestTestStep
    				|| step instanceof HttpTestRequestStep) {
    				log.info "Found a request step of required type"
    				def assertions = step.assertionList
    				assertions.each{ updateAssertionSLA(it, newSLA) }
    			}
    		}
    	}
    }

     

     

  • nmrao's avatar
    nmrao
    Champion Level 3

    Groovy Approach

    Note that though it is tested in a limited environment, have a copy of your original soapui project before running this script. This will modify the value of Response SLA assertion for most of the types which supports Response SLA i.e., HTTP, SOAP, REST and JDBC test requests

     

     

    Add a Groovy Script step in the same project, in any test case. and copy the below and run the script:

     

    Like it is mentioned earlier reply, it assigns a project level custom property RESPONSE_TIME_SLA as value of Response SLA assertion. So, you need to define the same project level property and put a value that is needed and save the project.

     

    Of course, if you are executing the project using SOAPUI_HOME/bin/testrunner.bat/.sh utility, and the there is network latency you can still increase the value from the command-line itself (without even require to modify the value in project) by passing "-PRESPONSE_TIME_SLA=500" along with testrunner command line options.

     

    You can also find it here

    https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/UpdateResponseSLA.groovy

     

    /**
     * This script updates value of Response SLA assertion in the soapui project
     * with value mentioned for 'newSLA' variable below which 
     * currently assigns project level custom property call RESPONSE_TIME_SLA and
     * you need to define it with required value for the request steps of type
     * SOAP, REST, JDBC, HTTP
     */
    import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
    import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
    import com.eviware.soapui.impl.wsdl.teststeps.JdbcRequestTestStep
    import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep
    import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.ResponseSLAAssertion
    //update the new value as needed
    def newSLA = '\${#Project#RESPONSE_TIME_SLA}'
    //Get the project object
    def project = context.testCase.testSuite.project
    
    //Closure to update the Response SLA assertion value
    def updateAssertionSLA = { assertion, sla ->
    	if (assertion instanceof ResponseSLAAssertion) {
    		log.info "Found a request step assertion with Response SLA type, and updating its value"
    		assertion.setSLA(sla)
    	}
    }
    //Actual script that traverse thru the project -> suite -> case -> step
    project.testSuiteList.each { suite ->
    	log.info "Looking into test suite: ${suite.name}"
    	suite.testCaseList.each { tCase ->
    		log.info "Looking into test case: ${tCase.name}"
    		tCase.testStepList.each { step ->
    			log.info "Looking into test step: ${step.name}"
    			if (step instanceof WsdlTestRequestStep 
    				|| step instanceof RestTestRequestStep
    				|| step instanceof JdbcRequestTestStep
    				|| step instanceof HttpTestRequestStep) {
    				log.info "Found a request step of required type"
    				def assertions = step.assertionList
    				assertions.each{ updateAssertionSLA(it, newSLA) }
    			}
    		}
    	}
    }

     

     

    • RMC's avatar
      RMC
      New Member

      Thanks a lot nmrao and rupert_anderson. This script really helped me in updating SLAs for over 900 test cases and it worked wonderfully.

    • PrakashKannan's avatar
      PrakashKannan
      Contributor

      Do we have option to update the xpath for all assertions available in the project ?

       

      My current xpath in all the test cases : //*:<api_name>_mResponse/<parent node>/<parent node_row>

      I want to update :  //*:<api_name>_mResponse/*:<parent node>/*:<parent node_row>

       

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi Lokesh,

     

    Have you tried setting a project level property e.g. responseTime and then using this to set the response time in all the Assertions e.g. using a property expansion ${responseTime} ?

     

    Regards,

    Rupert

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi Rupert,

       

      This is obviously how it had to be done initially, but I take it that Lokesh already has 250 tests with the hardcoded value of 2secs... :)

      So I think that the question should be read as: "Could someone please provide me with the groovy script sample to iterate through all test steps within test project, find assertions and update the value of one of its properties (e.g. set timeout value to ${responseTime})"

       

      P.S. My experience with SoapUI is quite limited and I don't have such code sample at hand (and don't need it at the moment), but if this is possible I would like to see the sample as well. (Just for my education :) )

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi Alex,

         

        Thanks, I kind of knew that was what was being asked - for your information, Lokesh has been messaging me privately and I have already provided him with a Groovy script to copy a REST service's request body across all its POST REST Request TestSteps (250).

         

        In this particular case, I could certainly enhance the Groovy script to update all Assertions across all his TestSteps, but I don't see it as the best way forward - better to set the Assertions up to use a property.

         

        Of course, now that he has 250 Assertions to update - maybe a 'hacky' compromise would be to:

        • Setup the responseTime property
        • Edit the SoapUI project file XML and globally replace the "250" with ${responseTime}

        Of course, please backup the project file first in case it goes wrong!

         

        Thanks,

        Rupert

         

         

  • nmrao's avatar
    nmrao
    Champion Level 3

    If it has to be done very quickly, I would do it in a dirty way. Be sure and careful before adopting it, there is back copy of the soapui project.

     

    Close the project from SoapUI

    Open the project in a favourite text editor.

    Search for (change the value if it is different, when you say 2 seconds, did you put 2000?)

    <token>2</token>

    Try replacing it with (do not apply replace all, but still this can be quick way)

    <token>${#Project#RESPONSE_TIME_SLA}</token>

     

    Save the project.

    Now Open the project in SoapUI

    Define project level custom property RESPONSE_TIME_SLA and assign the value what ever you want.

    This not helps with current change, but also for any changes in the future since it is parameterized.

     

    Hope this helps.