Ask a Question

How do I automatically insert a REST test step into a SoapUI test using Groovy?

SOLVED
MikeDally
Contributor

How do I automatically insert a REST test step into a SoapUI test using Groovy?

My team and I currently have a few hundred tests scripted which we want to start automating. Instead of manually creating test cases in SoapUI, I have written a script which generates test cases with default properties. My next task is to insert a sample request for every REST service we have defined in our SoapUI project; I'm not too sure where to start with the SoapUI Groovy API, but below is a sample of my test creation script so far:

 

import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory
import com.eviware.soapui.impl.rest.RestResource;
import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory
import com.eviware.soapui.config.TestStepConfig
import com.eviware.soapui.impl.rest.RestRequest;
import com.eviware.soapui.impl.rest.RestService;

listOfTestCases = [
'Address Lookup with valid PostCode',
'Address Lookup with Valid CompanyName',
'Address Lookup with Valid Street',
'Address Lookup with Valid town',
'Address Lookup with Valid CompanyName & Postcode',
'Address Lookup with Valid Street & Postcode',
'Address Lookup with Valid Town & PostCode',
'Address Lookup with Valid CompanyName & Street',
'Address Lookup with Valid CompanyName & Town', 
'Address Lookup with Valid Street & Town',  
'Address Lookup with Valid Postcode, CompanyName, Street & Town']
for (String testCase : listOfTestCases) {
   tc = context.testCase.testSuite.addNewTestCase(testCase)
   tc.addProperty("testcycl-id") 
   tc.addProperty("cycle-id") 
   tc.addProperty("test-id") 
   tc.addProperty("run-id") 
   tc.addProperty("test-config-id")
   tc.addProperty("test-step-id")
} 

My question is thus: For every test case I have created, how do I insert one of every REST request?

1 ACCEPTED SOLUTION

Accepted Solutions
MikeDally
Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

Hi guys,

 

Wow didn't realise this was such a thought-provoking post! πŸ˜„

 

I have figured out what I need to do anyway, which involves numerous loops:

 

    import com.eviware.soapui.impl.rest.RestResource;
    import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory
    import com.eviware.soapui.config.TestStepConfig
    import com.eviware.soapui.impl.rest.RestRequest;
    import com.eviware.soapui.impl.rest.RestService;

 List<RestService> restServiceList = testRunner.testCase.testSuite.project.getInterfaceList()
 for(RestService restService : restServiceList){
 	operationList = restService.getOperationList()
 	for(int indexOfOperation; indexOfOperation < operationList.size(); indexOfOperation ++){
 	     RestResource resource = restService.getOperationAt(indexOfOperation);
 	     List<RestResource> requestList = resource.getRequestList();
          for(int indexOfRequestList; indexOfRequestList < requestList.size(); indexOfRequestList++)
          	restRequest = resource.getRequestAt(indexOfRequestList)
 	     	testCaseName = resource.getName()
 	     	TestStepConfig testStepConfig = RestRequestStepFactory.createConfig( restRequest, testCaseName );
          	testRunner.testCase.addTestStep( testStepConfig )
 }
 }

View solution in original post

9 REPLIES 9
nmrao
Community Hero

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

It looks like there is some formatting issue with this forum showing the formatted code. Obviously not sure what this above code is doing, but using soapUI API, you may be able to do it using java or groovy.


Regards,
Rao.
rupert_anderson
Valued Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

Hi, 

 

Sorry to jump in Rao, but I thought this topic looked interesting and I posted some previous solutions that may be along the same lines e.g. using java & groovy to generate and submit SoapUI projects & requests programatically.

 

The first one was for REST requests:

 

http://community.smartbear.com/t5/SoapUI-Open-Source/How-to-create-REST-project-using-Groovy-script/...

 

The second one is for SOAP requests: 

 

http://community.smartbear.com/t5/SoapUI-Open-Source/How-to-get-parameter-of-SOAP-operation/m-p/9976...

 

Whilst this isn't doing exactly what you are trying to do e.g. create the TestCase objects, I wanted to see if it helped or prompt further discussions?

 

Cheers,

Rup

Author of SoapUI Cookbook
nmrao
Community Hero

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

Hello Rup, you are welcome always to jump in and provide your views / comments / suggestions. And you are phenomenal. Sincerely appreciate your willingness to help users, efforts and time.



Regards,
Rao.
rupert_anderson
Valued Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

Hello Rao,

 

Thats very nice of you to say, I sincerely appreciate your thanks / praise / kudos, really makes a difference to keeping motivated to attempt to help out where possible! May I say that I am also a strong admirer of all your efforts in the community and your work rate in answering is amazing! Go SoapUI Community!:-)

 

