Forum Discussion

MikeDally's avatar
MikeDally
Contributor
10 years ago

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?

  • MikeDally's avatar
    MikeDally
    10 years ago

    Hi guys,

     

    Wow didn't realise this was such a thought-provoking post! :D

     

    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 )
     }
     }
  • 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.

  • nmrao's avatar
    nmrao
    Champion Level 3
    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.