Forum Discussion

jjam's avatar
3 months ago

Switching Environments Groovy Scripting

I am running automation on a Test Suite. There are 5 Private environments and 5 corresponding Public environments. I can kick off the automation in Jenkins with a starter Environment, i.e. QA. Each run will have to switch to Public 75% through. So in a run, it will have to switch from QA to Public QA, and 4 more switches to follow on the subsequent Jenkins job.


I have a script that can switch environments that I borrowed from Smartbear support:


def env = context.testCase.testSuite.project.getEnvironmentByName("Public QA2");


But it does not solve my dilemma.


Since I am running the same test suite for all environments I need a script that will switch depending on the current environment.


I am by far no JS or JSON coder, I get by, but it needs it to run like this:


if QA then Public QA

if QA2 then Public QA2

if UAT then Public UAT

if ENV1 then Public ENV1

if SITE then Public SITE


If someone could code that for me to run in a groovy script I would be forever indebted to you.


4 Replies

  • nmrao's avatar
    Champion Level 3

    Here is the approach:

    Define a project property say, ACTIVATE_PROFILE say QA.

    One can modify the property value any time when it is used in the ReadyAPI tool.

    In the Project's Load script, use below script:




    //Add the environment names in the below if there are more; 
    //pass the key to load the respective environment
    def envMap = [QA: 'Public QA', QA2: 'Public QA2', UAT: 'Public UAT', ENV1: 'Public ENV1', SITE: 'Public SITE']
    def profile = project.getPropertyValue('ACTIVE_PROFILE')
    if (envMap.containsKey(profile)) {
    	println 'Found the environment and activating'
    	def env = project.getEnvironmentByName(envMap[profile])
    } else {
    	println 'NO suitable environment found, pass the right value and retry!'





    From the command line and jenkins, one can pass the project property value (without modifying the same in the project file) as below to activate UAT environment:


    • jjam's avatar

      Thanks richie  for the quick response.


      nmrao Thanks for your help. I followed the instructions and restarted ReadyAPI 3.49.0. The program would not start. I had to move the composite project folder to another directory so ReadyAPI wouldn't pick it up upon load. The program started after this. When I try to import the automation project ReadyAPI closes immediately.


      Any idea what happened here? I need to restore my project as it has over a month of work. I imagine I have to edit some .XML now?


      Edit I was able to get the script out of the XML and can now load the project. I think it was a conflict of what env it opened with and what env I specified in ACTIVE_PROFILE.

  • richie's avatar
    Community Hero
    Hey jjam,

    I'm a little out of practice with my groovyscript, but i can probably sort this for you.

    However the coders like nmrao and ChrisAdams are a lot better coders than i am and you'll get something professional rather than the inefficient hack job you'll get off me.

    I'll start looking at this tomorrow


  • nmrao's avatar
    Champion Level 3

    Have you seen the logs?

    Please see the below statement from the above script



    There is no point in running the project in the Jenkins or commandline when it is not able to find the given environment name correct.

    You may comment the same if you want.