0
votes

I tried best couldn't find a complete instructions on how to config a properties file with Maven,Testng.

Here are what I did and the exception I got:

  1. from TestNG for suite, added

content of the config file: user=testuser password=pswd

  1. pom.xml src/test/resources true
  2. in code: @BeforeTest @Parameters(value = { "config-file" }) public void initFramework(String configfile) throws Exception {

        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...
        }
    

    }

Here is what I got when compile:

org.testng.TestNGException: Parameter 'config-file' is required by @Configuration on method initFramework but has not been marked @Optional or defined

Question: I think I got all options mixed but really wanted a working way to read the parameter for Java/Selenium/TestNG/Maven.

1

1 Answers

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

//Now simply read through property file:-

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

//To write property file:-

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