Best Testng code snippet using org.testng.xml.XmlTest.setPreserveOrder
Source:PreserveOrderTest.java
...31 for (int i = 0; i < t.length; i++) {32 fullTestNames[i] = "test.preserveorder." + t[i];33 }34 XmlTest xt = createXmlTest(s, "Test", fullTestNames);35 xt.setPreserveOrder("true");36// System.out.println(s.toXml());37 tng.setXmlSuites(Arrays.asList(s));38 tng.run();39 // 3 methods per class, 3 classes, so the log should contain 9 calls40 List<String> log = BaseLogTest.getLog();41 Assert.assertEquals(log.size(), 9);42 // Every slice of 3 logs should belong to the same class in the same43 // order as in the specified string: AAA.a1, AAA.a2, AAA.a3, B.a1, etc...44 // Since we're only testing class ordering in this test, we only match45 // against the class name46 for (int i = 0; i < t.length; i++) {47 for (int j = 0; j < 3; j++) {48 Assert.assertTrue(log.get(j + (3 * i)).startsWith(t[i] + "."));49 }50 }51 }52 }53 @Test54 public void preserveMethodOrder() {55 String[][] methods = new String[][] {56 new String[] { "a1", "a2", "a3" },57 new String[] { "a1", "a3", "a2" },58 new String[] { "a2", "a1", "a3" },59 new String[] { "a2", "a3", "a1" },60 new String[] { "a3", "a2", "a1" },61 new String[] { "a3", "a1", "a2" },62 };63 for (String[] m : methods) {64 TestNG tng = create();65 // tng.setVerbose(2);66 XmlSuite s = createXmlSuite("Suite");67 String testName = "test.preserveorder.AAA";68 XmlTest xt = createXmlTest(s, "Test", testName);69 addMethods(xt.getXmlClasses().get(0), m);70 xt.setPreserveOrder("true");71 // System.out.println(s.toXml());72 tng.setXmlSuites(Arrays.asList(s));73 tng.run();74 List<String> log = BaseLogTest.getLog();75// System.out.println(log);76 for (int i = 0; i < log.size(); i++) {77 if (!log.get(i).endsWith(m[i])) {78 throw new AssertionError("Expected " + Arrays.asList(m) + " but got " + log);79 }80 }81 }82 }83 @Test84 public void orderShouldBePreservedWithDependencies() {85 TestNG tng = create();86 XmlSuite s = createXmlSuite("PreserveOrder");87 XmlTest t = new XmlTest(s);88 t.getXmlClasses().add(new XmlClass("test.preserveorder.ChuckTest4"));89 t.getXmlClasses().add(new XmlClass("test.preserveorder.ChuckTest3"));90 t.setPreserveOrder("true");91 tng.setXmlSuites(Arrays.asList(s));92 TestListenerAdapter tla = new TestListenerAdapter();93 tng.addListener(tla);94 tng.run();95 verifyPassedTests(tla, "c4TestOne", "c4TestTwo", "c4TestThree",96 "c3TestOne", "c3TestTwo", "c3TestThree");97 }98 @Test(dataProvider = "dp")99 public void preserveOrderAnna(String[] classes, String[] expectedMethods) {100 TestNG tng = create();101 XmlSuite s = createXmlSuite("PreserveOrder");102 XmlTest t = new XmlTest(s);103 for (String c : classes) {104 t.getXmlClasses().add(new XmlClass("test.preserveorder." + c));105 }106 t.setPreserveOrder("true");107 tng.setXmlSuites(Arrays.asList(s));108 TestListenerAdapter tla = new TestListenerAdapter();109 tng.addListener(tla);110 tng.run();111 try {112 verifyPassedTests(tla, expectedMethods);113 } catch(Exception ex) {114 ex.printStackTrace();115 }116 }117 @DataProvider118 public Object[][] dp() {119 String[][] classes = new String[][] {120 { "PrgTest", "SibTest", "EdnTest" },121 { "PrgTest", "EdnTest", "SibTest" },122 { "SibTest", "PrgTest", "EdnTest" },123 { "SibTest", "EdnTest", "PrgTest" },124 { "EdnTest", "PrgTest", "SibTest" },125 { "EdnTest", "SibTest", "PrgTest" },126 };127 String[][] expectedMethods = new String[][] {128 { "prg1", "prg2", "sib1", "sib2", "edn1", "edn2" },129 { "prg1", "prg2", "edn1", "edn2", "sib1", "sib2" },130 { "sib1", "sib2", "prg1", "prg2", "edn1", "edn2" },131 { "sib1", "sib2", "edn1", "edn2", "prg1", "prg2" },132 { "edn1", "edn2", "prg1", "prg2", "sib1", "sib2" },133 { "edn1", "edn2", "sib1", "sib2", "prg1", "prg2" },134 };135 List<Object[]> result = Lists.newArrayList();136 for (int i = 0; i < classes.length; i++) {137 Object[] o = new Object[] { classes[i], expectedMethods[i] };138 result.add(o);139 }140 return result.toArray(new Object[result.size()][]);141 }142 @Test(description = "preserve-order on a factory doesn't cause an NPE")143 public void factoryPreserve() {144 TestNG tng = create();145 XmlSuite s = createXmlSuite("FactoryPreserve");146 XmlTest t = new XmlTest(s);147 t.getXmlClasses().add(new XmlClass(TestClassFactory.class.getName()));148 t.setPreserveOrder("true");149 tng.setXmlSuites(Arrays.asList(s));150 tng.run();151 }152}...
Source:CreateTestNGTest.java
...59 int intRowNum = sheet.getPhysicalNumberOfRows();60 61 XmlTest xmlTest=new XmlTest(xmlSuite);62 xmlTest.setName("test");63 xmlTest.setPreserveOrder("true");64 65 for(int i =1;i<intRowNum;i++)66 {67 HSSFRow Exlrow = sheet.getRow(i);68 String flag = Exlrow.getCell(4).getStringCellValue();69 if(flag.equals("Yes"))70 {71 72 strScenarioName = Exlrow.getCell(1).getStringCellValue();73 strGadgetName = Exlrow.getCell(2).getStringCellValue();74 String scName = strGadgetName.split("_")[0];75 TCNames.add(strGadgetName);76 scenarioName.add(scName);77 }78 }79 /*XmlTest xmlTest=new XmlTest(xmlSuite);80 xmlTest.setName("test");81 //xmlTest.addParameter("Browser", Browser );82 xmlTest.setPreserveOrder("true");*/83 84 System.out.println(scenarioName);85 ArrayList<XmlInclude> methodsToRun = new ArrayList<XmlInclude>();86 87 for (int tc = 0; tc < scenarioName.size(); tc++) { 88 89 String testCaseName = TCNames.get(tc);90 String scName = testCaseName.split("_")[0];91 92 /*if(TCNames.get(tc-1).contains(scName)){93 XmlClass test1Class=new XmlClass(Class.forName("testCases."+ scName));94 List<XmlClass> listClass=new ArrayList<XmlClass>();95 listClass.add(test1Class);96 xmlTest.setXmlClasses(listClass);
...
Source:XmlVerifyTest.java
...67 @Test68 public void preserverOrderAttribute() {69 XmlSuite suite = new XmlSuite();70 XmlTest test = new XmlTest(suite);71 suite.setPreserveOrder(true);72 test.setPreserveOrder(false);73 Assert.assertFalse(test.getPreserveOrder());74 suite.setPreserveOrder(false);75 test.setPreserveOrder(true);76 Assert.assertTrue(test.getPreserveOrder());77 suite.setPreserveOrder((Boolean)null);78 test.setPreserveOrder(false);79 Assert.assertFalse(test.getPreserveOrder());80 suite.setPreserveOrder(false);81 test.setPreserveOrder((Boolean)null);82 Assert.assertFalse(test.getPreserveOrder());83 suite.setPreserveOrder((Boolean)null);84 test.setPreserveOrder(true);85 Assert.assertTrue(test.getPreserveOrder());86 suite.setPreserveOrder(true);87 test.setPreserveOrder((Boolean)null);88 Assert.assertTrue(test.getPreserveOrder());89 suite.setPreserveOrder((Boolean)null);90 test.setPreserveOrder((Boolean)null);91 Assert.assertNull(test.getPreserveOrder());92 }93}...
Source:runTestNGDynamically.java
...24 {25 this.className = className;26 }27 28 private void setPreserveOrder(Boolean preserveOrder)29 {30 this.preserveOrder = preserveOrder;31 }32 33 public void setTestName(String testName)34 {35 this.testName = testName;36 }37 38 public void setThreadCount(int threadCount)39 {40 this.threadCount = threadCount;41 }42 43 public void setGuiceStage(guiceStage gStage )44 {45 this.gStage = gStage;46 }47 48 public void setSuiteName(String suiteName)49 {50 this.suiteName = suiteName;51 }52 53 public String getSuiteName()54 {55 return suiteName;56 }57 58 public guiceStage getGuiceStage()59 {60 if(gStage.equals(guiceStage.NULL))61 {62 gStage = guiceStage.DEVELOPMENT;63 }64 return gStage;65 }66 67 public int getThreadCount()68 {69 return threadCount;70 }71 72 public String getTestName()73 {74 return testName;75 }76 77 public String getPreserveOrder()78 {79 return preserveOrder.toString();80 }81 82 public String getClassName()83 {84 return className;85 }86 87 88 public void setIncludeMethodList(List<String> includeMethods)89 {90 if(includeMethods.size() > 0)91 {92 includeMethod = true;93 }94 95 if(includeMethods.size() == 1){96 xmlincludes.add(new XmlInclude(includeMethods.get(0)));97 } 98 else if(includeMethods.size() >1)99 {100 for(int i =0; i<includeMethods.size();i++)101 {102 xmlincludes.add(new XmlInclude(includeMethods.get(i)));103 }104 }105 }106 107 public List<XmlInclude> getIncludeMethodList()108 {109 return xmlincludes;110 }111 112 public void runTestNGTest(Map<String,String> testngParams)113 {114 TestNG testng = new TestNG();115 XmlTest test = new XmlTest();116 XmlSuite suite = new XmlSuite();117 List<XmlClass> classes = new ArrayList<XmlClass>();118 List<XmlTest> tests = new ArrayList<XmlTest>();119 List<XmlSuite> suites = new ArrayList<XmlSuite>();120 121 testng.addListener(new HTMLReporter());122 testng.addListener(new JUnitXMLReporter());123 124 suite.setName(getSuiteName());125 suite.setGuiceStage(getGuiceStage().toString());126 suite.setDataProviderThreadCount(getThreadCount());127 128 test.setName(getTestName());129 test.setVerbose(2);130 test.setPreserveOrder(getPreserveOrder());131 test.setParameters(testngParams);132 test.setSuite(suite);133 134 XmlClass class1 = new XmlClass(getClassName());135 classes.add(class1);136 test.setClasses(classes);137 138 //this is the addition for include method139 if(includeMethod.equals(true))140 {141 class1.getIncludedMethods().addAll(getIncludeMethodList());142 }143 144 tests.add(test);...
Source:startTest.java
...20 XmlSuite suite = new XmlSuite();21 suite.setName("51offer");22 XmlTest test = new XmlTest(suite);23 test.setName("test");24 test.setPreserveOrder("true");25 List<XmlClass> classes = new ArrayList<XmlClass>();26 27 XmlClass registerClass = new XmlClass("com.testcase.register");28 29 /*List<XmlInclude> registerIncludes = new ArrayList<XmlInclude>();30 31 XmlInclude registerMethodOne= new XmlInclude("selectRegister");32 33 XmlInclude registerMethodTwo= new XmlInclude("register"); 34 XmlInclude registerMethodThree= new XmlInclude("finishRegister"); 35 //XmlInclude registerMethodFour= new XmlInclude("logout");36 registerIncludes.add(registerMethodOne);37 registerIncludes.add(registerMethodTwo);38 registerIncludes.add(registerMethodThree);39 //registerIncludes.add(registerMethodFour);40 41 registerClass.setIncludedMethods(registerIncludes); */42 43 XmlClass loginClass = new XmlClass("com.testcase.login");44 45 /*List<XmlInclude> loginIncludes = new ArrayList<XmlInclude>();46 XmlInclude loginMethodOne= new XmlInclude("selectLogin"); 47 XmlInclude loginMethodTwo= new XmlInclude("login"); 48 XmlInclude loginMethodThree= new XmlInclude("startThreeStep"); 49 XmlInclude loginMethodFour= new XmlInclude("oneStep");50 XmlInclude loginMethodFive= new XmlInclude("twoStep"); 51 XmlInclude loginMethodSix= new XmlInclude("threeStep");52 XmlInclude loginMethodSeven= new XmlInclude("logout");53 registerIncludes.add(loginMethodOne);54 registerIncludes.add(loginMethodTwo); 55 registerIncludes.add(loginMethodThree); 56 registerIncludes.add(loginMethodFour);57 registerIncludes.add(loginMethodFive); 58 registerIncludes.add(loginMethodSix); 59 registerIncludes.add(loginMethodSeven);60 61 loginClass.setIncludedMethods(loginIncludes); */62 63 classes.add(registerClass);64 classes.add(loginClass);65 66 test.setXmlClasses(classes) ;67 68 //testngListener tla = new testngListener();69 TestNG testng = new TestNG();70 //testng.addListener(tla);71 List<XmlSuite> suites = new ArrayList<XmlSuite>();72 suite.addListener("com.test.testngListener");73 suites.add(suite);74 testng.setXmlSuites(suites);75 testng.setPreserveOrder(true);76 testng.run();77 78 emailReport emailReportIns = emailReport.getInstance();79 if( emailReportIns != null )80 {81 if(( Config.bTestFailure )||( Config.bTestSkip ))82 {83 emailReportIns.sendEmail("×Ô¶¯»¯²âÊÔÓöµ½ÁË´íÎ󣬾ßÌåÇë²é¿´¸½¼þµÄ²âÊÔ±¨¸æ£¬",84 "test-output/emailable-report.html");85 }86 else87 {88 emailReportIns.sendEmail("×Ô¶¯»¯²âÊÔ˳ÀûÍê³É£¬¸½¼þΪ²âÊÔ±¨¸æ£¬",89 "test-output/emailable-report.html");...
Source:TestNgRunner.java
...28 xmlSuite.addListener("com.testcase.CustomeIsuiteListener");29 30 XmlTest xmlTest = new XmlTest(xmlSuite);31 xmlTest.setName("Test - 1");32 xmlTest.setPreserveOrder("true");33 34 XmlTest xmlTest2 = new XmlTest(xmlSuite);35 xmlTest.setName("Test - 2");36 xmlTest.setPreserveOrder("true");37 38 /*39 * To Include specific methods40 * */41 XmlInclude includeMethod = new XmlInclude("anotherPublicMethodTest");42 43 XmlClass publicTestClass = new XmlClass(TestPublicMethod.class);44 publicTestClass.setIncludedMethods(Arrays.asList(includeMethod));45 46 XmlClass staticTestClass = new XmlClass(TestStaticMethod.class);47 48 List<XmlClass> list = new ArrayList<XmlClass>();49 list.add(publicTestClass);50 ...
Source:TestNGExecutor.java
...28 for(TestNGModuleDTO o:list){29 suite=new XmlSuite();30 suite.setName(key);31 suite.setThreadCount(1);32 suite.setPreserveOrder("true");33 suite.setVerbose(3);34 35 36 XmlTest test=new XmlTest(suite);37 test.setPreserveOrder("true");38 test.setName(o.getTest_Scenario_Name());39 if(!classes.contains(xmClass)){40 xmClass.setName(o.getTESTCLASS());41 includeMethods.add(new XmlInclude(o.getMethod(), iMethodOrder));42 xmClass.setIncludedMethods(includeMethods);43 classes.add(xmClass);44 45 }46 else{47 includeMethods.add(new XmlInclude(o.getMethod(), iMethodOrder));48 xmClass.setIncludedMethods(includeMethods);49 }50 test.setClasses(classes);51 suites=new ArrayList<XmlSuite>();52 suites.add(suite);53 iMethodOrder++;54 }55 System.out.println(suite.toXml());56 TestNG tng=new TestNG();57 tng.setXmlSuites(suites);58 tng.setPreserveOrder(true);59 tng.run(); 60 61 }62 63 }64}...
Source:TestNG173Test.java
...16 XmlSuite s = createXmlSuite("PreserveOrder");17 XmlTest t = new XmlTest(s);18 t.getXmlClasses().add(new XmlClass("test.testng173.ClassA"));19 t.getXmlClasses().add(new XmlClass("test.testng173.ClassB"));20 t.setPreserveOrder("true");21 tng.setXmlSuites(Arrays.asList(s));22 TestListenerAdapter tla = new TestListenerAdapter();23 tng.addListener(tla);24 tng.run();25 // bug26 //verifyPassedTests(tla, "test1", "test2", "testX", "test1", "test2");27 // Proposed fix28 verifyPassedTests(tla, "test1", "test2", "testX", "test2", "test1");29 }30 @Test31 public void orderShouldBePreservedInMethodsWithSameNameAndInDifferentClassesAndDifferentPackage() {32 TestNG tng = create();33 XmlSuite s = createXmlSuite("PreserveOrder");34 XmlTest t = new XmlTest(s);35 t.getXmlClasses().add(new XmlClass("test.testng173.ClassA"));36 t.getXmlClasses().add(new XmlClass("test.testng173.anotherpackage.ClassC"));37 t.setPreserveOrder("true");38 tng.setXmlSuites(Arrays.asList(s));39 TestListenerAdapter tla = new TestListenerAdapter();40 tng.addListener(tla);41 tng.run();42 // bug43 //verifyPassedTests(tla, "test1", "test2", "testX", "test1", "test2");44 verifyPassedTests(tla, "test1", "test2", "testX", "test2", "test1");45 }46}...
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!!