How to read values from Config File
When we are using / drafting any framework, it is very important that we should keep configurable things/properties separate from static things. To elaborate more, consider an example of test data. Test data is non static part of any framework, because it will change for each of regression/ test cycle. So we end up replacing test data after each test cycle. Another example could be web elements. If any change is happening to UI, it ultimately affects your web elements. So the best practice says keep your static things apart from configurable one. Best part to implement this is to have config.properties file. Its basically a text file which will have values in the format of key and its value.
For Example: key1 = value1
key2=value2
We will use Properties class which implements map interface.
To create config file, right click on your project > Select file option > save the file with name “config.properties“.
![Capture](https://qatestinglearn.files.wordpress.com/2018/07/capture.png?w=900)
Create a class which will read key from your config file and you will get output as its value. In above example if we are calling key “browser” we will get output as “chrome“.
![Capture](https://qatestinglearn.files.wordpress.com/2018/07/capture1.png?w=900)
Explanation:
- We have created static method, so that we can call method directly by using class_name.method_name
- File will store the location of config.properties file
- FileInputStream will transfer the file object to Properties class.
- load() method will load all the values from config.properties to map interface
To read values from config file you need to:
classname.readvaluesFromConfigFile().getProperty(“browser”); and it’s output will be “chrome“.
Note: Do not store value with double quotes , because above method will return value as is.
Comments
Post a Comment