[testng-dev] Need help on creating 'testng.xml' file programatically

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[testng-dev] Need help on creating 'testng.xml' file programatically

Saran S
Below is the scenario:

I have an excel document which contains TestCase Name, for ex., 'verifyHomePage', 'verifyLoginPage'. I have to read those TestCase Name from that excel file and pass it to 'testng.xml' file and it has to execute only those particular methods.

Ex.: In a Java file (TestNG class file), I have 3 methods: 'verifyHomePage', 'verifyLoginPage' and 'verifyAccountsPage'. In the excel I have mentioned only two method names: 'verifyHomePage', 'verifyLoginPage', so it should execute only those 2 methods.

Below is my code:

package testngpackage;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;


import testpackage2.ReadTestCaseNameFromExcel;


public class TestNG_FS {
   
public static ArrayList<XmlClass> classes = new ArrayList<XmlClass>();


   
public static void main(String[] args) throws IOException {
       
XmlSuite suite = new XmlSuite();
        suite
.setName("Test Suite");
        suite
.setThreadCount(10);


       
XmlTest test = new XmlTest(suite);
        test
.setName("Test");
        test
.setPreserveOrder("true");


       
List<XmlClass> classNames = getClassNames();
       
int classSize = classNames.size();
       
System.out.println("Class size: " + classSize);


       
List<String> methodNames = ReadTestCaseNameFromExcel.readScenarioName(); //This is another class file I am using to fetch the method names from excel document, it is working fine.
         
int methodSize = methodNames.size();
         
System.out.println("Total no of methods: " + methodSize);


       
for (int i = 0; i < classNames.size(); i++) {
           
XmlClass className = classNames.get(i);
           
System.out.println("Class Name: " + className);
                       
           
for (int j = 0; j < methodNames.size(); j++) {
               
String methodName = methodNames.get(j);
           
               
List<XmlInclude> methodsToRun = new ArrayList<XmlInclude>();
                methodsToRun
.add(new XmlInclude(methodName));
                className
.setIncludedMethods(methodsToRun);
           
               
List<XmlSuite> suites = new ArrayList<XmlSuite>();
                suites
.add(suite);
               
                test
.setXmlClasses(classes);


               
/*TestNG testng = new TestNG();
                testng.setXmlSuites(suites);
                testng.run();*/



               
File file = new File("C:/Temp/TestNG.xml");
               
FileWriter writer = new FileWriter(file);
                writer
.write(suite.toXml());
               
System.out.println(suite.toXml());
                writer
.close();    
           
}
       
}    
   
}


   
public static List<XmlClass> getClassNames() {


       
XmlClass groupingClass = new XmlClass();
        groupingClass
.setName("testngpackage.TestNG_Grouping");
        classes
.add(groupingClass);


       
XmlClass classDemo1 = new XmlClass();
        classDemo1
.setName("testngpackage.TestNG_ClassDemo1");
        classes
.add(classDemo1);


       
return classes;
   
}
}


I am using another class file 'ReadTestCaseNameFromExcel​' with the method name 'readScenarioName​' to read the excel document to fetch the method names, it is Working Fine.

And also I am using 'getClassNames' (above code) method to pass the Class names.

I am getting the output as below:

Class size: 2


Total no of methods: 3
Class Name: [XmlClass class=testngpackage.TestNG_Grouping]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">
       
<methods>
         
<include name="cars_sedan"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
     
<class name="testngpackage.TestNG_ClassDemo1"/>
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">
        <methods>
          <include name="
train_Local"/>
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
      <class name="
testngpackage.TestNG_ClassDemo1"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">
       
<methods>
         
<include name="flight_Domestic"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
     
<class name="testngpackage.TestNG_ClassDemo1"/>
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


Class Name: [XmlClass class=testngpackage.TestNG_ClassDemo1]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">
        <methods>
          <include name="
flight_Domestic"/>
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
      <class name="
testngpackage.TestNG_ClassDemo1">
        <methods>
          <include name="
cars_sedan"/>
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">
       
<methods>
         
<include name="flight_Domestic"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
     
<class name="testngpackage.TestNG_ClassDemo1">
       
