-2

How can I override properties file values from the values in testng.xml file.

Eg: Have 3 properties file 
a.properties
#This property file contains host url and userid and env
hosturl=abcd.com
uid=xyz

similarly other b.properties

Now when I run as a testng.xml suite the parameters need to be picked from the testng.xml which contain same parameters as hosturl and uid.

~nerdlearner

  • Questions asking us to **recommend or find a book, tool, software library, tutorial or other off-site resource** are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, [describe the problem](http://meta.stackoverflow.com/questions/254393) and what has been done so far to solve it. – nyedidikeke Jan 12 '17 at 02:04

1 Answers1

1

Here's how you go about building this.

  • You would first need to build a configuration manager that leverages a library such as apache commons configurations and which feeds off of all of your 3 configuration properties and initialises itself.
  • You then build a TestNG listener which implements either ITestListener (or) ISuiteListener (or) both wherein within the onStart() method, you extract out all the parameters using org.testng.ISuite.getXmlSuite.getParameters() (in the case of ISuiteListener) (or) org.testng.ITestContext.getCurrentXmlTest().getLocalParameters() and then pass it to the configuration manager (built in earlier step) so that it can update itself with the parameters passed either via the suite or via the <test> tag.

Now your tests would merely query the configuration manager passing in the key and get its value. Since the configuration manager is now aware of both the properties file and also the testng xml file, it will work the way you need.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66