Generate TestNg.xml Dynamically

         Generate TestNg.xml Dynamically

We can create a virtual testng.xml file yourself. In order to do this, you can use the classes found the packageorg.testng.xml: XmlClass, XmlTest, etc... Each of these classes correspond to their XML tag counterpart.

For example, suppose you want to create the following virtual file:
<suite name="TmpSuite" >
  <test name="TmpTest" >
    <classes>
      <class name="test.failures.Child"  />
    <classes>
    </test>
</suite>
You would use the following code:

XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");

XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;


And then you can pass this XmlSuite to TestNG:

List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();


Program
package org.nitin;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class TCase1 {

 @Test
    public void TCase()
     {
  System.setProperty("webdriver.chrome.driver","C:\\Documents and Settings\\Administrator\\My Documents\\Downloads\\chromedriver_win32_2.1\\chromedriver.exe");
   ChromeDriver f = new ChromeDriver();
   f.get("http://mail.in.com");
   f.findElementById("f_id").sendKeys("uname");
   f.findElementById("f_pwd").sendKeys("pass");
   f.findElementByClassName("signin");
   XmlSuite suite = new XmlSuite();
   suite.setName("OMS");
   XmlTest test1 = new XmlTest(suite);
   test1.setName("TCase1");
   List<XmlClass> classes = new ArrayList<XmlClass>();
   classes.add(new XmlClass("org.nitin.TCase1"));
   List<XmlSuite> suite_a = new ArrayList<XmlSuite>();
   suite_a.add(suite);
   TestNG tng = new TestNG();
   tng.setXmlSuites(suite_a);
   tng.run();
   /*XmlSuite suite = new XmlSuite();
   suite.setName("OMS");
   
   XmlTest test = new XmlTest(suite);
   test.setName("Case1");
   List<XmlClass> classes = new ArrayList<XmlClass>();
   classes.add(new XmlClass("org.nitin.TCase1"));
   test.setXmlClasses(classes) ;
   List<XmlSuite> suites = new ArrayList<XmlSuite>();
   suites.add(suite);
   TestNG tng = new TestNG();
   tng.setXmlSuites(suites);
   tng.run();
 */

  }

}


14 comments :

14 comments :

  1. Sir what is the benefit of writing code for creating Testng.xml.?

    ReplyDelete
  2. The blog gave me idea to generate testNG.xml file dynamically My sincere thanks for sharing this post
    Selenium Training in Chennai

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. What if i want to implement a listeners tag

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Excellent way of writing and expressing your thoughts and ideas to the readers, very much impressed in your article. Keep doing more, waiting to read your next blog.
    Regards:
    software testing course in chennai
    software testing training institute chennai

    ReplyDelete
  9. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    nebosh course in chennai

    ReplyDelete
  10. I am really thankful for posting such useful information. It really made me understand lot of important concepts in the topic. Keep up the good work!
    Oracle Training in Chennai | Oracle Course in Chennai

    ReplyDelete