Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public static void createXml() throws Exception {
try {
// Initialization of drivers
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
DocumentBuilder documentBuilder = documentBuilderFactory
Document document = documentBuilder.newDocument();
String xlFilePath = null;
xlFilePath = "Resource\\Initial.xls";
ExcelSheetLibrary lib = new ExcelSheetLibrary(xlFilePath);
// Get the flagged cells with value = "Y" from excel file
// Get the number of parameter to be created in XML
int totalnoofelementsflaggedtorun = lib.flaggedMethod.size();
// Type the suite tag element in the XML file
Element rootElementsuite = document.createElement("suite");
// Type the parameter set of lines in the XML file
for (int elementcounter = 1; elementcounter <= totalnoofelementsflaggedtorun; elementcounter++) {
Element rootElementparameter = document
String[] flagElement = lib.flaggedMethod.get(elementcounter)
rootElementparameter.setAttribute("name", flagElement[0]);
rootElementparameter.setAttribute("value", flagElement[1]);
// Type the root elements in the XML file
Element rootElementtest = document.createElement("test");
Element rootElementClass = document.createElement("classes");
Element childelementClass = document.createElement("class");
// Assign attribute to the root elements
childelementClass.setAttribute("name", "com.example.CalcAppTest");
Element rootElementgroups = document.createElement("methods");
// Assign attribute to the root elements
rootElementsuite.setAttribute("name", "SimpleAppiumFramework");
rootElementtest.setAttribute("name", "testing");
// Append values to the root elements
// rootElementgroups.appendChild(rootElementrun);
// Obtain the column value flag = "Y" in a loop
for (int elementcounter = 1; elementcounter <= totalnoofelementsflaggedtorun; elementcounter++) {
String element = "include";
Element em = document.createElement(element);
String[] flagElement = lib.flaggedMethod.get(elementcounter)
em.setAttribute("name", flagElement[0]);
// Generate the file.
FileWriter fstream = new FileWriter("Resource\\testng.xml");
BufferedWriter out = new BufferedWriter(fstream);
// Generate the required XML output file
TransformerFactory transformerFactory = TransformerFactory
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
// Prints all the Generated Xml code in the File object
StreamResult result = new StreamResult(fstream);
transformer.transform(source, result);
// close the generated file.
} catch (DOMException e) {
* This method runs the XML suite file dynamically
public static void autoRunXml() {
// Create a list
List files = new ArrayList();
// Add the required xml files to the list
// create object for TestNG
TestNG tng = new TestNG();
// Add the list of files to create a suite
// Run the suite;

This comment has been minimized.

Copy link
Owner Author

@executeautomation executeautomation commented Aug 31, 2015

This is the complete working code for TestNG custom library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.