Saturday, March 1, 2014

Using Configuration (Config File) While Test Automation.

It is always advised to use a config file in your test automation. The purpose of having a config file is to have few variables outside of code so that if you can change variables' values without changing your code.

To use a config file in Java, Apache's common configuration jar is very popular and easy to use.

Download the latest version of these jar and add them to Project's Build Path.
commons-lang-2.6.jar
commons-configuration-1.10.jar

Here is the sample code to read config:

public static void InitializeConfiguration()  
{
        PropertiesConfiguration propertyConfigFile;
try
{
propertyConfigFile = new PropertiesConfiguration();
String config = "/config/config.txt"; //Location of config file
propertyConfigFile.load(config);

        //Read variable from config 
        String url = propertyConfigFile.getProperty("qaURL").toString();
}
catch(ConfigurationException e)
{
System.out.println("Error Occured While Reading Config File");
}

}

Copy the below line in a notepad and save it with some valid extension. qaURL is the variable being called in above program. This will serve the purpose of a config file.
## is used to comment the line.

Sample:

## Please provide the URL here:
qaURL = https://www.google.com





3 comments:

  1. its really nice to know that config can create magic

    ReplyDelete
    Replies
    1. Definitely, it can .. let me know if you need any more information on this.

      Delete
  2. Appreciable! The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking training / cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.

    ReplyDelete