使用 selenium 创建动态 TestNG
Creating Dynamic TestNG using selenium
我想通过代码动态创建 testng.xml 文件。
下面的示例testng.xml文件需要通过代码创建。
使用下面的代码我可以创建套件、测试并添加 Class 文件。但我无法添加带有 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添加方法。我可以 class 但我无法添加方法。请帮我生成 testng.xml 如上所示
以下代码将创建动态 TestNG 并 运行 它。在这个 TestNG 中,我们可以添加 Listener Class,传递参数,在方法模式下设置并行执行,指定执行线程数和 Select 需要从特定 class 执行的方法。
代码
//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();
我想通过代码动态创建 testng.xml 文件。
下面的示例testng.xml文件需要通过代码创建。
使用下面的代码我可以创建套件、测试并添加 Class 文件。但我无法添加带有 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添加方法。我可以 class 但我无法添加方法。请帮我生成 testng.xml 如上所示
以下代码将创建动态 TestNG 并 运行 它。在这个 TestNG 中,我们可以添加 Listener Class,传递参数,在方法模式下设置并行执行,指定执行线程数和 Select 需要从特定 class 执行的方法。
代码
//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();