<methods>
         
<include name="train_Local"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_ClassDemo1 -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">
        <methods>
          <include name="
flight_Domestic"/>
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
      <class name="
testngpackage.TestNG_ClassDemo1">
        <methods>
          <include name="
flight_Domestic"/>
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


I am expecting the result as: One class name should be passed at a time in testng.xml file and it should iterate through all the methods from excel document as below:

My expected output is:

Class size: 2


Total no of methods: 3
Class Name: [XmlClass class=testngpackage.TestNG_Grouping]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">        (First class file - only one class file for testng.xml)
       
<methods>
         
<include name="cars_sedan"/>                     (First method name from excel file)
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">        (First class file)
        <methods>
          <include name="
train_Local"/>                    (Second method name from excel file)
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">        (First class file)
       
<methods>
         
<include name="flight_Domestic"/>                (Third method name from excel file)
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


Class Name: [XmlClass class=testngpackage.TestNG_ClassDemo1]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_ClassDemo1">    (Second class file)
        <methods>
          <include name="
cars_sedan"/>                    (First method name from excel file)
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_ClassDemo1">    (Second class file)
       
<methods>
         
<include name="train_Local"/>                    (Second method name from excel file)
       
</methods>
      </
class> <!-- testngpackage.TestNG_ClassDemo1 -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_ClassDemo1">    (Second class file)
        <methods>
          <include name="
flight_Domestic"/>                (Third method name from excel file)
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


Could anyone please give me any suggestion or code to achieve this?

Thanks in advance

--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-dev.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [testng-dev] Need help on creating 'testng.xml' file programatically

Julien Herr
Hi,

You should post on the testng-users list instead.

Regards,
Julien

Le ven. 18 août 2017 à 18:25, Saran S <[hidden email]> a écrit :
Below is the scenario:

I have an excel document which contains TestCase Name, for ex., 'verifyHomePage', 'verifyLoginPage'. I have to read those TestCase Name from that excel file and pass it to 'testng.xml' file and it has to execute only those particular methods.

Ex.: In a Java file (TestNG class file), I have 3 methods: 'verifyHomePage', 'verifyLoginPage' and 'verifyAccountsPage'. In the excel I have mentioned only two method names: 'verifyHomePage', 'verifyLoginPage', so it should execute only those 2 methods.

Below is my code:

package testngpackage;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;


import testpackage2.ReadTestCaseNameFromExcel;


public class TestNG_FS {
   
public static ArrayList<XmlClass> classes = new ArrayList<XmlClass>();


   
public static void main(String[] args) throws IOException {
       
XmlSuite suite = new XmlSuite();
        suite
.setName("Test Suite");
        suite
.setThreadCount(10);


       
XmlTest test = new XmlTest(suite);
        test
.setName("Test");
        test
.setPreserveOrder("true");


       
List<XmlClass> classNames = getClassNames();
       
int classSize = classNames.size();
       
System.out.println("Class size: " + classSize);


       
List<String> methodNames = ReadTestCaseNameFromExcel.readScenarioName(); //This is another class file I am using to fetch the method names from excel document, it is working fine.
         
int methodSize = methodNames.size();
         
System.out.println("Total no of methods: " + methodSize);


       
for (int i = 0; i < classNames.size(); i++) {
           
XmlClass className = classNames.get(i);
           
System.out.println("Class Name: " + className);
                       
           
for (int j = 0; j < methodNames.size(); j++) {
               
String methodName = methodNames.get(j);
           
               
List<XmlInclude> methodsToRun = new ArrayList<XmlInclude>();
                methodsToRun
.add(new XmlInclude(methodName));
                className
.setIncludedMethods(methodsToRun);
           
               
List<XmlSuite> suites = new ArrayList<XmlSuite>();
                suites
.add(suite);
               
                test
.setXmlClasses(classes);


               
/*TestNG testng = new TestNG();
                testng.setXmlSuites(suites);
                testng.run();*/



               
File file = new File("C:/Temp/TestNG.xml");
               
FileWriter writer = new FileWriter(file);
                writer
.write(suite.toXml());
               
System.out.println(suite.toXml());
                writer
.close();    
           
}
       
}    
   
}


   
public static List<XmlClass> getClassNames() {


       
XmlClass groupingClass = new XmlClass();
        groupingClass
.setName("testngpackage.TestNG_Grouping");
        classes
.add(groupingClass);


       
XmlClass classDemo1 = new XmlClass();
        classDemo1
.setName("testngpackage.TestNG_ClassDemo1");
        classes
.add(classDemo1);


       
return classes;
   
}
}


