How to use onFinish method of org.testng.reporters.ExitCodeListener class

Best Testng code snippet using org.testng.reporters.ExitCodeListener.onFinish

Source:TestNG.java Github

copy

Full Screen

...913 for (IExecutionListener l : m_configuration.getExecutionListeners()) {914 if (start) {915 l.onExecutionStart();916 } else {917 l.onExecutionFinish();918 }919 }920 }921 /**922 * @deprecated Use addListener(ITestNGListener) instead923 */924 // TODO remove later925 @Deprecated926 public void addAlterSuiteListener(IAlterSuiteListener l) {927 addListener((ITestNGListener) l);928 }929 /**930 * @deprecated Use addListener(ITestNGListener) instead931 */932 // TODO remove later933 @Deprecated934 public void addExecutionListener(IExecutionListener l) {935 addListener((ITestNGListener) l);936 }937 private static void usage() {938 if (m_jCommander == null) {939 m_jCommander = new JCommander(new CommandLineArgs());940 }941 m_jCommander.usage();942 }943 private void generateReports(List<ISuite> suiteRunners) {944 for (IReporter reporter : m_reporters.values()) {945 try {946 long start = System.currentTimeMillis();947 reporter.generateReport(m_suites, suiteRunners, m_outputDir);948 Utils.log("TestNG", 2, "Time taken by " + reporter + ": "949 + (System.currentTimeMillis() - start) + " ms");950 }951 catch(Exception ex) {952 System.err.println("[TestNG] Reporter " + reporter + " failed");953 ex.printStackTrace(System.err);954 }955 }956 }957 /**958 * This needs to be public for maven2, for now..At least959 * until an alternative mechanism is found.960 */961 public List<ISuite> runSuitesLocally() {962 if (m_suites.isEmpty()) {963 error("No test suite found. Nothing to run");964 usage();965 return Collections.emptyList();966 }967 SuiteRunnerMap suiteRunnerMap = new SuiteRunnerMap();968 if (m_suites.get(0).getVerbose() >= 2) {969 Version.displayBanner();970 }971 // First initialize the suite runners to ensure there are no configuration issues.972 // Create a map with XmlSuite as key and corresponding SuiteRunner as value973 for (XmlSuite xmlSuite : m_suites) {974 createSuiteRunners(suiteRunnerMap, xmlSuite);975 }976 //977 // Run suites978 //979 if (m_suiteThreadPoolSize == 1 && !m_randomizeSuites) {980 // Single threaded and not randomized: run the suites in order981 for (XmlSuite xmlSuite : m_suites) {982 runSuitesSequentially(xmlSuite, suiteRunnerMap, getVerbose(xmlSuite),983 getDefaultSuiteName());984 }985 //986 // Generate the suites report987 //988 return Lists.newArrayList(suiteRunnerMap.values());989 }990 // Multithreaded: generate a dynamic graph that stores the suite hierarchy. This is then991 // used to run related suites in specific order. Parent suites are run only992 // once all the child suites have completed execution993 DynamicGraph<ISuite> suiteGraph = new DynamicGraph<>();994 for (XmlSuite xmlSuite : m_suites) {995 populateSuiteGraph(suiteGraph, suiteRunnerMap, xmlSuite);996 }997 IThreadWorkerFactory<ISuite> factory = new SuiteWorkerFactory(suiteRunnerMap,998 0 /* verbose hasn't been set yet */, getDefaultSuiteName());999 GraphThreadPoolExecutor<ISuite> pooledExecutor =1000 new GraphThreadPoolExecutor<>("suites", suiteGraph, factory, m_suiteThreadPoolSize,1001 m_suiteThreadPoolSize, Integer.MAX_VALUE, TimeUnit.MILLISECONDS,1002 new LinkedBlockingQueue<Runnable>());1003 Utils.log("TestNG", 2, "Starting executor for all suites");1004 // Run all suites in parallel1005 pooledExecutor.run();1006 try {1007 pooledExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);1008 pooledExecutor.shutdownNow();1009 } catch (InterruptedException handled) {1010 Thread.currentThread().interrupt();1011 error("Error waiting for concurrent executors to finish " + handled.getMessage());1012 }1013 //1014 // Generate the suites report1015 //1016 return Lists.newArrayList(suiteRunnerMap.values());1017 }1018 private static void error(String s) {1019 LOGGER.error(s);1020 }1021 /**1022 * @return the verbose level, checking in order: the verbose level on1023 * the suite, the verbose level on the TestNG object, or 1.1024 */1025 private int getVerbose(XmlSuite xmlSuite) {1026 int result = xmlSuite.getVerbose() != null ? xmlSuite.getVerbose()1027 : (m_verbose != null ? m_verbose : DEFAULT_VERBOSE);1028 return result;1029 }1030 /**1031 * Recursively runs suites. Runs the children suites before running the parent1032 * suite. This is done so that the results for parent suite can reflect the1033 * combined results of the children suites.1034 *1035 * @param xmlSuite XML Suite to be executed1036 * @param suiteRunnerMap Maps {@code XmlSuite}s to respective {@code ISuite}1037 * @param verbose verbose level1038 * @param defaultSuiteName default suite name1039 */1040 private void runSuitesSequentially(XmlSuite xmlSuite,1041 SuiteRunnerMap suiteRunnerMap, int verbose, String defaultSuiteName) {1042 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1043 runSuitesSequentially(childSuite, suiteRunnerMap, verbose, defaultSuiteName);1044 }1045 SuiteRunnerWorker srw = new SuiteRunnerWorker(suiteRunnerMap.get(xmlSuite), suiteRunnerMap,1046 verbose, defaultSuiteName);1047 srw.run();1048 }1049 /**1050 * Populates the dynamic graph with the reverse hierarchy of suites. Edges are1051 * added pointing from child suite runners to parent suite runners, hence making1052 * parent suite runners dependent on all the child suite runners1053 *1054 * @param suiteGraph dynamic graph representing the reverse hierarchy of SuiteRunners1055 * @param suiteRunnerMap Map with XMLSuite as key and its respective SuiteRunner as value1056 * @param xmlSuite XML Suite1057 */1058 private void populateSuiteGraph(DynamicGraph<ISuite> suiteGraph /* OUT */,1059 SuiteRunnerMap suiteRunnerMap, XmlSuite xmlSuite) {1060 ISuite parentSuiteRunner = suiteRunnerMap.get(xmlSuite);1061 if (xmlSuite.getChildSuites().isEmpty()) {1062 suiteGraph.addNode(parentSuiteRunner);1063 }1064 else {1065 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1066 suiteGraph.addEdge(0, parentSuiteRunner, suiteRunnerMap.get(childSuite));1067 populateSuiteGraph(suiteGraph, suiteRunnerMap, childSuite);1068 }1069 }1070 }1071 /**1072 * Creates the {@code SuiteRunner}s and populates the suite runner map with1073 * this information1074 * @param suiteRunnerMap Map with XMLSuite as key and it's respective1075 * SuiteRunner as value. This is updated as part of this method call1076 * @param xmlSuite Xml Suite (and its children) for which {@code SuiteRunner}s are created1077 */1078 private void createSuiteRunners(SuiteRunnerMap suiteRunnerMap /* OUT */, XmlSuite xmlSuite) {1079 if (null != m_isJUnit && ! m_isJUnit.equals(XmlSuite.DEFAULT_JUNIT)) {1080 xmlSuite.setJUnit(m_isJUnit);1081 }1082 // If the skip flag was invoked on the command line, it1083 // takes precedence1084 if (null != m_skipFailedInvocationCounts) {1085 xmlSuite.setSkipFailedInvocationCounts(m_skipFailedInvocationCounts);1086 }1087 // Override the XmlSuite verbose value with the one from TestNG1088 if (m_verbose != null) {1089 xmlSuite.setVerbose(m_verbose);1090 }1091 if (null != m_configFailurePolicy) {1092 xmlSuite.setConfigFailurePolicy(m_configFailurePolicy);1093 }1094 for (XmlTest t : xmlSuite.getTests()) {1095 for (Map.Entry<String, Integer> ms : m_methodDescriptors.entrySet()) {1096 XmlMethodSelector xms = new XmlMethodSelector();1097 xms.setName(ms.getKey());1098 xms.setPriority(ms.getValue());1099 t.getMethodSelectors().add(xms);1100 }1101 for (XmlMethodSelector selector : m_selectors) {1102 t.getMethodSelectors().add(selector);1103 }1104 }1105 suiteRunnerMap.put(xmlSuite, createSuiteRunner(xmlSuite));1106 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1107 createSuiteRunners(suiteRunnerMap, childSuite);1108 }1109 }1110 /**1111 * Creates a suite runner and configures its initial state1112 * @param xmlSuite1113 * @return returns the newly created suite runner1114 */1115 private SuiteRunner createSuiteRunner(XmlSuite xmlSuite) {1116 SuiteRunner result = new SuiteRunner(getConfiguration(), xmlSuite,1117 m_outputDir,1118 m_testRunnerFactory,1119 m_useDefaultListeners,1120 m_methodInterceptors,1121 m_invokedMethodListeners.values(),1122 m_testListeners.values(),1123 m_classListeners.values(),1124 m_dataProviderListeners,1125 Systematiser.getComparator());1126 for (ISuiteListener isl : m_suiteListeners.values()) {1127 result.addListener(isl);1128 }1129 for (IReporter r : result.getReporters()) {1130 maybeAddListener(m_reporters, r.getClass(), r, true);1131 }1132 for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {1133 result.addConfigurationListener(cl);1134 }1135 return result;1136 }1137 protected IConfiguration getConfiguration() {1138 return m_configuration;1139 }1140 /**1141 * The TestNG entry point for command line execution.1142 *1143 * @param argv the TestNG command line parameters.1144 * @throws FileNotFoundException1145 */1146 public static void main(String[] argv) {1147 TestNG testng = privateMain(argv, null);1148 System.exit(testng.getStatus());1149 }1150 /**1151 * <B>Note</B>: this method is not part of the public API and is meant for internal usage only.1152 */1153 public static TestNG privateMain(String[] argv, ITestListener listener) {1154 TestNG result = new TestNG();1155 if (null != listener) {1156 result.addListener((Object)listener);1157 }1158 //1159 // Parse the arguments1160 //1161 try {1162 CommandLineArgs cla = new CommandLineArgs();1163 m_jCommander = new JCommander(cla, argv);1164 validateCommandLineParameters(cla);1165 result.configure(cla);1166 }1167 catch(ParameterException ex) {1168 exitWithError(ex.getMessage());1169 }1170 //1171 // Run1172 //1173 try {1174 result.run();1175 }1176 catch(TestNGException ex) {1177 if (TestRunner.getVerbose() > 1) {1178 ex.printStackTrace(System.out);1179 }1180 else {1181 error(ex.getMessage());1182 }1183 result.exitCode = ExitCode.newExitCodeRepresentingFailure();1184 }1185 return result;1186 }1187 /**1188 * Configure the TestNG instance based on the command line parameters.1189 */1190 protected void configure(CommandLineArgs cla) {1191 if (cla.verbose != null) {1192 setVerbose(cla.verbose);1193 }1194 setOutputDirectory(cla.outputDirectory);1195 String testClasses = cla.testClass;1196 if (null != testClasses) {1197 String[] strClasses = testClasses.split(",");1198 List<Class> classes = Lists.newArrayList();1199 for (String c : strClasses) {1200 classes.add(ClassHelper.fileToClass(c));1201 }1202 setTestClasses(classes.toArray(new Class[classes.size()]));1203 }1204 setOutputDirectory(cla.outputDirectory);1205 if (cla.testNames != null) {1206 setTestNames(Arrays.asList(cla.testNames.split(",")));1207 }1208// List<String> testNgXml = (List<String>) cmdLineArgs.get(CommandLineArgs.SUITE_DEF);1209// if (null != testNgXml) {1210// setTestSuites(testNgXml);1211// }1212 // Note: can't use a Boolean field here because we are allowing a boolean1213 // parameter with an arity of 1 ("-usedefaultlisteners false")1214 if (cla.useDefaultListeners != null) {1215 setUseDefaultListeners("true".equalsIgnoreCase(cla.useDefaultListeners));1216 }1217 setGroups(cla.groups);1218 setExcludedGroups(cla.excludedGroups);1219 setTestJar(cla.testJar);1220 setXmlPathInJar(cla.xmlPathInJar);1221 setJUnit(cla.junit);1222 setMixed(cla.mixed);1223 setSkipFailedInvocationCounts(cla.skipFailedInvocationCounts);1224 if (cla.parallelMode != null) {1225 setParallel(cla.parallelMode);1226 }1227 if (cla.configFailurePolicy != null) {1228 setConfigFailurePolicy(cla.configFailurePolicy);1229 }1230 if (cla.threadCount != null) {1231 setThreadCount(cla.threadCount);1232 }1233 if (cla.dataProviderThreadCount != null) {1234 setDataProviderThreadCount(cla.dataProviderThreadCount);1235 }1236 if (cla.suiteName != null) {1237 setDefaultSuiteName(cla.suiteName);1238 }1239 if (cla.testName != null) {1240 setDefaultTestName(cla.testName);1241 }1242 if (cla.listener != null) {1243 String sep = ";";1244 if (cla.listener.contains(",")) {1245 sep = ",";1246 }1247 String[] strs = Utils.split(cla.listener, sep);1248 List<Class<? extends ITestNGListener>> classes = Lists.newArrayList();1249 for (String cls : strs) {1250 Class<?> clazz = ClassHelper.fileToClass(cls);1251 if (ITestNGListener.class.isAssignableFrom(clazz)) {1252 classes.add((Class<? extends ITestNGListener>) clazz);1253 }1254 }1255 setListenerClasses(classes);1256 }1257 if (null != cla.methodSelectors) {1258 String[] strs = Utils.split(cla.methodSelectors, ",");1259 for (String cls : strs) {1260 String[] sel = Utils.split(cls, ":");1261 try {1262 if (sel.length == 2) {1263 addMethodSelector(sel[0], Integer.parseInt(sel[1]));1264 } else {1265 error("Method selector value was not in the format org.example.Selector:4");1266 }1267 }1268 catch (NumberFormatException nfe) {1269 error("Method selector value was not in the format org.example.Selector:4");1270 }1271 }1272 }1273 if (cla.objectFactory != null) {1274 setObjectFactory(ClassHelper.fileToClass(cla.objectFactory));1275 }1276 if (cla.testRunnerFactory != null) {1277 setTestRunnerFactoryClass(1278 ClassHelper.fileToClass(cla.testRunnerFactory));1279 }1280 if (cla.reporter != null) {1281 ReporterConfig reporterConfig = ReporterConfig.deserialize(cla.reporter);1282 addReporter(reporterConfig);1283 }1284 if (cla.commandLineMethods.size() > 0) {1285 m_commandLineMethods = cla.commandLineMethods;1286 }1287 if (cla.suiteFiles != null) {1288 setTestSuites(cla.suiteFiles);1289 }1290 setSuiteThreadPoolSize(cla.suiteThreadPoolSize);1291 setRandomizeSuites(cla.randomizeSuites);1292 }1293 public void setSuiteThreadPoolSize(Integer suiteThreadPoolSize) {1294 m_suiteThreadPoolSize = suiteThreadPoolSize;1295 }1296 public Integer getSuiteThreadPoolSize() {1297 return m_suiteThreadPoolSize;1298 }1299 public void setRandomizeSuites(boolean randomizeSuites) {1300 m_randomizeSuites = randomizeSuites;1301 }1302 /**1303 * This method is invoked by Maven's Surefire, only remove it once1304 * Surefire has been modified to no longer call it.1305 */1306 public void setSourcePath(String path) {1307 // nop1308 }1309 /**1310 * This method is invoked by Maven's Surefire to configure the runner,1311 * do not remove unless you know for sure that Surefire has been updated1312 * to use the new configure(CommandLineArgs) method.1313 *1314 * @deprecated use new configure(CommandLineArgs) method1315 */1316 @SuppressWarnings({"unchecked"})1317 @Deprecated1318 public void configure(Map cmdLineArgs) {1319 CommandLineArgs result = new CommandLineArgs();1320 Integer verbose = (Integer) cmdLineArgs.get(CommandLineArgs.LOG);1321 if (null != verbose) {1322 result.verbose = verbose;1323 }1324 result.outputDirectory = (String) cmdLineArgs.get(CommandLineArgs.OUTPUT_DIRECTORY);1325 String testClasses = (String) cmdLineArgs.get(CommandLineArgs.TEST_CLASS);1326 if (null != testClasses) {1327 result.testClass = testClasses;1328 }1329 String testNames = (String) cmdLineArgs.get(CommandLineArgs.TEST_NAMES);1330 if (testNames != null) {1331 result.testNames = testNames;1332 }1333 String useDefaultListeners = (String) cmdLineArgs.get(CommandLineArgs.USE_DEFAULT_LISTENERS);1334 if (null != useDefaultListeners) {1335 result.useDefaultListeners = useDefaultListeners;1336 }1337 result.groups = (String) cmdLineArgs.get(CommandLineArgs.GROUPS);1338 result.excludedGroups = (String) cmdLineArgs.get(CommandLineArgs.EXCLUDED_GROUPS);1339 result.testJar = (String) cmdLineArgs.get(CommandLineArgs.TEST_JAR);1340 result.xmlPathInJar = (String) cmdLineArgs.get(CommandLineArgs.XML_PATH_IN_JAR);1341 result.junit = (Boolean) cmdLineArgs.get(CommandLineArgs.JUNIT);1342 result.mixed = (Boolean) cmdLineArgs.get(CommandLineArgs.MIXED);1343 result.skipFailedInvocationCounts = (Boolean) cmdLineArgs.get(1344 CommandLineArgs.SKIP_FAILED_INVOCATION_COUNTS);1345 String parallelMode = (String) cmdLineArgs.get(CommandLineArgs.PARALLEL);1346 if (parallelMode != null) {1347 result.parallelMode = XmlSuite.ParallelMode.getValidParallel(parallelMode);1348 }1349 String threadCount = (String) cmdLineArgs.get(CommandLineArgs.THREAD_COUNT);1350 if (threadCount != null) {1351 result.threadCount = Integer.parseInt(threadCount);1352 }1353 // Not supported by Surefire yet1354 Integer dptc = (Integer) cmdLineArgs.get(CommandLineArgs.DATA_PROVIDER_THREAD_COUNT);1355 if (dptc != null) {1356 result.dataProviderThreadCount = dptc;1357 }1358 String defaultSuiteName = (String) cmdLineArgs.get(CommandLineArgs.SUITE_NAME);1359 if (defaultSuiteName != null) {1360 result.suiteName = defaultSuiteName;1361 }1362 String defaultTestName = (String) cmdLineArgs.get(CommandLineArgs.TEST_NAME);1363 if (defaultTestName != null) {1364 result.testName = defaultTestName;1365 }1366 Object listeners = cmdLineArgs.get(CommandLineArgs.LISTENER);1367 if (listeners instanceof List) {1368 result.listener = Utils.join((List<?>) listeners, ",");1369 } else {1370 result.listener = (String) listeners;1371 }1372 String ms = (String) cmdLineArgs.get(CommandLineArgs.METHOD_SELECTORS);1373 if (null != ms) {1374 result.methodSelectors = ms;1375 }1376 String objectFactory = (String) cmdLineArgs.get(CommandLineArgs.OBJECT_FACTORY);1377 if(null != objectFactory) {1378 result.objectFactory = objectFactory;1379 }1380 String runnerFactory = (String) cmdLineArgs.get(CommandLineArgs.TEST_RUNNER_FACTORY);1381 if (null != runnerFactory) {1382 result.testRunnerFactory = runnerFactory;1383 }1384 String reporterConfigs = (String) cmdLineArgs.get(CommandLineArgs.REPORTER);1385 if (reporterConfigs != null) {1386 result.reporter = reporterConfigs;1387 }1388 String failurePolicy = (String)cmdLineArgs.get(CommandLineArgs.CONFIG_FAILURE_POLICY);1389 if (failurePolicy != null) {1390 result.configFailurePolicy = failurePolicy;1391 }1392 Object suiteThreadPoolSize = cmdLineArgs.get(CommandLineArgs.SUITE_THREAD_POOL_SIZE);1393 if (null != suiteThreadPoolSize) {1394 if (suiteThreadPoolSize instanceof String){1395 result.suiteThreadPoolSize=Integer.parseInt((String) suiteThreadPoolSize);1396 }1397 if (suiteThreadPoolSize instanceof Integer){1398 result.suiteThreadPoolSize=(Integer) suiteThreadPoolSize;1399 }1400 }1401 configure(result);1402 }1403 /**1404 * Only run the specified tests from the suite.1405 */1406 public void setTestNames(List<String> testNames) {1407 m_testNames = testNames;1408 }1409 public void setSkipFailedInvocationCounts(Boolean skip) {1410 m_skipFailedInvocationCounts = skip;1411 }1412 private void addReporter(ReporterConfig reporterConfig) {1413 IReporter instance = reporterConfig.newReporterInstance();1414 if (instance != null) {1415 addListener(instance);1416 } else {1417 LOGGER.warn("Could not find reporter class : " + reporterConfig.getClassName());1418 }1419 }1420 /**1421 * Specify if this run should be made in JUnit mode1422 *1423 * @param isJUnit1424 */1425 public void setJUnit(Boolean isJUnit) {1426 m_isJUnit = isJUnit;1427 }1428 /**1429 * Specify if this run should be made in mixed mode1430 */1431 public void setMixed(Boolean isMixed) {1432 if(isMixed==null){1433 return;1434 }1435 m_isMixed = isMixed;1436 }1437 /**1438 * @deprecated The TestNG version is now established at load time. This1439 * method is not required anymore and is now a no-op.1440 */1441 @Deprecated1442 public static void setTestNGVersion() {1443 LOGGER.info("setTestNGVersion has been deprecated.");1444 }1445 /**1446 * Returns true if this is the JDK 1.4 JAR version of TestNG, false otherwise.1447 *1448 * @return true if this is the JDK 1.4 JAR version of TestNG, false otherwise.1449 */1450 @Deprecated1451 public static boolean isJdk14() {1452 return false;1453 }1454 /**1455 * Double check that the command line parameters are valid.1456 */1457 protected static void validateCommandLineParameters(CommandLineArgs args) {1458 String testClasses = args.testClass;1459 List<String> testNgXml = args.suiteFiles;1460 String testJar = args.testJar;1461 List<String> methods = args.commandLineMethods;1462 if (testClasses == null && testJar == null1463 && (testNgXml == null || testNgXml.isEmpty())1464 && (methods == null || methods.isEmpty())) {1465 throw new ParameterException("You need to specify at least one testng.xml, one class"1466 + " or one method");1467 }1468 String groups = args.groups;1469 String excludedGroups = args.excludedGroups;1470 if (testJar == null &&1471 (null != groups || null != excludedGroups) && testClasses == null1472 && (testNgXml == null || testNgXml.isEmpty())) {1473 throw new ParameterException("Groups option should be used with testclass option");1474 }1475 Boolean junit = args.junit;1476 Boolean mixed = args.mixed;1477 if (junit && mixed) {1478 throw new ParameterException(CommandLineArgs.MIXED + " can't be combined with "1479 + CommandLineArgs.JUNIT);1480 }1481 }1482 /**1483 * @return true if at least one test failed.1484 */1485 public boolean hasFailure() {1486 return this.exitCode.hasFailure();1487 }1488 /**1489 * @return true if at least one test failed within success percentage.1490 */1491 public boolean hasFailureWithinSuccessPercentage() {1492 return this.exitCode.hasFailureWithinSuccessPercentage();1493 }1494 /**1495 * @return true if at least one test was skipped.1496 */1497 public boolean hasSkip() {1498 return this.exitCode.hasSkip();1499 }1500 static void exitWithError(String msg) {1501 System.err.println(msg);1502 usage();1503 System.exit(1);1504 }1505 public String getOutputDirectory() {1506 return m_outputDir;1507 }1508 public IAnnotationTransformer getAnnotationTransformer() {1509 return m_annotationTransformer;1510 }1511 /**1512 * @deprecated Use addListener(ITestNGListener) instead1513 */1514 // TODO make private1515 @Deprecated1516 public void setAnnotationTransformer(IAnnotationTransformer t) {1517 // compare by reference!1518 if (m_annotationTransformer != m_defaultAnnoProcessor && m_annotationTransformer != t) {1519 LOGGER.warn("AnnotationTransformer already set");1520 }1521 m_annotationTransformer = t;1522 }1523 /**1524 * @return the defaultSuiteName1525 */1526 public String getDefaultSuiteName() {1527 return m_defaultSuiteName;1528 }1529 /**1530 * @param defaultSuiteName the defaultSuiteName to set1531 */1532 public void setDefaultSuiteName(String defaultSuiteName) {1533 m_defaultSuiteName = defaultSuiteName;1534 }1535 /**1536 * @return the defaultTestName1537 */1538 public String getDefaultTestName() {1539 return m_defaultTestName;1540 }1541 /**1542 * @param defaultTestName the defaultTestName to set1543 */1544 public void setDefaultTestName(String defaultTestName) {1545 m_defaultTestName = defaultTestName;1546 }1547 /**1548 * Sets the policy for whether or not to ever invoke a configuration method again after1549 * it has failed once. Possible values are defined in {@link XmlSuite}. The default1550 * value is {@link org.testng.xml.XmlSuite.FailurePolicy#SKIP}1551 * @param failurePolicy the configuration failure policy1552 */1553 public void setConfigFailurePolicy(XmlSuite.FailurePolicy failurePolicy) {1554 m_configFailurePolicy = failurePolicy;1555 }1556 /**1557 * @deprecated Use {@link #setConfigFailurePolicy(org.testng.xml.XmlSuite.FailurePolicy)} instead1558 */1559 @Deprecated1560 public void setConfigFailurePolicy(String failurePolicy) {1561 setConfigFailurePolicy(XmlSuite.FailurePolicy.getValidPolicy(failurePolicy));1562 }1563 /**1564 * Returns the configuration failure policy.1565 * @return config failure policy1566 */1567 public XmlSuite.FailurePolicy getConfigFailurePolicy() {1568 return m_configFailurePolicy;1569 }1570 // DEPRECATED: to be removed after a major version change1571 /**1572 * @deprecated since 5.11573 */1574 @Deprecated1575 public static TestNG getDefault() {1576 return m_instance;1577 }1578 @Deprecated1579 /**1580 * @deprecated - This class stands deprecated as of TestNG v6.131581 */1582 public static class ExitCodeListener implements IResultListener2 {1583 private TestNG m_mainRunner;1584 public ExitCodeListener() {1585 m_mainRunner = TestNG.m_instance;1586 }1587 public ExitCodeListener(TestNG runner) {1588 m_mainRunner = runner;1589 }1590 @Override1591 public void beforeConfiguration(ITestResult tr) {1592 }1593 @Override1594 public void onTestFailure(ITestResult result) {1595 setHasRunTests();1596 }1597 @Override1598 public void onTestSkipped(ITestResult result) {1599 setHasRunTests();1600 }1601 @Override1602 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {1603 setHasRunTests();1604 }1605 @Override1606 public void onTestSuccess(ITestResult result) {1607 setHasRunTests();1608 }1609 @Override1610 public void onStart(ITestContext context) {1611 setHasRunTests();1612 }1613 @Override1614 public void onFinish(ITestContext context) {1615 }1616 @Override1617 public void onTestStart(ITestResult result) {1618 setHasRunTests();1619 }1620 private void setHasRunTests() {1621 }1622 /**1623 * @see org.testng.IConfigurationListener#onConfigurationFailure(org.testng.ITestResult)1624 */1625 @Override1626 public void onConfigurationFailure(ITestResult itr) {1627 }1628 /**...

