在自動化測試過程中,經常會有一些公用的屬性要配置,以便后面給腳本使用,我們可以選擇xml, excel或者json格式來存貯這些數據,但其實java本身就提供了properties類來處理properties文件,雖然名字叫properties,其實打開它發現就是一個記事本的文件,所以看起來也比較直觀,下面是解析properties文件的實現代碼。
properties文件里存貯的樣子是這樣的,然后給他保存為xxx.properties即可。
gsBAMUserName1=automation_bam_dg1
gsBAMUserName1_FirstName=DaGuang_BAM
gsBAMUserName1_LastName=Zhu
代碼:
package utilities; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Properties; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; @SuppressWarnings("unused") public class PropertiesOperation { /** * Script Name : <b>PropertiesOperation</b> * Generated : <b>May 21, 2012 2:03:25 PM</b> * Description : Functional Test Script * Original Host : WinNT Version 5.1 Build 2600 (S) */ public static String filename1= "supportfiles/PropertyFiles/Sourcing.properties"; public static String filename2= "supportfiles/PropertyFiles/TestCaseMapping.properties"; public static String filename3 = "supportfiles/PropertyFiles/RFxNumber.properties"; public static String filename4 = "supportfiles/PropertyFiles/RFxNumber.properties"; public static Properties properties; public static FileInputStream inputFile; public static FileOutputStream outputFile; //獲取properties文件里值得主要方法,根據它的key來獲取 public static String getSourcingValueBykey(String key){ String value=""; try{ FileInputStream inputFile = new FileInputStream(filename1); Properties properties = new Properties(); properties.load(inputFile); inputFile.close(); value = properties.getProperty(key); if(value == null || value.equals("")){ System.out.println("The value for key: " + key + " doesn't exist."); System.out.println("Please check the content of the properties file."); } }catch(Exception e){ e.printStackTrace(); } return value; } //Added by Justin 06/13 //Store the new key/value pair to the property file and save the file public static void setRFxNumberBykey(String key, String value){ String description = "Property file for Sourcing Automation"; //filename4 = getRFxNumber(); Properties prop = new Properties(); try{ FileInputStream fis = new FileInputStream(filename4); prop.load(fis); fis.close(); prop.setProperty(key, value); FileOutputStream fos = new FileOutputStream(filename4); prop.store(fos, description); fos.close(); }catch(Exception e){ e.printStackTrace(); } } //Read the RFxNumber property file to get the RFx or Item number saved during execution public static String getRFxNumberValueBykey(String key){ String value=""; //filename4 = getRFxNumber(); try{ FileInputStream inputFile = new FileInputStream(filename4); Properties properties = new Properties(); properties.load(inputFile); inputFile.close(); value = properties.getProperty(key); if(value == null || value.equals("")){ System.out.println("The value for key: " + key + " doesn't exist."); System.out.println("Please check the content of the properties file."); } }catch(Exception e){ e.printStackTrace(); } return value; } //Read the TestCaseMapping property file to get the Spreadsheet excel filename public static String getTestCaseMappingBykey(String key){ String value=""; try{ FileInputStream in = new FileInputStream(filename2); Properties settings = new Properties(); settings.load(in); value = settings.getProperty(key); if(value==null||value.equals("")){ System.out.println("The value for key: " + key + " doesn't exist."); System.out.println("Please check the content of the properties file."); } }catch(Exception e){ e.getMessage(); } return value; } // public static String getRFxNumber(){ // filename4= getSourcingValueBykey("gsAutoDataPath")+"RFxNumber.Properties"; // // System.out.println("filename4:" + filename4); // return filename4; // } public static void main(String[] args) throws IOException { //getRFxNumber(); } }