Best Testng code snippet using org.testng.xml.XmlRun
Source:AuthorXMLBuilder.java
...12import org.testng.annotations.Test;13import org.testng.xml.XmlClass;14import org.testng.xml.XmlGroups;15import org.testng.xml.XmlInclude;16import org.testng.xml.XmlRun;17import org.testng.xml.XmlSuite;18import org.testng.xml.XmlSuite.FailurePolicy;19import org.testng.xml.XmlSuite.ParallelMode;20import org.testng.xml.XmlTest;21import com.textura.framework.annotations.Author;22import com.textura.framework.objects.main.Page;23/**24 * @author gvalaval25 * Purpose:To automate the process of writing test cases into XML followed by Jenkins job picking up XML and trigger a run.26 */27public class AuthorXMLBuilder {28 public static void getTestcasesAndWriteIntoXml(String[] author) {29 Map<String, List<String>> suiteWithParallelTests = new HashMap<String, List<String>>();30 Map<String, List<String>> suiteWithExternalExecTests = new HashMap<String, List<String>>();31 Map<String, List<String>> suiteWithProductionIssuesTests = new HashMap<String, List<String>>();32 Map<String, List<String>> suiteWithObsoleteTests = new HashMap<String, List<String>>();33 Map<String, List<String>> suiteWithDateTimeChangeTests = new HashMap<String, List<String>>();34 String[] suites = null;35 try {36 suites = getAllSuites();37 } catch (IOException e1) {38 e1.printStackTrace();39 }40 try {41 getTestcases(author, suites, suiteWithParallelTests, suiteWithExternalExecTests, suiteWithProductionIssuesTests,42 suiteWithObsoleteTests,43 suiteWithDateTimeChangeTests);44 } catch (ClassNotFoundException e) {45 e.printStackTrace();46 }47 try {48 generateXml(suiteWithParallelTests, "parallelTests");49 } catch (IOException e) {50 e.printStackTrace();51 }52 try {53 generateXml(suiteWithDateTimeChangeTests, "dateTimeChangeTests");54 } catch (IOException e) {55 e.printStackTrace();56 }57 try {58 generateXml(suiteWithExternalExecTests, "externalExecTests");59 } catch (IOException e) {60 e.printStackTrace();61 }62 try {63 generateXml(suiteWithProductionIssuesTests, "productionIssuesTests");64 } catch (IOException e) {65 e.printStackTrace();66 }67 try {68 generateXml(suiteWithObsoleteTests, "obsoleteTests");69 } catch (IOException e) {70 e.printStackTrace();71 }72 }73 // To get all suite names from a properties file74 public static String[] getAllSuites() throws IOException {75 FileReader reader = new FileReader("C:\\Automation\\Textura\\CPM\\src\\main\\config\\TestSuites.properties");76 Properties p = new Properties();77 p.load(reader);78 String allClasses = p.getProperty("TestSuites");79 String[] classesArray = allClasses.split(",");80 return classesArray;81 }82 // To get all the test cases of mentioned authors83 public static void getTestcases(String[] selectedAuthor,String[] classes, Map<String, List<String>> suiteWithParallelTests,84 Map<String, List<String>> suiteWithExternalExecTests, Map<String, List<String>> suiteWithProductionIssuesTests,85 Map<String, List<String>> suiteWithObsoleteTests, Map<String, List<String>> suiteWithDateTimeChangeTests)86 throws ClassNotFoundException {87 Method[] methods = null;88 for (int j = 0; j < classes.length; j++) {89 Class eachClass = Class.forName("com.textura.cpm.testsuites." + classes[j]);90 methods = eachClass.getMethods();91 List<String> parallelTestcases = new ArrayList<String>();92 List<String> externalExecTestcases = new ArrayList<String>();93 List<String> productionIssuesTestcases = new ArrayList<String>();94 List<String> obsoleteTestcases = new ArrayList<String>();95 List<String> dateTimeChangeTestcases = new ArrayList<String>();96 for (Method m : methods) {97 if (m.isAnnotationPresent(Author.class)) {98 if (m.isAnnotationPresent(Test.class)) {99 Test testAnnotation = m.getAnnotation(Test.class);100 Author author = m.getAnnotation(Author.class);101 for (String auth : selectedAuthor) {102 if (author.name().equalsIgnoreCase(auth)) {103 String[] groups = testAnnotation.groups();104 if(groups.length==0){105 parallelTestcases.add(m.getName());106 suiteWithParallelTests.put(classes[j], parallelTestcases);107 } else {108 for (String groupName : groups) {109 if (groupName.equals("ExternalExec.All")){110 externalExecTestcases.add(m.getName());111 suiteWithExternalExecTests.put(classes[j], externalExecTestcases);112 }113 if (groupName.equals("ProductionIssues")){114 productionIssuesTestcases.add(m.getName());115 suiteWithProductionIssuesTests.put(classes[j], productionIssuesTestcases);116 }117 if (groupName.equals("ObsoleteTestCases")){118 obsoleteTestcases.add(m.getName());119 suiteWithObsoleteTests.put(classes[j], obsoleteTestcases);120 }121 if (groupName.equals("DateTimeChange")){122 dateTimeChangeTestcases.add(m.getName());123 suiteWithDateTimeChangeTests.put(classes[j], dateTimeChangeTestcases);124 }125 }126 }127 }128 }129 }130 }131 }132 }133 // Suite wise Test cases count and test case ID's134 Page.printFormattedMessage("Parallel Tests====================================");135 int parallelTestsCount = getTestcasesData(suiteWithParallelTests);136 Page.printFormattedMessage("External Exec Tests===============================");137 int externalExecTestsCount = getTestcasesData(suiteWithExternalExecTests);138 Page.printFormattedMessage("Production Issues ================================");139 int productionIssuesTestsCount = getTestcasesData(suiteWithProductionIssuesTests);140 Page.printFormattedMessage("Obsolete Tests====================================");141 int obsoleteTestsCount = getTestcasesData(suiteWithObsoleteTests);142 Page.printFormattedMessage("Date Time Change Tests============================");143 int dateTimeChangeTestsCount = getTestcasesData(suiteWithDateTimeChangeTests);144 // Overall Test cases count145 Page.printFormattedMessage("==================================================");146 int serialTestsCount = externalExecTestsCount + productionIssuesTestsCount + obsoleteTestsCount + dateTimeChangeTestsCount;147 int totalAutomatedCount = parallelTestsCount + serialTestsCount;148 Page.printFormattedMessage("Total Automated Tests Count :" + totalAutomatedCount);149 Page.printFormattedMessage("Parallel Tests Count :" + parallelTestsCount);150 Page.printFormattedMessage("Serial Tests Count :" + serialTestsCount);151 Page.printFormattedMessage("ExternalExec Tests Count :" + externalExecTestsCount);152 Page.printFormattedMessage("ProductionIssues Tests Count :" + productionIssuesTestsCount);153 Page.printFormattedMessage("Obsolete Tests Count :" + obsoleteTestsCount);154 Page.printFormattedMessage("DateTimeChange Tests Count :" + dateTimeChangeTestsCount);155 Page.printFormattedMessage("==================================================");156 }157 // Provides Suite names,test case ID's,test cases count158 public static int getTestcasesData(Map<String, List<String>> suiteTests) {159 int count = 0;160 Set<String> allkeys = suiteTests.keySet();161 for (String keyeach : allkeys) {162 Page.printFormattedMessage("Suite name:" + keyeach);163 Page.printFormattedMessage("Test cases count:" + suiteTests.get(keyeach).size());164 String value = "";165 for (String val : suiteTests.get(keyeach)) {166 value = value + val + ",";167 count++;168 }169 Page.printFormattedMessage("Test cases:" + value);170 }171 Page.printFormattedMessage("Total count:" + count);172 return count;173 }174 // To generate and write data into XML175 public static void generateXml(Map<String, List<String>> suiteTests, String xmlName) throws IOException {176 XmlSuite suite = new XmlSuite();177 suite.setThreadCount(25);178 suite.setConfigFailurePolicy(FailurePolicy.CONTINUE);179 suite.setGuiceStage("DEVELOPMENT");180 suite.setVerbose(0);181 suite.setName("Failed suite [Failed suite [Automation Test Suite Custom]]");182 if (xmlName.equals("parallelTests")) {183 suite.setParallel(ParallelMode.METHODS);184 }185 suite.setJunit(false);186 suite.setSkipFailedInvocationCounts(false);187 suite.setDataProviderThreadCount(10);188 suite.setGroupByInstances(false);189 suite.setAllowReturnValues(false);190 XmlTest test = new XmlTest(suite);191 test.setName("Automation Test: Custom Test Suite(failed)(failed)");192 if (xmlName.equals("parallelTests")) {193 test.setParallel(ParallelMode.METHODS);194 }195 test.setJunit(false);196 test.setSkipFailedInvocationCounts(false);197 test.setGroupByInstances(false);198 test.setAllowReturnValues(false);199 String[] groupNames = { "ExternalExec.All", "ProductionIssues", "ObsoleteTestCases", "DateTimeChange" };200 XmlGroups xmlGroups = new XmlGroups();201 XmlRun xmlRun = new XmlRun();202 if (xmlName.equals("parallelTests")) {203 for (String group : groupNames) {204 xmlRun.onExclude(group);205 }206 xmlGroups.setRun(xmlRun);207 for (String group : groupNames) {208 test.addExcludedGroup(group);209 }210 }211 if (xmlName.equals("externalExecTests")) {212 xmlRun.onInclude("ExternalExec.All");213 xmlGroups.setRun(xmlRun);214 test.addIncludedGroup("ExternalExec.All");215 }...
Source:TestStarter.java
...9import org.slf4j.Logger;10import org.slf4j.LoggerFactory;11import org.testng.*;12import org.testng.xml.XmlPackage;13import org.testng.xml.XmlRun;14import org.testng.xml.XmlSuite;15import org.testng.xml.XmlTest;16import java.nio.file.Paths;17import java.util.ArrayList;18import java.util.List;19import java.util.Map;20/**21 * Created by afalko, smulakala, minho.park on 7/14/16.22 */23public class TestStarter {24 private final static Logger log = LoggerFactory.getLogger(TestStarter.class);25 private final static String TEST_PATH = "/tmp/test-results/test-results/";26 private final static String TEST_RESULTS_PATH = Paths.get(TEST_PATH,"test-results.xml").toString();27 public static void main(String[] args) {28 TestStarter starter = new TestStarter();29 starter.start();30 }31 public void start() {32 IReporter stdoutReporter = new StdOutReporter();33 TestListenerAdapter tla = new TestListenerAdapter();34 TestNG testNG = new TestNG();35 testNG.setOutputDirectory(TEST_PATH);36 testNG.getReporters().add(stdoutReporter);37 testNG.setXmlSuites(this.getXmlSuites());38 testNG.addListener(tla);39 testNG.run();40 if (testNG.hasFailure()) {41 log.error("Test(s) have failed see output above");42 System.exit(2);43 }44 }45 /**46 * This will provide a test suite for TestNG which will be based on the package.47 * It delegates any class inspection / reflection to TestNG.48 *49 * @return the XmlSuite for com.salesforce.dockerfileimageupdate.itest.tests.*50 */51 private List<XmlSuite> getXmlSuites() {52 XmlSuite suite = new XmlSuite();53 suite.setName("Full Integration Test");54 XmlTest test = new XmlTest(suite);55 test.setName("all-tests");56 XmlRun xmlRun = new XmlRun();57 xmlRun.onInclude(test.getName());58 List<XmlPackage> packages = new ArrayList<>();59 packages.add(new XmlPackage("com.salesforce.dockerfileimageupdate.itest.tests.*"));60 test.setXmlPackages(packages);61 List<XmlSuite> suites = new ArrayList<>();62 suites.add(suite);63 return suites;64 }65 /**66 * TestNG reporter that will output what our tests produce67 */68 private class StdOutReporter implements IReporter {69 @Override70 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String huh) {...
Source:DriverScript.java
...10import org.apache.log4j.Logger;11import org.testng.TestNG;12import org.testng.xml.XmlGroups;13import org.testng.xml.XmlPackage;14import org.testng.xml.XmlRun;15import org.testng.xml.XmlSuite;16import org.testng.xml.XmlSuite.ParallelMode;17import org.testng.xml.XmlTest;1819public class DriverScript {2021 22 /**23 * This driver script for Web Automation project, it is main class of this24 * project. It gets following details from Jenkins job and create TestNG.xml25 * file programmatically. Created TestNG.xml would be available with name26 * programmedTestNG.xml in root directory of the project. It contains27 * details of test cases for execution.28 * 29 */30 public static String platform = null;31 public static final Logger logger = Logger.getLogger(DriverScript.class.getName());3233 public static void main(String[] args) throws InterruptedException, IOException {3435 logger.info("Env : " + System.getProperty("Env"));36 logger.info("Browser :" + System.getProperty("Browser"));37 logger.info("TestSuite : " + System.getProperty("TestSuite"));3839 // Create Automation Suite40 XmlSuite xmlSuite = new XmlSuite();41 xmlSuite.setName("Automation Suite");42 xmlSuite.setParallel(ParallelMode.TESTS);43 xmlSuite.setThreadCount(1);44 xmlSuite.setVerbose(1);4546 // Add listeners to test suite47 List<String> listeners = new ArrayList<String>();48 listeners.add("com.assignment.util.LoggingListener");49 listeners.add("org.uncommons.reportng.HTMLReporter");50 listeners.add("org.uncommons.reportng.JUnitXMLReporter");51 xmlSuite.setListeners(listeners);5253 // Create Test Suite54 String testName = null;55 testName = System.getProperty("Browser");56 Map<String, String> parameters = new HashMap<String, String>();57 parameters.put("testName", testName);58 createTest(xmlSuite, testName, parameters);59 createTestNGXmlFile(xmlSuite, "programmedTestNG.xml");6061 // Configure and Run TestNG62 TestNG testNG = new TestNG();63 List<XmlSuite> suites = new ArrayList<XmlSuite>();64 suites.add(xmlSuite);65 testNG.setXmlSuites(suites);66 testNG.run();67 }6869 public static XmlSuite createTest(XmlSuite xmlSuite, String testName, Map<String, String> parameters) {70 // Set Smoke or Regression Test Suite71 XmlTest xmlTest = new XmlTest(xmlSuite);72 xmlTest.setName(testName);73 // xmlTest.addParameter("testName", testName);74 xmlTest.setPreserveOrder(true);75 xmlTest.setParameters(parameters);76 xmlTest.setSuite(xmlSuite);7778 List<XmlPackage> xmlPackages = new ArrayList<XmlPackage>();79 XmlPackage xmlPackage = new XmlPackage("com.assignment.tests");80 xmlPackages.add(xmlPackage);81 xmlTest.setPackages(xmlPackages);8283 List<XmlTest> tests = new ArrayList<XmlTest>();84 tests.add(xmlTest);85 xmlSuite.setTests(tests);8687 // Group: Smoke or Regression88 XmlGroups group = new XmlGroups();89 XmlRun xmlRun = new XmlRun();90 xmlRun.onInclude(System.getProperty("TestSuite"));91 group.setRun(xmlRun);92 xmlTest.setGroups(group);93 return xmlSuite;94 }9596 public static void createTestNGXmlFile(XmlSuite xmlSuite, String fileName) {97 // Generate TestNG file created by programmatically.98 File file = new File(fileName);99 FileWriter writer;100 try {101 writer = new FileWriter(file);102 writer.write(xmlSuite.toXml());103 writer.close();
...
Source:IssueTest.java
...11import org.testng.TestNG;12import org.testng.annotations.Test;13import org.testng.xml.XmlGroups;14import org.testng.xml.XmlPackage;15import org.testng.xml.XmlRun;16import org.testng.xml.XmlSuite;17import org.testng.xml.XmlTest;18import test.SimpleBaseTest;19import java.util.Collections;20import java.util.Random;21import java.util.concurrent.TimeUnit;22public class IssueTest extends SimpleBaseTest {23 private static final Random random = new Random();24 @Test(description = "GITHUB-2232", invocationCount = 10)25 //This test case doesn't vet out the fix completely because the bug by itself is very26 //sporadic and is not easy to reproduce. That is why this test is being executed 10 times27 // to ensure that the issue can be reproduced in one of the executions28 public void ensureNoNPEThrownWhenRunningGroups() throws InterruptedException {29 TestNG testng = create(constructSuite());30 testng.run();31 assertThat(testng.getStatus()).isEqualTo(0);32 TimeUnit.MILLISECONDS.sleep(random.nextInt(5000));33 }34 private XmlSuite constructSuite() {35 XmlSuite xmlsuite = createXmlSuite("2232_suite");36 xmlsuite.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);37 xmlsuite.setThreadCount(256);38 xmlsuite.setVerbose(2);39 xmlsuite.setParallel(XmlSuite.ParallelMode.CLASSES);40 XmlTest xmltest = createXmlTest(xmlsuite, "2232_test");41 XmlRun xmlrun = new XmlRun();42 xmlrun.onInclude("Group2");43 xmlrun.onExclude("Broken");44 XmlGroups xmlgroup = new XmlGroups();45 xmlgroup.setRun(xmlrun);46 xmltest.setGroups(xmlgroup);47 XmlPackage xmlpackage = new XmlPackage();48 xmlpackage.setName(getClass().getPackage().getName() + ".samples.*");49 xmltest.setPackages(Collections.singletonList(xmlpackage));50 return xmlsuite;51 }52 @Test(invocationCount = 10, description = "GITHUB-2232")53 //Ensuring that the bug doesn't surface even when tests are executed via the command line mode54 public void commandlineTest() throws IOException, InterruptedException {55 Path suitefile = Files.write(Files.createTempFile("testng", ".xml"),...
Source:DynamicTestNG.java
...8import org.testng.ITestNGListener;9import org.testng.TestNG;10import org.testng.xml.XmlGroups;11import org.testng.xml.XmlPackage;12import org.testng.xml.XmlRun;13import org.testng.xml.XmlSuite;14import org.testng.xml.XmlTest;15import com.nag.nagp.testListeners.TestListener;16public class DynamicTestNG {17 public static void main(String[] args) {18 String csvFile = "Clients.csv";19 BufferedReader br = null;20 String line = "";21 String cvsSplitBy = ",";22 try {23 br = new BufferedReader(new FileReader(csvFile));24 while ((line = br.readLine()) != null) {25 if(((br.readLine().split(cvsSplitBy))[2]).equals("email@gmail.com")){26 String[] data = line.split(cvsSplitBy);27 System.out.println("First Name: "+data[0]+" Last Name: "+data[1]+" Activity Level: "+data[7]);28 }29 }30 } catch (FileNotFoundException e) {31 e.printStackTrace();32 } catch (IOException e) {33 e.printStackTrace();34 } finally {35 if (br != null) {36 try {37 br.close();38 } catch (IOException e) {39 e.printStackTrace();40 }41 }42 }43 }44 public static void main1(String args[]) {45 46 //Create an instance on TestNG 47 TestNG tng = new TestNG();48 49 //Create an instance of XML Suite and assign a name for it.50 XmlSuite suite = new XmlSuite();51 suite.setName("RegressionSuite");52 //mySuite.setParallel("Tests"); 53 //mySuite.setThreadCount(10);54 55 //Create an instance of XmlTest and assign a name for it. 56 XmlTest test = new XmlTest(suite);57 test.setName("Test");58 //test.setPreserveOrder("true");59 60 // Create an instance of XmlGroups that will hold the Run Instance61 XmlGroups grp=new XmlGroups();62 63 //Create an instance of XmlRun to include group name in XML file 64 XmlRun xmlRun = constructIncludes("Suite;Group1".split(";"));65 grp.setRun(xmlRun);66 67 68 test.setGroups(grp);69 70 List<XmlPackage> packages = new ArrayList<XmlPackage>();71 packages.add(new XmlPackage("com.nag.nagp.testcases.*"));72 packages.add(new XmlPackage("com.nag.nagp.testcasebase.*"));73 test.setXmlPackages(packages);74 75 TestListener tla = new TestListener();76 tng.addListener((ITestNGListener) tla);77 78 List<XmlSuite> suites = new ArrayList<XmlSuite>();79 suites.add(suite);80 tng.setXmlSuites(suites);81 System.out.println (suite.toXml ());82 tng.run();83 84 85 86 }87 88 private static XmlRun constructIncludes (String... methodNames) {89 XmlRun xmlRun = new XmlRun();90 for (String eachMethod : methodNames) {91 xmlRun.onInclude(eachMethod.trim());92 }93 return xmlRun;94 }95}...
Source:RayAlterSuiteListener.java
...3import io.ray.runtime.config.RunMode;4import java.util.List;5import org.testng.IAlterSuiteListener;6import org.testng.xml.XmlGroups;7import org.testng.xml.XmlRun;8import org.testng.xml.XmlSuite;9public class RayAlterSuiteListener implements IAlterSuiteListener {10 @Override11 public void alter(List<XmlSuite> suites) {12 XmlSuite suite = suites.get(0);13 String excludedGroup =14 RayConfig.create().runMode == RunMode.SINGLE_PROCESS ? "cluster" : "singleProcess";15 XmlGroups groups = new XmlGroups();16 XmlRun run = new XmlRun();17 run.onExclude(excludedGroup);18 groups.setRun(run);19 suite.setGroups(groups);20 }21}...
XmlRun
Using AI Code Generation
1XmlRun xmlRun = new XmlRun();2xmlRun.setSuiteFileName("testng.xml");3xmlRun.run();4XmlSuite xmlSuite = new XmlSuite();5xmlSuite.setFileName("testng.xml");6xmlSuite.run();
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!