seleniumtestng.xml

Creating Dynamic TestNG using selenium


I want to create the testng.xml file dynamically through code.

Below the Sample testng.xml file need to be created through code.

Using the below code i could able to create Suite, Test and add Class file. but i could not able to add the methods with include tag.

        **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);**

Kindly help me generate the testng.xml as shown below to add methods. I could able to class but i could not able to add methods. Kindly help me to generate testng.xml as shown above


Solution

  • Following code will Create a Dynamic TestNG and run it. In this TestNG we can add Listener Class, pass Parameters, set Parallel execution in Methods mode, specify the execution Thread Count and Select the Methods which needs to be executed from a particular class.

    Code

        //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();