使用Java以编程方式创建TestNg Xml文件

问题描述 投票:0回答:2

我有以下testNg xml文件。

有人可以建议如何使用Java动态创建它吗?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <groups>
     <run>   
       <include name="PrometheusHome" />
       <include name="AlertMgrHome" />
     </run>
   </groups>
    <classes>
      <class name="com.amex.eag.telemetry.testcases.PrometheusTests"/>
      <class name="com.amex.eag.telemetry.testcases.AlertManagerTests"/>
    </classes>
  </test> 
</suite> 
java xml selenium automated-tests testng
2个回答
0
投票

here:的调整后核心数>

 public class DynamicTestNG {
 public void runTestNGTest(Map<String,String> testngParams)
 {   //Create an instance on TestNG 
     TestNG myTestNG = new TestNG();   

     //Create an instance of XML Suite and assign a name for it. 
      XmlSuite mySuite = new XmlSuite(); 
      mySuite.setName("Suite"); 
      mySuite.setParallel(XmlSuite.ParallelMode.METHODS);   

     //Create an instance of XmlTest and assign a name for it.  
     XmlTest myTest = new XmlTest(mySuite); 
     myTest.setName("Test");   
     //add groups
     myTest.addIncludedGroup("PrometheusHome")
     myTest.addIncludedGroup("AlertMgrHome")

     //Add any parameters that you want to set to the Test. 
     myTest.setParameters(testngParams); 

     //Create a list which can contain the classes that you want to run.
     List<XmlClass> myClasses = new ArrayList<XmlClass>();
     myClasses.add(new XmlClass("com.amex.eag.telemetry.testcases.PrometheusTests"));   
     myClasses.add(new XmlClass("com.amex.eag.telemetry.testcases.AlertManagerTests"));   

     //Assign that to the XmlTest Object created earlier. 
     myTest.setXmlClasses(myClasses);   

     //Create a list of XmlTests and add the Xmltest you created earlier to it.
     List<XmlTest> myTests = new ArrayList<XmlTest>(); 
     myTests.add(myTest);   

     //add the list of tests to your Suite. 
     mySuite.setTests(myTests);   

     //Add the suite to the list of suites. 
     List<XmlSuite> mySuites = new ArrayList<XmlSuite>(); 
     mySuites.add(mySuite);   

     //Set the list of Suites to the testNG object you created earlier. 
     myTestNG.setXmlSuites(mySuites);
     mySuite.setFileName("myTemp.xml"); 
     mySuite.setThreadCount(3);   
     myTestNG.run();

     //Create physical XML file based on the virtual XML content 
     for(XmlSuite suite : mySuites) 
     {  
         createXmlFile(suite); 
     }   
     System.out.println("File generated successfully.");   

     //Print the parameter values 
     Map<String,String> params = myTest.getParameters(); 
     for(Map.Entry<String, String> entry : params.entrySet()) 
     { 
       System.out.println(entry.getKey() + " => " + entry.getValue()); 
     }
    }
    //This method will create an Xml file based on the XmlSuite data 
    public void createXmlFile(XmlSuite mSuite) 
    { 
       FileWriter writer; 
       try { 
            writer = new FileWriter(new File("myTemp.xml")); 
            writer.write(mSuite.toXml()); 
            writer.flush(); 
            writer.close(); 
            System.out.println(new File("myTemp.xml").getAbsolutePath());
           } catch (IOException e)
            {
              e.printStackTrace(); 
            }
    }
   //Main Method
   public static void main (String args[]) 
   { 
      DynamicTestNG dt = new DynamicTestNG(); 
      //This Map can hold your testng Parameters. 
      Map<String,String> testngParams = new HashMap<String,String> ();
      testngParams.put("device1", "Desktop"); 
      dt.runTestNGTest(testngParams); 
   }
 }

0
投票

如果您希望以编程方式运行测试,最好阅读现有的testng XML文件,进行一些运行时修改,然后以编程方式运行它。 这基本上可以避免在代码中对类名和测试名进行硬编码的问题

© www.soinside.com 2019 - 2024. All rights reserved.