Tuesday 27 February 2018

How to Handle Configuration File in Selenium

When we are working on automation framework then we have to follow such strategy so we can avoid hard code value as much as we can.

There are many parameters in every framework we can be changed frequently for running test case on different environemnt as well as with different configuration. So, we can not use hard code value for such parameters. We have to use configuration file for this scenario.

In that file, we can store all the parameters which are used in framework like browser name, browser type, application URL etc.

If you are using C# in selenium then go to below link for reading configuration file.
How to Read Configuration File in Selenium C#

We have to create one method to access value of those parameters from configuraiton file.

First of all we have to create one configuratioj file. Please follow below step.


  1. Create Configs folder under src folder
  2. Right click on Configs folder
  3. Go to New > File
  4. Give file name 'Configuration.properties'

Now add parameters which you are going to use in your framework.






Now, we have to create one method to access all these values of parameters. For that I have created one class 'ConfigFileReader'. I have created all methods for accessing values that class.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class ConfigFileReader 
{
 
 private Properties properties;
 private final String propertyFilePath= "Configs/Configuation.properties";
 
 
 public ConfigFileReader(){
  BufferedReader reader;
  try {
   reader = new BufferedReader(new FileReader(propertyFilePath));
   properties = new Properties();
   try {
    properties.load(reader);
    reader.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   throw new RuntimeException("Configuration.properties not found at " + propertyFilePath);
  }  
 }
 
 public String getChromeDriverPath()
 {
  String driverPath = properties.getProperty("chromeDriverPath");
  if(driverPath!= null) return driverPath;
  else throw new RuntimeException("driverPath not specified in the Configuration.properties file.");  
 }
 
 public String getBrowserName()
 {
  String browserName = properties.getProperty("browserName");
  if(browserName!= null) return browserName;
  else throw new RuntimeException("Browser name not specified in the Configuration.properties file.");  
 }
 
 public String getBrowserType()
 {
  String browserType = properties.getProperty("browserType");
  if(browserType!= null) return browserType;
  else throw new RuntimeException("Browser Type not specified in the Configuration.properties file.");  
 }
 
 public long getImplicitlyWait()
 {  
  String implicitlyWait = properties.getProperty("ImplicitlyWait");
  if(implicitlyWait != null) return Long.parseLong(implicitlyWait);
  else throw new RuntimeException("implicitlyWait not specified in the Configuration.properties file.");  
 }
 
 public String getApplicationUrl()
 {
  String url = properties.getProperty("appURL");
  if(url != null) return url;
  else throw new RuntimeException("APP URL not specified in the Configuration.properties file.");
 }
 
 public String getTestDataSheetPath()
 {
  String path = properties.getProperty("testDataSheetPath");
  if(path != null) return path;
  else throw new RuntimeException("Test data sheet not specified in the Configuration.properties file.");
 }

}

You can see I have created separate function for access value of particular parameter. So you have to create separate function for all parameters. 

Please add comment if you have any question.

No comments:

Post a Comment

Popular