Creating configuration properties for Selenium Test Suite

来源:互联网 发布:报表数据分析实例 编辑:程序博客网 时间:2024/05/17 02:23


http://selftechy.com/2011/06/20/creating-configuration-properties-for-selenium-test-suite

Create a configuration properties file to store parameters such as log file path, test data path, results path, selenium execution speed, wait period, etc, then make changes to the parameters whenever needed.

Java provides a way to create configuration file for storing configurable parameters of an application.  Selenium Test Suite also needs such a file to keep global configuration settings.

In this post let us try to understand how we can create a “.properties” file and how to access values from the properties file.

Create a file text file (using any text editor such as notepad) Selconfig.properties (as in the file –view file) and save the file as “Selfconfig.properties

Create classes as in the following code snippets:

package com.selftechy.readdata;/* * Author - Seetaram Hegde *  */import com.selftechy.library.*;import java.io.IOException;public class TestRun {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubconfig cfg = new config("F:\\Helios-Workspace\\Config\\SelConfig.properties");System.out.println(cfg.ReadProperty("ConfigPath"));}}
package com.selftechy.library;/* * Author - Seetaram Hegde *  */import java.io.*;import java.util.*;public class config {  String str, key;  private String filepath;    public config(String filepath){  this.filepath=filepath;  }    public String ReadProperty(String propkey){  String propval="";  try{  int check = 0;  while(check == 0){  check = 1;  File cfgfile = new File(filepath);  if(cfgfile.exists()){  Properties props = new Properties();  FileInputStream propin = new FileInputStream(cfgfile);  props.load(propin);  propval=props.getProperty(propkey);  }  else{  check = 0;  }}  }  catch(IOException e){  e.printStackTrace();  }return propval;    }}

Save both the classes and then execute the first one (TestRun.java) as Java application.  In the Selconfig.properties file, TestData & ConfigPath are the keys and “F:\\Helios-Workspace\\TestData & F:\\Helios-Workspace\\Config” are the respective values.  In .properties file we can put comments using “#”.  Any sentence that starts with # will be considered as comment.


To access the values of any properties, use the “key” parameter.  For example, to access the value of “TestData” and “ConfigPath” write the code as follows:


config cfg = new config("F:\\Helios-Workspace\\Config\\SelConfig.properties");



cfg.ReadProperty("ConfigPath")


cfg.ReadProperty("TestData")


0 0
原创粉丝点击