Source:CheckSuiteNamesTest.java
2import org.testng.Assert;3import org.testng.TestListenerAdapter;4import org.testng.TestNG;5import org.testng.annotations.Test;6import org.testng.xml.Parser;7import org.testng.xml.XmlClass;8import org.testng.xml.XmlSuite;9import org.testng.xml.XmlTest;10import org.xml.sax.SAXException;11import test.SimpleBaseTest;12import java.io.IOException;13import java.util.Arrays;14import javax.xml.parsers.ParserConfigurationException;15public class CheckSuiteNamesTest extends SimpleBaseTest {16 /**17 * Child suites have different names18 */19 @Test20 public void checkChildSuites() {21 TestListenerAdapter tla = new TestListenerAdapter();22 TestNG tng = create();23 String testngXmlPath = getPathToResource("sanitycheck/test-s-b.xml");24 tng.setTestSuites(Arrays.asList(testngXmlPath));25 tng.addListener(tla);26 tng.run();27 Assert.assertEquals(tla.getPassedTests().size(), 4);28 }29 /**30 * Child suites have same names31 */32 @Test33 public void checkChildSuitesFails() {34 TestListenerAdapter tla = new TestListenerAdapter();35 TestNG tng = create();36 String testngXmlPath = getPathToResource("sanitycheck/test-s-a.xml");37 tng.setTestSuites(Arrays.asList(testngXmlPath));38 tng.addListener(tla);39 tng.run();40 Assert.assertEquals(tla.getTestContexts().get(0).getSuite().getName(), "SanityCheck suites");41 Assert.assertEquals(tla.getTestContexts().get(1).getSuite().getName(), "SanityCheck suites");42 Assert.assertEquals(tla.getTestContexts().get(2).getSuite().getName(), "SanityCheck suites (0)");43 Assert.assertEquals(tla.getTestContexts().get(3).getSuite().getName(), "SanityCheck suites (0)");44 }45 /**46 * Checks that suites created programmatically also works as expected47 */48 @Test49 public void checkProgrammaticSuitesFails() {50 XmlSuite xmlSuite1 = new XmlSuite();51 xmlSuite1.setName("SanityCheckSuite");52 {53 XmlTest result = new XmlTest(xmlSuite1);54 result.getXmlClasses().add(new XmlClass(SampleTest1.class.getCanonicalName()));55 }56 XmlSuite xmlSuite2 = new XmlSuite();57 xmlSuite2.setName("SanityCheckSuite");58 {59 XmlTest result = new XmlTest(xmlSuite2);60 result.getXmlClasses().add(new XmlClass(SampleTest2.class.getCanonicalName()));61 }62 TestNG tng = create();63 tng.setXmlSuites(Arrays.asList(xmlSuite1, xmlSuite2));64 tng.run();65 Assert.assertEquals(xmlSuite1.getName(), "SanityCheckSuite");66 Assert.assertEquals(xmlSuite2.getName(), "SanityCheckSuite (0)");67 }68 69 @Test70 public void checkXmlSuiteAddition() throws ParserConfigurationException, SAXException, IOException {71 TestNG tng = create();72 String testngXmlPath = getPathToResource("sanitycheck/test-s-b.xml");73 Parser parser = new Parser(testngXmlPath); 74 tng.setXmlSuites(parser.parseToList());75 tng.initializeSuitesAndJarFile(); 76 }77}...
Source:TimeOutFromXmlTest.java
1package test.timeout;2import org.testng.annotations.Test;3import org.testng.xml.SuiteXmlParser;4import org.testng.xml.XmlClass;5import org.testng.xml.XmlInclude;6import org.testng.xml.XmlSuite;7import org.testng.xml.XmlTest;8import test.BaseTest;9import java.io.FileInputStream;10import java.io.IOException;11import java.util.ArrayList;12import java.util.Collections;13import java.util.List;14public class TimeOutFromXmlTest extends BaseTest {15 private void timeOutTest(boolean onSuite) {16 addClass(TestTimeOutSampleTest.class);17 if (onSuite) {18 setSuiteTimeOut(1_000);19 } else {20 setTestTimeOut(1_000);21 }22 run();23 verifyPassedTests();24 verifyFailedTests("timeoutTest");25 }26 @Test27 public void timeOutOnSuiteTag() {28 timeOutTest(true /* on suite */);29 }30 @Test31 public void timeOutOnTestTag() {32 timeOutTest(false /* on test */);33 }34 @Test35 public void noTimeOut() {36 addClass(TestTimeOutSampleTest.class);37 run();38 verifyPassedTests("timeoutTest");39 verifyFailedTests();40 }41 @Test42 public void twoDifferentTests() {43 XmlSuite result = new XmlSuite();44 result.setName("Suite");45 createXmlTest(result, "WithoutTimeOut");46 createXmlTest(result, "WithTimeOut").setTimeOut(1_000);47 setSuite(result);48 run();49 verifyPassedTests("timeoutTest");50 verifyFailedTests("timeoutTest");51 }52 private XmlTest createXmlTest(XmlSuite suite, String name) {53 XmlTest result = new XmlTest(suite);54 result.setName(name);55 List<XmlClass> classes = new ArrayList<>();56 XmlClass cls = new XmlClass(TestTimeOutSampleTest.class);57 cls.setIncludedMethods(58 Collections.singletonList(new XmlInclude("timeoutTest")));59 classes.add(cls);60 result.setXmlClasses(classes);61 return result;62 }63 @Test64 public void timeOutInParallelTestsFromXml() throws IOException {65 String file = "src/test/java/test/timeout/issue575.xml";66 try (FileInputStream stream = new FileInputStream(file)) {67 SuiteXmlParser suiteParser = new SuiteXmlParser();68 XmlSuite suite = suiteParser.parse(file, stream, true);69 setSuite(suite);70 run();71 verifyPassedTests("timeoutShouldPass");72 verifyFailedTests("timeoutShouldFailByException", "timeoutShouldFailByTimeOut");73 }74 }75}...
Source:FailedReporterTest.java
1package test.reports;2import javax.xml.parsers.ParserConfigurationException;3import org.testng.Assert;4import org.testng.TestListenerAdapter;5import org.testng.TestNG;6import org.testng.annotations.Test;7import org.testng.collections.Lists;8import org.testng.reporters.FailedReporter;9import org.testng.xml.Parser;10import org.testng.xml.XmlClass;11import org.testng.xml.XmlSuite;12import org.testng.xml.XmlTest;13import org.xml.sax.SAXException;14import test.SimpleBaseTest;15import java.io.ByteArrayInputStream;16import java.io.File;17import java.io.IOException;18import java.util.Collection;19public class FailedReporterTest extends SimpleBaseTest {20 private static final String XML = "<suite name=\"Suite\">\n"21 + "<parameter name=\"n\" value=\"42\"/>\n"22 + "<test name=\"Test\">\n"23 + "<classes>\n"24 + "<parameter name=\"o\" value=\"43\"/>\n"25 + "<class name=\"test.reports.FailedSampleTest\">\n"26 + "<parameter name=\"p\" value=\"44\"/>\n"27 + "</class>"28 + "</classes>"29 + "</test></suite>";30 @Test31 public void failedFile() throws ParserConfigurationException, SAXException, IOException {32 TestNG tng = new TestNG();33 tng.setVerbose(0);34 Collection<XmlSuite> suites = 35 new Parser(new ByteArrayInputStream(XML.getBytes())).parse();36 tng.setXmlSuites(Lists.newArrayList(suites));37 TestListenerAdapter tla = new TestListenerAdapter();38 File f = new File("/tmp");39 tng.setOutputDirectory(f.getAbsolutePath());40 tng.addListener(tla);41 tng.run();42 Collection<XmlSuite> failedSuites =43 new Parser(new File(f, FailedReporter.TESTNG_FAILED_XML).getAbsolutePath()).parse();44 XmlSuite failedSuite = failedSuites.iterator().next();45 Assert.assertEquals("42", failedSuite.getParameter("n"));46 XmlTest test = failedSuite.getTests().get(0);47 Assert.assertEquals("43", test.getParameter("o"));48 XmlClass c = test.getClasses().get(0);49 Assert.assertEquals("44", c.getAllParameters().get("p"));50 }51}...
Source:DomXmlParser.java
1package org.testng.xml.dom;2import org.testng.log4testng.Logger;3import org.testng.xml.ISuiteParser;4import org.testng.xml.Parser;5import org.testng.xml.XMLParser;6import org.testng.xml.XmlSuite;7import org.w3c.dom.Document;8import org.xml.sax.SAXException;9import javax.xml.parsers.DocumentBuilder;10import javax.xml.parsers.DocumentBuilderFactory;11import javax.xml.parsers.ParserConfigurationException;12import java.io.IOException;13import java.io.InputStream;14public class DomXmlParser extends XMLParser<XmlSuite> implements ISuiteParser {15 @Override16 public XmlSuite parse(String currentFile, InputStream inputStream, boolean loadClasses) {17 XmlSuite result = null;18 try {19 result = parse2(currentFile, inputStream, loadClasses);20 } catch (Exception e) {21 Logger.getLogger(DomXmlParser.class).error(e.getMessage(), e);22 }23 return result;24 }25 @Override26 public boolean accept(String fileName) {27 return Parser.hasFileScheme(fileName) && fileName.endsWith(".xml");28 }29 public XmlSuite parse2(String currentFile, InputStream inputStream, boolean loadClasses)30 throws ParserConfigurationException, SAXException, IOException {31 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();32 factory.setNamespaceAware(true); // never forget this!33 DocumentBuilder builder = factory.newDocumentBuilder();34 Document doc = builder.parse(inputStream);35 DomUtil xpu = new DomUtil(doc);36 XmlSuite result = new XmlSuite();37 xpu.populate(result);38 System.out.println(result.toXml());39 return result;40 }41}...
Source:TestNGContentHandlerTest.java
1package test.xml;2import org.testng.Assert;3import org.testng.annotations.Test;4import org.testng.xml.SuiteXmlParser;5import org.testng.xml.TestNGContentHandler;6import org.testng.xml.XmlInclude;7import test.SimpleBaseTest;8import java.io.FileInputStream;9import java.util.List;10public class TestNGContentHandlerTest extends SimpleBaseTest {11 @Test12 public void testDescriptionInclusion() throws Exception {13 final String xml = getPathToResource("xml/simple-suite-with-method-desc.xml");14 SuiteXmlParser parser = new SuiteXmlParser();15 TestNGContentHandler handler = new TestNGContentHandler(xml, false);16 parser.parse(new FileInputStream(xml), handler);17 List<XmlInclude> includes = handler.getSuite().getTests().get(0).getXmlClasses().get(0).getIncludedMethods();18 String desc = includes.get(0).getDescription();19 Assert.assertEquals("simple-description", desc);20 }21 public static class LocalTestClass {22 @Test23 public void helloWorld() {24 }25 }26}...
1<project xmlns="http://maven.apache.org/POM/4.0.0"2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">45<!-- ... -->67 <build>8 <plugins>9 <plugin>10 <groupId>org.apache.maven.plugins</groupId>11 <artifactId>maven-compiler-plugin</artifactId>12 <version>3.5.1</version>13 <configuration>14 <source>1.8</source>15 <target>1.8</target>16 </configuration>17 </plugin>18 </plugins>19 </build>2021<!-- ... -->2223</project>24
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
- JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
- 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.
- Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
- 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.
- Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
- 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.
- 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.
- 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.
- Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
- Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
- Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
- 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.
- Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
- 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.
- TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
- 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.
- 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.