0

我想通过代码动态创建 testng.xml 文件。

下面的 Sample testng.xml 文件需要通过代码创建。

使用下面的代码,我可以创建套件、测试和添加类文件。但我无法使用包含标签添加方法。

        **XmlSuite suite = new XmlSuite();
        //below line is to add suite
        suite.setName("FirstTest");
        //below line is to set methods in parallel
        suite.setParallel(XmlSuite.ParallelMode.METHODS);
        //below line is to set the threadcount
        suite.setThreadCount(5);
        XmlTest test = new XmlTest();
        test.setName("ChromeTest");
        XmlClass classname = new XmlClass("test2.MainTest1");
        List<XmlClass> list = new ArrayList<XmlClass>();
        list.add(classname);**

请帮我生成如下所示的 testng.xml 以添加方法。我可以上课,但我无法添加方法。请帮我生成 testng.xml 如上所示

4

1 回答 1

0

以下代码将创建一个动态 TestNG 并运行它。在这个TestNG中,我们可以添加Listener Class,传递参数,在Methods模式下设置并行执行,指定执行线程数和选择需要从特定类执行的方法。

代码

    //Creating TestNG object
    TestNG myTestNG = new TestNG();

    //Creating XML Suite
    XmlSuite mySuite = new XmlSuite();

    //Setting the name for XML Suite
    mySuite.setName("My Suite");

    //Setting the XML Suite Parallel execution mode as Methods
    mySuite.setParallel(XmlSuite.ParallelMode.METHODS);

    //Adding the Listener class to the XML Suite
    mySuite.addListener("test.Listener1");

    //Creating XML Test and add the Test to the Suite
    XmlTest myTest = new XmlTest(mySuite);

    //Setting the name for XML Test
    myTest.setName("My Test");

    //Setting the Preserve Order for XML Test to True to execute the Test in Order
    myTest.setPreserveOrder("true");

    //Creating HashMap for setting the Parameters for the XML Test 
    HashMap<String,String> testngParams = new HashMap<String,String> ();
    testngParams.put("browserName", "Chrome"); 
    testngParams.put("browserVersion","81"); 
    myTest.setParameters(testngParams);

    //Creating XML Class
    XmlClass myClass = new XmlClass("test.MainTest1");

    //Creating XML Include in the form of ArrayList to add Multiple Methods which i need to run from the Class
    List<XmlInclude> myMethods = new ArrayList<>();
    myMethods.add(new XmlInclude("method1"));
    myMethods.add(new XmlInclude("method2"));

    //Adding the Methods selected to the my XML Class defined
    myClass.setIncludedMethods(myMethods);

    //Getting the Classes and adding it to the XML Test defined
    myTest.getClasses().add(myClass);

    //Creating XML Suite in the form of ArrayList and adding the list of Suites defined
    List<XmlSuite> mySuitesList = new ArrayList<XmlSuite>();
    mySuitesList.add(mySuite);

    //Adding the XMLSuites selected to the TestNG defined
    myTestNG.setXmlSuites(mySuitesList);

    //Setting the execution Thread Count for Parallel Execution
    mySuite.setThreadCount(10);

    //Setting the Verbose Count for Console Logs
    mySuite.setVerbose(2);

    //Executing the TestNG created
    myTestNG.run();
于 2020-04-26T20:05:36.893 回答