Source:java.util.Date to XMLGregorianCalendar
public class DrawPanel extends JPanel {
//TODO not much
}
Best Testng code snippet using org.testng.xml.Xml
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:DomXmlParser.java
1package org.testng.xml.dom;2import org.testng.xml.ISuiteParser;3import org.testng.xml.XMLParser;4import org.testng.xml.XmlSuite;5import org.w3c.dom.Document;6import org.xml.sax.SAXException;7import javax.xml.parsers.DocumentBuilder;8import javax.xml.parsers.DocumentBuilderFactory;9import javax.xml.parsers.ParserConfigurationException;10import javax.xml.xpath.XPathExpressionException;11import java.io.IOException;12import java.io.InputStream;13public class DomXmlParser extends XMLParser<XmlSuite> implements ISuiteParser {14 @Override15 public XmlSuite parse(String currentFile, InputStream inputStream, boolean loadClasses) {16 XmlSuite result = null;17 try {18 result = parse2(currentFile, inputStream, loadClasses);19 } catch (Exception e) {20 e.printStackTrace();21 }22 return result;23 }24 @Override25 public boolean accept(String fileName) {26 return fileName.endsWith(".xml");27 }28 public XmlSuite parse2(String currentFile, InputStream inputStream,29 boolean loadClasses) throws ParserConfigurationException, SAXException,30 IOException, XPathExpressionException {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// XPathFactory xpathFactory = XPathFactory.newInstance();39// XPath xpath = xpathFactory.newXPath();40//41// {42// XPathExpression expr = xpath.compile("//suite");43// Object result = expr.evaluate(doc, XPathConstants.NODESET);44// NodeList nodes = (NodeList) result;45// for (int i = 0; i < nodes.getLength(); i++) {46// Node node = nodes.item(i);47// for (int j = 0; j < node.getAttributes().getLength(); j++) {48// System.out.println(node.getAttributes().item(j));49// }50// }51// }52// {53// XPathExpression expr = xpath.compile("//suite/@name");54// Object result = expr.evaluate(doc, XPathConstants.STRING);55// System.out.println("NAME:" + result);56// }57 System.out.println(result.toXml());58 return result;59 }60}...
Source:FailedReporterTest.java
...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:ShadowTest.java
1package test.parameters;2import org.testng.TestListenerAdapter;3import org.testng.TestNG;4import org.testng.annotations.Test;5import org.testng.xml.XmlClass;6import org.testng.xml.XmlInclude;7import org.testng.xml.XmlSuite;8import org.testng.xml.XmlTest;9import test.SimpleBaseTest;10import java.util.Arrays;11public class ShadowTest extends SimpleBaseTest {12 @Test13 public void parametersShouldNotBeShadowed() {14 XmlSuite s = createXmlSuite("s");15 XmlTest t = createXmlTest(s, "t");16 {17 XmlClass c1 = new XmlClass(Shadow1SampleTest.class.getName());18 XmlInclude include1 = new XmlInclude("test1");19 include1.setXmlClass(c1);20 c1.getLocalParameters().put("a", "First");21 c1.getIncludedMethods().add(include1);22 t.getXmlClasses().add(c1);23 }24 {25 XmlClass c2 = new XmlClass(Shadow2SampleTest.class.getName());26 XmlInclude include2 = new XmlInclude("test2");27 include2.setXmlClass(c2);28 c2.getLocalParameters().put("a", "Second");29 c2.getIncludedMethods().add(include2);30 t.getXmlClasses().add(c2);31 }32 TestNG tng = create();33 tng.setXmlSuites(Arrays.asList(s));34 TestListenerAdapter tla = new TestListenerAdapter();35 tng.addListener(tla);36 tng.run();37// System.out.println(s.toXml());38 assertTestResultsEqual(tla.getPassedTests(), Arrays.asList("test1", "test2"));39 }40}...
Source:VerifyTest.java
2import org.testng.Assert;3import org.testng.TestListenerAdapter;4import org.testng.TestNG;5import org.testng.annotations.Test;6import org.testng.xml.XmlClass;7import org.testng.xml.XmlInclude;8import org.testng.xml.XmlSuite;9import org.testng.xml.XmlTest;10import test.SimpleBaseTest;11import java.util.Arrays;12public class VerifyTest extends SimpleBaseTest {13 @Test14 public void verify() {15 XmlSuite suite = new XmlSuite();16 suite.setName("Suite");17 XmlTest test = new XmlTest(suite);18 test.setName("Test");19 XmlClass c1 = new XmlClass(B.class);20 c1.setIncludedMethods(Arrays.asList(new XmlInclude[] { new XmlInclude("b")}));21 XmlClass c2 = new XmlClass(Base.class);22 c2.setIncludedMethods(Arrays.asList(new XmlInclude[] { new XmlInclude("b")}));23 test.setXmlClasses(Arrays.asList(new XmlClass[] { c1, c2 }));24 TestNG tng = new TestNG();25 tng.setVerbose(0);26 tng.setXmlSuites(Arrays.asList(new XmlSuite[] { suite }));27 TestListenerAdapter tla = new TestListenerAdapter();28 tng.addListener(tla);29 tng.run();30 Assert.assertEquals(tla.getPassedTests().size(), 2);31 }32}...
Source:suiteRun.java
...3import java.util.ArrayList;4import java.util.List;56import org.testng.TestNG;7import org.testng.xml.XmlClass;8import org.testng.xml.XmlSuite;9import org.testng.xml.XmlTest;1011public class suiteRun {1213 public static void main(String arg[]) {14 TestNG testNG = new TestNG();15 XmlSuite xmlSuite = new XmlSuite();16 xmlSuite.setName("Test");17 xmlSuite.setParallel(XmlSuite.ParallelMode.TESTS);18 xmlSuite.setPreserveOrder(false);19 xmlSuite.setThreadCount(5);20 xmlSuite.setVerbose(1);21 XmlTest test = new XmlTest(xmlSuite);22 test.setName("TmpTest");23 List<XmlClass> classes = new ArrayList<XmlClass>();24 classes.add(new XmlClass("RestPost.multipleTest"));25 test.setXmlClasses(classes) ;26 List<XmlSuite> suiteList = new ArrayList<>();27 suiteList.add(xmlSuite);28 testNG.run();29 }3031}
...
Source:Bug90Test.java
1package test.bug90;2import org.testng.Assert;3import org.testng.TestNG;4import org.testng.annotations.Test;5import org.testng.xml.XmlClass;6import org.testng.xml.XmlInclude;7import org.testng.xml.XmlSuite;8import org.testng.xml.XmlTest;9import test.SimpleBaseTest;10import java.util.Arrays;11public class Bug90Test extends SimpleBaseTest {12 @Test(description = "Fix for https://github.com/cbeust/testng/issues/90")13 public void afterClassShouldRun() {14 XmlSuite s = createXmlSuite("Bug90");15 XmlTest t = createXmlTest(s, "Bug90 test", Sample.class.getName());16 XmlClass c = t.getClasses().get(0);17 c.setIncludedMethods(Arrays.asList(new XmlInclude("test1")));18 TestNG tng = create();19 tng.setXmlSuites(Arrays.asList(s));20 Sample.m_afterClassWasRun = false;21 tng.run();22 Assert.assertTrue(Sample.m_afterClassWasRun);23 }24}...
Source:MainTest.java
1package test.jason;2import org.testng.Assert;3import org.testng.TestNG;4import org.testng.annotations.Test;5import org.testng.xml.XmlClass;6import org.testng.xml.XmlInclude;7import org.testng.xml.XmlSuite;8import org.testng.xml.XmlTest;9import test.SimpleBaseTest;10import java.util.Arrays;11public class MainTest extends SimpleBaseTest {12 @Test13 public void afterClassShouldRun() {14 XmlSuite s = createXmlSuite("S");15 XmlTest t = createXmlTest(s, "T", Main.class.getName());16 XmlClass c = t.getXmlClasses().get(0);17 c.getIncludedMethods().add(new XmlInclude("test1"));18 t.setPreserveOrder("true");19 TestNG tng = create();20 tng.setXmlSuites(Arrays.asList(new XmlSuite[] { s }));21 Main.m_passed = false;22 tng.run();23 Assert.assertTrue(Main.m_passed);24 }25}...
Xml
Using AI Code Generation
1XmlSuite suite = new XmlSuite();2suite.setName("MySuite");3XmlTest test = new XmlTest(suite);4test.setName("MyTest");5test.setPreserveOrder("true");6List<XmlClass> classes = new ArrayList<>();7classes.add(new XmlClass("com.test.Test1"));8classes.add(new XmlClass("com.test.Test2"));9test.setXmlClasses(classes);10List<XmlSuite> suites = new ArrayList<>();11suites.add(suite);12TestNG tng = new TestNG();13tng.setXmlSuites(suites);14tng.run();15package com.test;16import org.testng.annotations.Test;17public class Test1 {18public void test1() {19System.out.println("Test 1");20}21}22package com.test;23import org.testng.annotations.Test;24public class Test2 {25public void test2() {26System.out.println("Test 2");27}28}
Xml
Using AI Code Generation
1XmlSuite suite = new XmlSuite();2suite.setName("MySuite");3XmlTest test = new XmlTest(suite);4test.setName("MyTest");5test.setParallel("tests");6test.setThreadCount(3);7List<XmlClass> classes = new ArrayList<XmlClass>();8classes.add(new XmlClass("test1"));9classes.add(new XmlClass("test2"));10classes.add(new XmlClass("test3"));11test.setXmlClasses(classes);12List<XmlSuite> suites = new ArrayList<XmlSuite>();13suites.add(suite);14TestNG tng = new TestNG();15tng.setXmlSuites(suites);16tng.run();17setName() – to set the name of the suite18setParallel() – to set the parallel mode19setThreadCount() – to set the number of threads20setXmlClasses() – to set the list of classes to be executed21setXmlSuites() – to set the list of suites to be executed22setName() – to set the name of the suite23setParallel() – to set the parallel mode24setThreadCount() – to set the number of threads25setXmlClasses() – to set the list of classes to be executed26setXmlSuites() – to set the list of suites to be executed27setParallel() – to set the parallel mode for the test methods28setThreadCount() – to set the number of threads for the test methods29setTimeOut() – to set the time out for the test methods30public void test1() {31 System.out.println("test1");32}33public void test2() {34 System.out.println("test2");35}36public void test3() {37 System.out.println("test3");38}39setParallel() – to set the parallel mode for the test methods40setThreadCount() – to set the number of threads for the
1public class DrawPanel extends JPanel {2 //TODO not much3}4
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!!