How to Create testng.xml from java code

Hi,

I want to create the testng.xml file from scratch through Java code,
The purpose is to build nice java ui and let the qa team to insert data like:

  1. How many devices do you want to run ? (Parallel)
  2. IP number
  3. Port
  4. Test names
    And so on,
    THe purpose is that 100% of the testng.xml will be create before the appium starts,

Anybody doest this ?

Thanks

Hi Abush,

We have done this by steps as mentioned below.

  1. A sample testng.xml file is kept in resources folder.
  2. At runtime read sample testng file and create tags for parameters using the information provided(IP number,Port) etc.Update the classnames tag etc.
  3. Using DocumentBuilder Write output to different xml file

Hope this solves the problem

You need mostly XmlSuite and XmlTest classes, once you instantiate the classes and populate them with the required configuration - you will be able to call XmlSuite.toXml() function and store generated suite into the file of your choice.

Example code:

XmlSuite suite = new XmlSuite();
suite.setName("TestNG XML created from Java Code");
suite.setParallel(XmlSuite.ParallelMode.TESTS);
suite.setThreadCount(10);

XmlTest xmlTest1 = new XmlTest(suite);
xmlTest1.setName("My first test");
xmlTest1.addParameter("foo", "bar");

XmlTest xmlTest2 = new XmlTest(suite);
xmlTest2.setName("My second test");
xmlTest2.addParameter("baz", "qux");

System.out.println(suite.toXml());

Which produces output like:

More information:

I want to create it based on the ammount of device, How can I create the testng.xml with 2 or more tests?
For a few devices ?

I have done something similar. not sure it will covers all the requirements you need. But you will get basic idea.

But testng.xml already does perfect parallel, I don’t want to deal with hub and nodes…
I just want help to be able to edit the testng.xml moment before running it and to edit the device amount/test ls amount…

Check this out.

Wow, Thanks, That looks too complicated for me, I will try…

1 Like