I am using another class file 'ReadTestCaseNameFromExcel​' with the method name 'readScenarioName​' to read the excel document to fetch the method names, it is Working Fine.

And also I am using 'getClassNames' (above code) method to pass the Class names.

I am getting the output as below:

Class size: 2


Total no of methods: 3
Class Name: [XmlClass class=testngpackage.TestNG_Grouping]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">
       
<methods>
         
<include name="cars_sedan"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
     
<class name="testngpackage.TestNG_ClassDemo1"/>
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">
        <methods>
          <include name="
train_Local"/>
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
      <class name="
testngpackage.TestNG_ClassDemo1"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">
       
<methods>
         
<include name="flight_Domestic"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
     
<class name="testngpackage.TestNG_ClassDemo1"/>
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


Class Name: [XmlClass class=testngpackage.TestNG_ClassDemo1]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">
        <methods>
          <include name="
flight_Domestic"/>
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
      <class name="
testngpackage.TestNG_ClassDemo1">
        <methods>
          <include name="
cars_sedan"/>
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">
       
<methods>
         
<include name="flight_Domestic"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
     
<class name="testngpackage.TestNG_ClassDemo1">
       
<methods>
         
<include name="train_Local"/>
       
</methods>
      </
class> <!-- testngpackage.TestNG_ClassDemo1 -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">
        <methods>
          <include name="
flight_Domestic"/>
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
      <class name="
testngpackage.TestNG_ClassDemo1">
        <methods>
          <include name="
flight_Domestic"/>
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


I am expecting the result as: One class name should be passed at a time in testng.xml file and it should iterate through all the methods from excel document as below:

My expected output is:

Class size: 2


Total no of methods: 3
Class Name: [XmlClass class=testngpackage.TestNG_Grouping]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">        (First class file - only one class file for testng.xml)
       
<methods>
         
<include name="cars_sedan"/>                     (First method name from excel file)
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_Grouping">        (First class file)
        <methods>
          <include name="
train_Local"/>                    (Second method name from excel file)
        </methods>
      </class> <!-- testngpackage.TestNG_Grouping -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_Grouping">        (First class file)
       
<methods>
         
<include name="flight_Domestic"/>                (Third method name from excel file)
       
</methods>
      </
class> <!-- testngpackage.TestNG_Grouping -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


Class Name: [XmlClass class=testngpackage.TestNG_ClassDemo1]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_ClassDemo1">    (Second class file)
        <methods>
          <include name="
cars_sedan"/>                    (First method name from excel file)
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="
1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Test Suite">
 
<test name="Test">
   
<classes>
     
<class name="testngpackage.TestNG_ClassDemo1">    (Second class file)
       
<methods>
         
<include name="train_Local"/>                    (Second method name from excel file)
       
</methods>
      </
class> <!-- testngpackage.TestNG_ClassDemo1 -->
   
</classes>
  </
test> <!-- Test -->
</suite> <!-- Test Suite -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:/
/testng.org/testng-1.0.dtd">
<suite thread-count="
10" name="Test Suite">
  <test name="
Test">
    <classes>
      <class name="
testngpackage.TestNG_ClassDemo1">    (Second class file)
        <methods>
          <include name="
flight_Domestic"/>                (Third method name from excel file)
        </methods>
      </class> <!-- testngpackage.TestNG_ClassDemo1 -->
    </classes>
  </test> <!-- Test -->
</suite> <!-- Test Suite -->


Could anyone please give me any suggestion or code to achieve this?

Thanks in advance

--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-dev.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-dev.
For more options, visit https://groups.google.com/d/optout.