Home > database >  How to substitute the environment variable in the parameter defined in the .properties file
How to substitute the environment variable in the parameter defined in the .properties file

Time:01-14

We have 2 test environments qa1 and qa2. Based on what we have defined in the Run/Debug configurations we either use qa1.properties file or qa2.properties file. We use Java, Selenium and IntelliJ IDEA IDE. I have defined the ENV_NAME=qa2 in the qa2.properties file

Within each of these properties file we have URLs defined. As an example in qa1.properties file we will have LOGIN_URL=https://demo.testing.com/vp-clientlib-v1.php?env=qa1 and the same URL in qa2.properties file we will have LOGIN_URL=https://demo.testing.com/vp-clientlib-v1.php?env=qa2

As you see, the environment is hardcoded. I am trying to substitute the ENV_NAME in the URL but haven't been successful so far. Could you please help me.

CodePudding user response:

Please have a look at Apache Configuration. It supports Variable Interpolation.

If you like to set ENV_NAME as a OS enviornment variable ENV_NAME=xyz your property will be written like

LOGIN_URL=https://demo.testing.com/vp-clientlib-v1.php?env=${env:ENV_NAME}

If you set ENV_NAME=xyz as property in the property file than you would write

LOGIN_URL=https://demo.testing.com/vp-clientlib-v1.php?env=${ENV_NAME}

Beside this Apache Configuration provides a lot of other cool stuff for flexible property definition.

CodePudding user response:

Thank you @Stefan D. Your response introduced me to variable interpolation and I was able to do further research on it.

A simple addition of the variable ENV_NAME=qa2 in the properties file did not help me with replacing the environment name in the URLS. On reading further I understood that it isnt possible substitute the placeholder. Standard properties files are just key-value pairs.

OscarRyz's answer helped me in figuring out how to do it.

If it helps anyone out there who is new to the world of coding, In the class where the properties file is loaded and read I declared private XProperties configFile = new XProperties();

I created another class for XProperties and copied in the code from GitHub XProperties

  •  Tags:  
  • Related