Full Screen

Full Screen

Source:ExitCodeListener.java Github

copy

Full Screen

...34 public void onStart(ITestContext context) {35 setHasRunTests();36 }37 @Override38 public void onFinish(ITestContext context) {}39 @Override40 public void onTestStart(ITestResult result) {41 setHasRunTests();42 }43 private void setHasRunTests() {}44 /** @see org.testng.IConfigurationListener#onConfigurationFailure(org.testng.ITestResult) */45 @Override46 public void onConfigurationFailure(ITestResult itr) {}47 /** @see org.testng.IConfigurationListener#onConfigurationSkip(org.testng.ITestResult) */48 @Override49 public void onConfigurationSkip(ITestResult itr) {}50 /** @see org.testng.IConfigurationListener#onConfigurationSuccess(org.testng.ITestResult) */51 @Override52 public void onConfigurationSuccess(ITestResult itr) {}...

Full Screen

Full Screen

Source:TestListener.java Github

copy

Full Screen

...11 public void onStart(ITestContext context) {12 System.out.println("Start test " + context.getName());13 }14 @Override15 public void onFinish(ITestContext context) {16 System.out.println("End test " + context.getName());17 }18 19 @Override20 public void onConfigurationFailure(ITestResult arg0) {21 System.out.println("Error");22 }23}

