2015-07-20 24 views
0

我試過最好找不到如何配置一個屬性與Maven,TestNG的文件一個完整的說明設置配置屬性。錯誤上的Java /硒/ TestNG的/ Maven的

下面是我所做的和異常我:

  1. 從TestNG的爲套裝,加

配置文件的內容: 用戶= testuser的 密碼= PSWD

  1. pom.xml src/test/res OURCES 真
  2. 在代碼: @BeforeTest @參數(值= { 「CONFIGFILE」}) 公共無效initFramework(字符串CONFIGFILE)拋出異常 {

    InputStream stream = Config.class.getResourceAsStream("/config.properties"); 
        Properties properties = new Properties(); 
        try { 
         properties.load(stream); 
         String user = properties.getProperty("user"); 
         String password = properties.getProperty("password"); 
         System.out.println("\nGot User FirstName+LastName shows as:"+ user +"\n" + password + "==========="); 
    
        } catch (IOException e) { 
         e.printStackTrace(); 
         // You will have to take some action here... 
        } 
    

    }

這是我編譯時得到的:

org.testng.TestNGException: 參數「配置文件」要求通過@Configuration的方法initFramework但仍未標記@optional或定義

問: 我覺得我得到了混合所有選項,但真想工作方式來讀取Java/Selenium/TestNG/Maven的參數。

回答

0
Properties CONFIG= new Properties(); 
FileInputStream ip = new FileInputStream("C://config.properties"); 
CONFIG.load(ip); 

//現在只需通過屬性文件閱讀: -

String user = CONFIG.getProperty("user"); 
String password = CONFIG.getProperty("password"); 

//要寫入屬性文件: -

CONFIG.setProperty("user","newbie1"); 
CONFIG.setProperty("password","secret123"); 
+0

這是超快速的答案,它的作品!我會接受答案。非常感謝 ! – newbie1

+0

如果在測試/ Mavan任何方式實施這將是更好,如果有任何:-) – newbie1