Aside from our mutual show of community member respect - sorry for the side track there, Steve is there anything more we can help with / discuss on this topic? It's quite an interesting one e.g. Groovy scripting / dynamic construction of SoapUI objects... Did you get any further with this problem?

 

Cheers,

Rup

Author of SoapUI Cookbook
MikeDally
Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

Hi guys,

 

Wow didn't realise this was such a thought-provoking post! πŸ˜„

 

I have figured out what I need to do anyway, which involves numerous loops:

 

    import com.eviware.soapui.impl.rest.RestResource;
    import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory
    import com.eviware.soapui.config.TestStepConfig
    import com.eviware.soapui.impl.rest.RestRequest;
    import com.eviware.soapui.impl.rest.RestService;

 List<RestService> restServiceList = testRunner.testCase.testSuite.project.getInterfaceList()
 for(RestService restService : restServiceList){
 	operationList = restService.getOperationList()
 	for(int indexOfOperation; indexOfOperation < operationList.size(); indexOfOperation ++){
 	     RestResource resource = restService.getOperationAt(indexOfOperation);
 	     List<RestResource> requestList = resource.getRequestList();
          for(int indexOfRequestList; indexOfRequestList < requestList.size(); indexOfRequestList++)
          	restRequest = resource.getRequestAt(indexOfRequestList)
 	     	testCaseName = resource.getName()
 	     	TestStepConfig testStepConfig = RestRequestStepFactory.createConfig( restRequest, testCaseName );
          	testRunner.testCase.addTestStep( testStepConfig )
 }
 }

View solution in original post

MikeDally
Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory
import com.eviware.soapui.impl.rest.RestResource;
import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory
import com.eviware.soapui.config.TestStepConfig
import com.eviware.soapui.impl.rest.RestRequest;
import com.eviware.soapui.impl.rest.RestService;
listOfTestCases = [
'Address Lookup with valid PostCode',
'Address Lookup with Valid CompanyName',
'Address Lookup with Valid Street',
'Address Lookup with Valid town',
'Address Lookup with Valid CompanyName & Postcode',
'Address Lookup with Valid Street & Postcode',
'Address Lookup with Valid Town & PostCode',
'Address Lookup with Valid CompanyName & Street',
'Address Lookup with Valid CompanyName & Town',	
'Address Lookup with Valid Street & Town',	
'Address Lookup with Valid Postcode, CompanyName, Street & Town']
for (String testCase : listOfTestCases) {
   tc = context.testCase.testSuite.addNewTestCase(testCase)
   tc.addProperty("testcycl-id") 
   tc.addProperty("cycle-id") 
   tc.addProperty("test-id") 
   tc.addProperty("run-id") 
   tc.addProperty("test-config-id")
   tc.addProperty("test-step-id")
    List<RestService> restServiceList = testRunner.testCase.testSuite.project.getInterfaceList()
 for(RestService restService : restServiceList){
 	operationList = restService.getOperationList()
 	for(int indexOfOperation; indexOfOperation < operationList.size(); indexOfOperation ++){
 	     RestResource resource = restService.getOperationAt(indexOfOperation);
 	     List<RestResource> requestList = resource.getRequestList();
          for(int indexOfRequestList; indexOfRequestList < requestList.size(); indexOfRequestList++)
          	restRequest = resource.getRequestAt(indexOfRequestList)
 	     	testCaseName = resource.getName()
 	     	TestStepConfig testStepConfig = RestRequestStepFactory.createConfig( restRequest, testCaseName );
          	tc.addTestStep( testStepConfig )
 }
 }

 Above is the full code to generate test cases and add REST requests.

MikeDally
Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

See my solution below πŸ™‚


@rupert_anderson wrote:

Hi, 

 

Sorry to jump in Rao, but I thought this topic looked interesting and I posted some previous solutions that may be along the same lines e.g. using java & groovy to generate and submit SoapUI projects & requests programatically.

 

The first one was for REST requests:

 

http://community.smartbear.com/t5/SoapUI-Open-Source/How-to-create-REST-project-using-Groovy-script/...

 

The second one is for SOAP requests: 

 

http://community.smartbear.com/t5/SoapUI-Open-Source/How-to-get-parameter-of-SOAP-operation/m-p/9976...

 

Whilst this isn't doing exactly what you are trying to do e.g. create the TestCase objects, I wanted to see if it helped or prompt further discussions?

 

Cheers,

Rup


 

rupert_anderson
Valued Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

Hi Mike,

 

Well done! That's quite cool!

 

I ran your final script (after adding an extra } ) and it generated the TestCases with REST Test Requests!

 

Well done on sharing this! πŸ™‚

 

Cheers,

Rup

Author of SoapUI Cookbook
MikeDally
Contributor

Re: How do I automatically insert a REST test step into a SoapUI test using Groovy?

No problem! I live to give πŸ™‚

cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β