Full Screen

Full Screen

Source:BrowserPerClass.java Github

copy

Full Screen

...6 * Annotate your test class with <code>@Listeners({ BrowserPerClass.class})</code>7 */8public class BrowserPerClass extends ExitCodeListener {9 @Override10 public void onFinish(ITestContext context) {11 super.onFinish(context);12 closeWebDriver();13 }14}...

Full Screen

Full Screen

onFinish

Using AI Code Generation

copy

Full Screen

1public class TestListener extends ExitCodeListener {2 public void onFinish(ITestContext testContext) {3 super.onFinish(testContext);4 int exitCode = getExitCode();5 if (exitCode != 0) {6 System.out.println("Test failed with exit code: " + exitCode);7 }8 }9}

Full Screen

Full Screen

onFinish

Using AI Code Generation

copy

Full Screen

1 public void onFinish(ITestContext context)2 {3 super.onFinish(context);4 }5}6public class TestNGTest {7 public void test1() {8 System.out.println("test1");9 }10 public void test2() {11 System.out.println("test2");12 }13}14BUILD SUCCESSFUL (total time: 0 seconds)

Full Screen

Full Screen

onFinish

Using AI Code Generation

copy

Full Screen

1public class TestListener implements ITestListener {2 private static final Logger logger = Logger.getLogger(TestListener.class);3 private WebDriver driver;4 private String testName;5 private String testDesc;6 public void onTestStart(ITestResult iTestResult) {7 logger.info("Test case started: " + iTestResult.getName());8 }9 public void onTestSuccess(ITestResult iTestResult) {10 logger.info("Test case passed: " + iTestResult.getName());11 }12 public void onTestFailure(ITestResult iTestResult) {13 logger.info("Test case failed: " + iTestResult.getName());14 takeScreenShot();15 }16 public void onTestSkipped(ITestResult iTestResult) {17 logger.info("Test case skipped: " + iTestResult.getName());18 }19 public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {20 logger.info("Test case failed but within success percentage: " + iTestResult.getName());21 }22 public void onStart(ITestContext iTestContext) {23 logger.info("Test suite started: " + iTestContext.getName());24 }25 public void onFinish(ITestContext iTestContext) {26 logger.info("Test suite finished: " + iTestContext.getName());27 }28 private void takeScreenShot() {29 File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);30 try {31 FileUtils.copyFile(scrFile, new File("target/screenshots/" + testName + "_" + testDesc + ".png"));32 } catch (IOException e) {33 e.printStackTrace();34 }35 }36}

