Do not use property files for storing test data
It is very common to store test data in property files as follows:
browser=Chrome
url=https://test.abc.com
username=aaa1
password=bbb1
We have here 4 properties for
browser
url
username
password
The code that reads all these properties is very simple:
Properties properties = new Properties();
FileInputStream file = new FileInputStream(fileName);
properties.load(file);
String browser = properties.getProperty("browser");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
We have one line of code for each property read from the property file.
Things get a bit more complex when we need to store the same properties for multiple environments.
Having 3 environments (test, stage and prod), to keep the property names unique, we will append the environment as suffix to the property name:
browser.test=Chrome
url.test=https://test.abc.com
username.test=aaa1
password.test=bbb1
browser.stage=Chrome
url.stage=https://test.abc.com
username.stage=aaa2
password.stage=bbb2
browser.prod=Chrome
url.prod=https://test.abc.com
username.prod=aaa3
password.prod=bbb3
This complicates the code to read the values from the property file since we need to use the environment as well:
Properties properties = new Properties();
FileInputStream file = new FileInputStream(fileName);
properties.load(file);
String browser = properties.getProperty("browser." + environment);
String url = properties.getProperty("url." + environment);
String username = properties.getProperty("username." + environment);
String password = properties.getProperty("password." + environment);
It is also not great that we need to read each value on a separate line as this leads to code duplication.
There is duplication also in the suffix added to the properties' names for each environment.
It would be great if we could group somehow all properties for an environment as follows:
environment=test
browser=Chrome
url=https://test.abc.com
username=aaa1
password=bbb1
environment=stage
browser=Chrome
url=https://test.abc.com
username=aaa2
password=bbb2
environment=prod
browser=Chrome
url=https://test.abc.com
username=aaa3
password=bbb3
This would allow not only the data to be grouped better but potentially reading all properties for an environment as an object.
Sadly, this cannot be done in property files.
But, it can be done in JSON:
{
"test": {
"browser": "Chrome",
"url": "https://test.abc.com",
"username": "aaa1",
"password": "bbb1"
},
"staging": {
"browser": "Chrome",
"url": "https://test.abc.com",
"username": "aaa2",
"password": "bbb2"
},
"prod": {
"browser": "Chrome",
"url": "https://test.abc.com",
"username": "aaa3",
"password": "bbb3"
}
}
Thanks for reading.