Reuse groovy scripts with a script library - Use case with dynamic environment url
Bundle all the frequently used scripts/ functions into a script library and use it where ever we want without duplicating the code.
USE CASE: below piece of code is used in various test steps througout the project. But what if the environment url's change, then I would need to go and find all groovy test steps where these urls have changed and manually update them from "http://testwebsites.net/api/v1/agents/" to "http://newtestwebsites.net/api/v1/agents/" (= Use Case 1). And what if I want to reuse the same code snippet but not for "agents" but for "organisations"? (= Use Case 2)
This piece of code below we want to isolate to be able to change it in one place only:
def env = testRunner.testCase.testSuite.project.activeEnvironment.name def url switch (env) { case "TEST": url = "http://testwebsites.net/api/v1/agents/" break case "DEV": url = "http://devwebsites.net/api/v1/agents/" break; case "BETA": url = "http://betawebsites.net/api/v1/agents/" break; }
Solution:
See also: https://www.soapui.org/scripting---properties/scripting-and-the-script-library.html
First we create a file Urls.groovy, put it in the scriptlibrary* , to isolate the url = Use Case 1:
import com.eviware.soapui.model.testsuite.TestRunner //update: import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunner--> this is only for running a groovy script test step individually, so use above TestRunner instead public class Urls { // Properties def testRunner; // Constructor Urls(TestRunner testRunner) { this.testRunner = testRunner; } // Methods def GetEnvUrl() { def env = testRunner.testCase.testSuite.project.activeEnvironment.name def url switch (env) { case "TEST": url = "http://testwebsites.net/api/v1/" break case "DEV": url = "http://devwebsites.net/api/v1/" break; case "BETA": url = "http://betawebsites.net/api/v1/" break; } return url; } }
Notes:
The reason why we need to use properties & constructor, is because we reuse some soapui built in objects, namely the testRunner.
See the api pro documentation: http://www.soapui.org/apidocs/com/eviware/soapui/model/testsuite/TestRunner.html
The method, which we named "GetEnvUrl()" contains the actual copy paste of the groovy script.
The reason why we need to "return url" at the end is because url will only be known within this isolated piece of code and when would just call upon it within the groovy test step (without return in the class) it is not "transfered".
A similar class for use case 2, namely AgentUrls.groovy:
-----------------------------------------------------------------------------------------------------—------------------------------------------------------------------------------------—
import com.eviware.soapui.model.testsuite.TestRunner class AgentUrls { def baseUrl = "agents/" // Properties def testRunner; // Constructor AgentUrls(TestRunner testRunner) { this.testRunner = testRunner; } // Methods String GetAllUrl() { def url = new Urls(testRunner).GetEnvUrl() + baseUrl return url; } }
We can easily duplicate this class AgentUrls.groovy to make a OrganisationUrls.groovy by changing baseUrl to "organisations/" (or others).
To call this piece of code again in the groovy test step, we can replace by this simple line:
def url = new AgentUrls(testRunner).GetAllUrl() if you just need to have the url and not the "agents/" behind it: def url = new Urls(testRunner).GetEnvUrl()
Note: the scriptlibrary can be set via an absolute path throught the settings/ReadyApi option (default = C:\Program Files\SmartBear\ReadyAPI-2.3.0\bin\scripts). If you share your codebase, in case of multiple testers or for running it in CIT build pipeline, we need a relative path. We can define it via Project Properties — SWcript Library option. We can do this the same way we define the relative path for the saving of our composite project:, also at project properties level (Resource Root) with for instance ${projectDir}.
The result should always be visible in the readyApi log , for instance:
Fri Nov 17 13:18:08 CET 2017:INFO:XXXXX\Script Library\Urls.groovy is new or has changed, reloading...
Fri Nov 17 13:18:08 CET 2017:INFO:Resetting groovy class cache due to 1 modified file