Forum Discussion

d19price's avatar
d19price
Occasional Contributor
21 days ago

Set active environment on Save

 

What is the correct way to always set the active environment to "Dev" upon saving a project?

Forgetting to do this creates a huge number of Git differences.

I tried with the below in my Project->Save Script and this works if the active environment is already set to "Dev", but fails with a bunch of errors if the active environment in the GUI is set to, for example, "Test" (the switch is actually made though to "Test").

project.environmentList.setActiveEnvironment("Dev")

 

 

 

3 Replies

  • d19price's avatar
    d19price
    Occasional Contributor

    *I tried with the below in my Project->Save Script and this works if the active environment is already set to "Dev", but fails with a bunch of errors if the active environment in the GUI is set to, for example, "Test" (the switch is actually made though to "Dev").

    • CarlosValcarcel's avatar
      CarlosValcarcel
      Icon for Staff rankStaff

      Your original question was:
      "What is the correct way to always set the active environment to "Dev" upon saving a project?"

      The answer is:

      1. select Configure Environments from the drop down to the right, you can 
      2. select which one of the environments is the Active one for a given project.

      1.

      2 .

       

      Make sure you press OK to save this config.

      This selection will always be the env for your project unless you change the selection from the dropdown.

      Your next question was different:
      "I tried with the below in my Project->Save Script and this works if the active environment is already set to "Dev", but fails with a bunch of errors if the active environment in the GUI is set to, for example, "Test" (the switch is actually made though to "Dev")."

      Setting the active environment for your project and then resetting it in a script are very different things.

      project.environmentList.setActiveEnvironment("Dev")

      is not the correct API call in ReadyAPI’s scripting environment, and it often fails because:

      1. environmentList may be null or not initialized yet when the Save Script runs.
      2. The UI’s “active environment” and the internal project environment state aren’t always in sync at Save Script time.
      3. The ReadyAPI Groovy API uses a different method for setting the active environment.

      The correct method is:

      project.setActiveEnvironment("Dev")

      Put this in a Load Script (not Save Script) because:

      • In a Load Script, the environment subsystem is fully initialized.
      • In a Save Script, parts of the environment API may not be ready yet, so calls like .setActiveEnvironment() can fail.

       

      Let me know if that helps!

  • I'm not sure if this is what you mean, but when you select Configure Environments from the drop down to the right, you can select which one of the environments if the Active one for a given project. That config will persist even when you restart ReadyAPI.

    Is that what you were looking for?

    Let me know how it goes!