Full Screen

Full Screen

onFinish

Using AI Code Generation

copy

Full Screen

1package com.test;2import org.testng.Assert;3import org.testng.annotations.Test;4public class TestNG_ExitCodeListener {5public void test1(){6System.out.println("This is test 1");7Assert.assertTrue(true);8}9public void test2(){10System.out.println("This is test 2");11Assert.assertTrue(false);12}13public void test3(){14System.out.println("This is test 3");15Assert.assertTrue(true);16}17}

Full Screen

Full Screen

TestNG tutorial

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.

Chapters

  1. JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
  2. Installing TestNG in Eclipse: Start installing the TestNG Plugin and learn how to set up TestNG in Eclipse to begin constructing a framework for your test project.
  3. Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
  4. Automation using TestNG: Dive into how to install TestNG in this Selenium TestNG tutorial, the fundamentals of developing an automation script for Selenium automation testing.
  5. Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
  6. Creating TestNG XML File: Here is a step-by-step tutorial on creating a TestNG XML file to learn why and how it is created and discover how to run the TestNG XML file being executed in parallel.
  7. Automation with Selenium, Cucumber & TestNG: Explore for an in-depth tutorial on automation using Selenium, Cucumber, and TestNG, as TestNG offers simpler settings and more features.
  8. JUnit Selenium Tests using TestNG: Start running your regular and parallel tests by looking at how to run test cases in Selenium using JUnit and TestNG without having to rewrite the tests.
  9. Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
  10. Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
  11. Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
  12. DataProviders in TestNG: Deep dive into learning more about TestNG's DataProvider and how to effectively use it in our test scripts for Selenium test automation.
  13. Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
  14. TestNG Listeners in Selenium WebDriver: Understand the various TestNG listeners to utilize them effectively for your next plan when working with TestNG and Selenium automation.
  15. TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
  16. TestNG Reporter Log in Selenium: Find out how to use the TestNG Reporter Log and learn how to eliminate the need for external software with TestNG Reporter Class to boost productivity.
  17. TestNG Reports in Jenkins: Discover how to generate TestNG reports in Jenkins if you want to know how to create, install, and share TestNG reports in Jenkins.

Certification

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.

YouTube

Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful