Read parameter value from TestNG XML file

     We have some situations in the automation to read parameter values from the TestNG XML file when the engineers use the TestNG framework. The parameter value may be used in different use cases like creating automation driver sessions, during automation execution, and during report generation.

     We can read parameter values from the TestNG XML file in different ways: with testng dependency and without testng dependency.

  • The first way with testng dependency, in this we can use the @Parameters annotation. For example as below:
@Parameters({ "browserName" })

     In the above example it will read the value of the parameter browserName from the TestNG XML file. We usually use this annotation in the test suite class.

  • The second way with testng dependency, in this we can use the ITestContext interface. For example as below:
ITestContext testContext;
XmlTest test = testContext.getCurrentXmlTest();
String browserName = test.getParameter("browserName").toString();

     The above example also read the parameter value from the TestNG XML file and is mostly used in the report creation class where ITestListener is implemented.

  • Third way without testng dependency; uses javax XML parser and W3C DOM to read the parameter value from the TestNG XML file. Here, I would like to share the simple utility and it is a reusable utility, you can use this utility in any class to read the parameter value. Following is the implementation logic:
public String getParameterValue(String keyName) {
String value = null;
try {
File fXmlFile = new File("./testng.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setValidating(false);
dbFactory.setNamespaceAware(true);
dbFactory.setFeature("http://xml.org/sax/features/namespaces", false);
dbFactory.setFeature("http://xml.org/sax/features/validation", false);
dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("parameter");
for (int temp = 0; temp < nList.getLength(); temp++) {
String name = nList.item(temp).getAttributes().getNamedItem("name").getTextContent();
if (name.equalsIgnoreCase(keyName)) {
value = nList.item(temp).getAttributes().getNamedItem("value").getTextContent();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}

NOTE:

  • The testng.xml file should be at project level.
  • Call the method like getParameterValue(“key_name”); for example: getParameterValue(“browsername”);

     I hope you really enjoyed reading this article and got the different ways of reading the parameter value from the TestNG XML file. You can try to use these utilities in your automation flow wherever you have such requirements.

make it perfect!

Leave a comment

Create a website or blog at WordPress.com

Up ↑