0

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.

Divya
  • 59
  • 10

2 Answers2

2

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.

Stefan D.
  • 299
  • 3
  • 8
  • I've made my answer a bit more precisely to bring it more to the point of your question. But as already suggested, take a time to read Apache Configuration documentation. – Stefan D. Jan 13 '22 at 09:10
  • Hey Stefan, Thank you so much for your response. I did the same where I declared ENV_HOME as another property in the properties file. But it failed to replace the placeholder in the URL and the URL continued to be LOGIN_URL=https://demo.testing.com/vp-clientlib-v1.php?env=${ENV_NAME} as is. I am curious to know if we could do this but from what I read in other posts, properties file is just a collection of strings in a key value pairs, and there is no direct way to substitute the property value within the property file. I would be happy if I wrong and if it is infact possible to do so – Divya Jan 13 '22 at 23:13
0

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

Divya
  • 59
  • 10