Site Loader
Auckland, New Zealand
In the last post we tried to run multiple Appium test using TestNG suite by manually creating the TestNG.xml file. In this post we are going to go a little further to create a custom library for
  • Reading the method names from excel sheet which is flagged to run
  • Dynamically creating xml file
  • Running the test using testng.xml file using TestNG API
This way we can reduce the painful manual effort of creating custom TestNG xml file rather we can control the same via a single excel sheet for running a group of methods. Please find below the complete video of the above discussion Please find below the complete code for the above video

Custom TestNG Library Code

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;


Code to Read Flagged methods

	public static Hashtable GetFlaggedMethods(String ColumnName) {
		try {
			// Load all the dictionary
			int methodcount = 1; // keycount flag will track of
									// method count
			for (int row = 0; row < RowCount(); row++) {
				if (ReadCell(ColumnName, row).equals("Y")) {
					// put method keycount and the method name
					flaggedMethod.put(methodcount, ReadCell("MethodName", row)
							+ ";" + ReadCell("ExcelSheetName", row));

		} catch (Exception e) {

		return flaggedMethod;
Thanks for reading the post and watching the video !!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK

Post Author: Karthik kk

4 Replies to “Custom TestNG library for Appium”

  1. Can you please explain how we can add multiple test cases to childElementClass.setAttribute. Above code is working just for single Testclass. ??

  2. Hi, I have one question on TestNG.
    I have few tests , before starting the tests it is executing before class method where it will do some actions on the server.
    Sometimes this @Beforeclass it self is failing and causing all test cases to skip which is expected.
    Is there any way to take screen shot on @BeforeClass failure?
    This is working fine for @Test methods.

    Please provide your valuable suggestions.

  3. Amazing video, i was looking for such amazing solution since a long time. I have successfully implemented this 🙂 many thanks.

    I have few queries , would be great if you please answer.

    If we run as java file from main method, how the testng report will be generated. I can see out puts on IDE console but no testng report was generated

  4. In this method, its perfect. I am facing one issue.

    I am passing data rows for each function from excel, now what is happening, for example
    i want to run

    scenario1: data row 1,3
    scenario2: data row1

    As test cases are in xml it executing both scenairos , how can we handel to execute scenario 1 only for two time and scenario2 one time

Leave a Reply

Your email address will not be published. Required fields are marked *