2015-07-20 64 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