Создание Dynami c TestNG с использованием селена - PullRequest
0 голосов
/ 24 апреля 2020

Я хочу создать файл testng. xml динамически через код.

Ниже примера тестирования. xml файл должен быть создан с помощью кода.

Используя приведенный ниже код, я смог создать Suite Протестируйте и добавьте файл класса. но я не смог добавить методы с тегом include.

        **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 как показано выше

1 Ответ

0 голосов
/ 26 апреля 2020

Следующий код создаст Dynami c TestNG и запустит его. В этом TestNG мы можем добавить класс прослушивателя, передать параметры, установить параллельное выполнение в режиме методов, указать число потоков выполнения и выбрать методы, которые необходимо выполнить из определенного класса.

код

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