Ask a Question

Centralizing the custom properties defined for Environments. Want to read values from a config file.

SOLVED
Madhusm
Occasional Contributor

Centralizing the custom properties defined for Environments. Want to read values from a config file.

We have projects created which has to be executed across N environments. Currently it is quite cumbersome to manually create environments and maintain it.
For example I have an environment defined XYZ and a custom property defined .Now if there is a change in the property value have to manually edit all the projects for the property to get updated.

How can I use one centralized file with all properties defined and based on the environment it can make the changes to that environment. What are the options I have to implement this?

Sample Config file 

Env1=Beatrice
Env1.NodeID=123456789
Env1.APIKey=Abnjjjkj89090909
Env2=Alex
Env2.NodeID=4665656565656
Env2.APIKey=Abjjkjkj87887878787
EnvN=XYZ
EnvN.NodeID=467878799956565656
EnvN.APIKey=MNKKjjkjkj87887878787

 

I have projects with environments defined as in the snapshot attached, now I want to set these properties from a generic config property file which has these properties defined with different values based on the environment. So when I execute the test I want that properties are extracted from the generic config file at the time of execution based on the environment chosen.

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

Just to demonstrate, created a simple project, called testProperties

- This project contains a single test suite and test case which contains a groovy script test step.

- The groovy script test step access the project properties. Of course, any test step in the project can access these properties which every knows.

- Created two property files. These property files are being passed as arguments to testrunner script command line

-- qa.properties => contains environment details of QA

-- staging.properties => contains environment details of Staging

- This is completely handled as out-of-the-box functionality of the tool, so no burden for the user at all.

- The argument needs to pass is -Dsoapui.properties.<projectName>=<property file path>

- The projectName in the above is what you see in when the project is opened in the tool. or you can find in the project properties Name field. In my case, my project is named as "testProperties"

- Here is the excerpt from the command line execution of the project and they show the correct values loaded from the property file. Notice the log statements.

- You can see the command

./testrunner.sh testProperties-soapui-project.xml -Dsoapui.properties.testProperties=qa.properties

 

nmrao_0-1624547233684.png

 

Attaching the sample project (created in free version for the benefit of SoapUI  tool users as well), so one can try themselves by downloading the artifacts.

 

https://github.com/nmrao/sample-soapui-projects/tree/master/environmentsExample

 

 

One can added as many properties required for the each environment in separate file and just pass the respective file when needed.

 

Of course, one can use Environment feature if there is need and when they have pro tool. I am sure, it must be possible to switch the environment name from the command-line, i am not familiar about that. One should be able find it in the documentation. I am wondering why one need to write the script like what you have.

,



Regards,
Rao.

View solution in original post

21 REPLIES 21
nmrao
Community Hero

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

Madhusm
Occasional Contributor

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

What I did was I wrote a groovy script which would change the  custom properties. This script works well when used as Groovy step.

However, I want this groovy script to be executed as the Load script or set up script. On trying to execute the following script as setupscript   I get error:

 

def props = new Properties();
File propFile = new File('C:/Axon_tests/config.properties')
props.load(propFile.newDataInputStream())
def env=props.getProperty('Env')

if (env=='Test')
{
def env1 = context.testCase.testSuite.project.getEnvironmentByName("Test 14.4");
context.testCase.testSuite.project.setActiveEnvironment(env1)
def NodeIDvalue=props.getProperty('NodeID')
def APIKeyValue=props.getProperty('ÁPIKey')
log.info(APIKeyValue)
testRunner.testCase.testSuite.project.setPropertyValue("NodeID", NodeIDvalue)
testRunner.testCase.testSuite.project.setPropertyValue("APIKey", APIKeyValue)
}

 

I get error if I try to execute it as setup script. Can anybody please help me with this how to resolve this.

 

Madhusm_0-1624438243943.png

 

nmrao
Community Hero

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

Have you tried the approach link given in the previous reply?


Regards,
Rao.
nmrao
Community Hero

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

What does the log say? Error in which line ?


Regards,
Rao.
Madhusm
Occasional Contributor

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

The error is in these lines it is not able to get the Environment based on context when executed as setscript at test suite level , I also tried with 

def env1 = context.testSuite.project.getEnvironmentByName("Test 14.4"); 

It still showed error on the same line number.

 

I have attached the error logs , the error is in line 8 

 

def props = new Properties();
File propFile = new File('C:/Axon_tests/config.properties')
props.load(propFile.newDataInputStream())
def env=props.getProperty('Env')
log.info(env)
if (env=='Test')
{
def env1 = context.testCase.testSuite.project.getEnvironmentByName("Test 14.4");
context.testCase.testSuite.project.setActiveEnvironment(env1)
log.info(env1)
/* def NodeIDvalue=props.getProperty('NodeID')
def APIKeyValue=props.getProperty('ÁPIKey')
log.info(APIKeyValue)
testRunner.testCase.testSuite.project.setPropertyValue("NodeID", NodeIDvalue)
testRunner.testCase.testSuite.project.setPropertyValue("APIKey", APIKeyValue)
}*/

nmrao
Community Hero

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

Where is script located in the project?
Why do you want to change the environment in the middle of the execution?


Regards,
Rao.
Madhusm
Occasional Contributor

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

I have added this as setup script as I understand setup script is executed at the beginning , the problem I'm trying to solve here is we have env defined but the custom properties values of the env we are trying to set up using a generic file. So in future when there is a change in one property I do not have to visit each project and manually change the values. At the time of execution , first it could do the setup and execute accordingly.

 

The problem I'm facing now is if I use the getEnvironmentbyName from the setupscript it is not able to find the context and throws an exception.

 

Madhusm_0-1624515650160.png

 

 

 

nmrao
Community Hero

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

@Madhusm 

Gave the answer with a link in my first reply which does the same . And also asked the followup question and it is not answered yet.

The link given does the same thing.

You add the all the properties in a file. And import in project and access them where all you need. No programming/scripting required. Just access ${#Project#PROPERTY_NAME}

The tool does many cool things in simple way.

And the above does not use Environment feature also.

 

You can have multiple property files by environment wise.

Try it and see.

 



Regards,
Rao.
Madhusm
Occasional Contributor

Re: Centralizing the custom properties defined for Environments. Want to read values from a config f

Hi nmrao,

 

My requirement is to use it with environment variable as we have dev, test , Staging and dev environment which executes with different BO release versions  combinations. And we have close to 600 ReadyAPI projects which executes every night. And by using the environment defined and tags defined we also control the execution of a test set in different environments. 

 

This environment feature helps in maintaining the tests in better way and the execution of tests using the command line version of ReadyAPI. The way you suggested will not work for us. Hence I insisted on using a script which will do the needful. As currently ReadyAPI does not provide an option to do that.

 

Regards

Madhusmita

cancel
Showing results for 
Search instead for 
Did you mean: