Forum Discussion

groovyguy's avatar
groovyguy
Community Hero
7 years ago

Automate Environment creation for REST and SOAP services

This came up in a previous thread, and I've finally worked out how to populate environments into a project and how to set their endpoints accordingly. This is relatively simple and can be extended for adding multiple environments. We're currently using this to convert all of our various ReadyAPI / SoapUI NG Pro projects to support environments. It's working like a charm!

 

// For REST Services

import com.eviware.soapui.model.environment.*
import com.eviware.soapui.config.*

def project = context.testCase.testSuite.project;

 // Assuming binding is the first interface on the project.
 def binding = context.testCase.testSuite.project.getInterfaceAt(1).getName().toString();

 // Getting current list of environment names
def environNames = project.getEnvironmentNames().toString();

 // Names of environments to add
def name = "env1"

// Environment URLs, number should match number in previous array for environment names

project.addNewEnvironment(name);
project.setActiveEnvironment(name);

def environ = project.getActiveEnvironment();

def newService;

	// For REST service: 
 	newService = environ.addNewService(binding, com.eviware.soapui.config.ServiceConfig.Type.REST);

	def serviceConfig = newService.getConfig();

	
	def endpointConfig;
	
	endpointConfig = serviceConfig.addNewEndpoint();
	
	
	newService.buildEndpoint(endpointConfig);
	def isCopy1 = true;
	
	log.info(isCopy1);
	environ.populateService(newService, isCopy1);

	environ.release();
	

For SOAP services:

import com.eviware.soapui.model.environment.*
import com.eviware.soapui.config.*

def project = context.testCase.testSuite.project;

 // Assuming binding is the first interface on the project.
 def binding = context.testCase.testSuite.project.getInterfaceAt(1).getName().toString();

 // Getting current list of environment names
def environNames = project.getEnvironmentNames().toString();

 // Names of environments to add
def name = "env1"

// Environment URLs, number should match number in previous array for environment names

project.addNewEnvironment(name);
project.setActiveEnvironment(name);

def environ = project.getActiveEnvironment();

def newService;

	// For SOAP service:
	newService = environ.addNewService(binding, com.eviware.soapui.config.ServiceConfig.Type.SOAP);
 	// For REST service: 
 	// newService = environ.addNewService(binding, com.eviware.soapui.config.ServiceConfig.Type.REST);

	def serviceConfig = newService.getConfig();

	
	def endpointConfig;
	
	endpointConfig = serviceConfig.addNewEndpoint();
	
	endpointConfig.setStringValue(url)
	
	newService.buildEndpoint(endpointConfig);
	def isCopy1 = true;
	
	log.info(isCopy1);
	environ.populateService(newService, isCopy1);

	environ.release();
	

The two are almost identicle except for a couple of lines. Other than that, these have both worked for me without any problems. 

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Great example, Matthew! Thanks for preparing it and sharing with the Community

    • groovyguy's avatar
      groovyguy
      Community Hero

      Always happy to share, TanyaYatskovska! I've seen a number of posts here looking for something similar, so I hope it helps others out. :) 

      • sanj's avatar
        sanj
        Super Contributor

        Thats why I refer to as a legend!

        Thanks man