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 identical except for a couple of lines. Other than that, these have both worked for me without any problems.449Views0likes0CommentsProperties in SoapUI - case study
The possibility to use properties is a very important aspect in SoapUI automation. And although they are widely used they tend to be not very well understood. Indeed the name 'property' seems a little unfit for an element that (by all means) is presented by SmartBear as a variable to hold certain values for later use in the test execution cycle. For instance, one can have a 'sessionId' element to hold the current session string or one can have a 'cookie' element to hold some cookie value and so on. 1.Defining properties Properties can be defined at the project level, at the test suite level, at the test case level or in a special properties step. For instance, in order to define or edit a property at the test case level one must click on the desired test case in the left hand navigator and then just select the 'Custom Properties' tab. This is similar for any test suite or project. As said before there is also the possibility to use a special'properties step'. Thisstepmust be created inside a certain test case thus its properties will only be available inside that particular test case. 2. Reading properties 2.1 Referencing in SoapUI steps When wanting to reference a property, attention has to be paid to the level at which it was defined. There are different syntaxes that need to be written depending on it as presented in the table below: Level at which it is defined Syntax Example property name Example request Project level ${#Project#Property} Port http://myserver:${#Project#Port} Test suite level ${#TestSuite#Property} Port http://myserver:${#TestSuite#Port} Test case level ${#TestCase#Property} Port http://myserver:${#TestCase#Port} Special properties step ${#Property} Port http://myserver:${#Port} Following the above example, if one would have a project level property named Port with the value 8080, then that property could be used anywhere in the project by simply referencing it like${#Project#Port}. Example: When sending the request, the value of the ${#Project#Port} would be replaced by its value (in our example 8080). 2.2 Referencing in scripts In order to read a property in a script the following syntax is required: /** * Notes: * There are 4 properties defined at 4 levels: project, test suite, test case and special properties step level * The special properties step is simply called 'Properties' */ def stepProperty = testRunner.testCase.getTestStepByName( "Properties" ).getPropertyValue( "myCustomProperty" ) def testCaseProperty = testRunner.testCase.getPropertyValue( "myCustomProperty" ) def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "myCustomProperty" ) def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "myCustomProperty" ) 3. Updating properties Updating tests can be done using a groovy script or a special transfer property step. Both of the methods are simple and straightforward but for the purpuse of this article only a groovy script example will be shown: /** * Notes: * This script changes 4 properties defined at 4 levels: project, test suite, test case and special properties step level * The special properties step is simply called 'Properties' */ testRunner.testCase.getTestStepByName( "Properties" ).setPropertyValue( "myCustomProperty", "newSpecialValue" ) testRunner.testCase.setPropertyValue( "myCustomProperty", "newTestCaseValue" ) testRunner.testCase.testSuite.setPropertyValue( "myCustomProperty", "newTestSuiteValue" ) testRunner.testCase.testSuite.project.setPropertyValue( "myCustomProperty", "newProjectValue" )495Views2likes0Comments