Best Testng code snippet using org.testng.xml.XmlTest.setGroups
Source:TestNgSuiteRunner.java
...198 parameters.put( "udid", deviceInfo );199 test.setParameters(parameters);200 if ( groups != null )201 {202 test.setGroups( groups );203 }204 test.setXmlClasses( classes );205 206 i++;207 }208 209 LOG.info("Número de threads em paralelo na suÃte: " + threadCount);210 suite.setThreadCount(threadCount);211 return suite;212 }213 214 /**Prepara suite de teste para o MobileCenter, com classes alternadas,215 * de forma que a execução sempre use o máximo de licenças disponÃveis.216 * Isto é, a continuação da execução das próximas classes nos próximos217 * dispositivos não aguarda o término da execução das classes anteriores218 * nos dispositivos anteriores.219 * 220 * @param classes Testar quais classes.221 * @param groups Testar somente as classes de quais grupos, se houver.222 * @param devices Executar em quais dispositivos.223 * @param threadCount Quantos testes em paralelo (em quantos dispositivos224 * ao mesmo tempo).225 * @return226 */227 protected XmlSuite setUpMobileCenterAlternatingSuite(228 List<String> testClassNames,229 XmlGroups groups,230 List<String> devices,231 int threadCount )232 {233 LOG.info( "Gerando suÃte para execução sobre o MobileCenter em 'LINHA' ..." );234 235 if ( threadCount <= 0 )236 {237 String message = "à necessário definir o número de threads.";238 throw new AutomationException( message );239 }240 241 ArrayList<ArrayList<XmlClass>> classes = new ArrayList<ArrayList<XmlClass>>();242 for ( String className : testClassNames )243 {244 ArrayList<XmlClass> singleList = new ArrayList<XmlClass>();245 singleList.add( new XmlClass(className) );246 classes.add( singleList );247 }248 249 XmlSuite suite = new XmlSuite();250 suite.setName( "MobileCenterSuite" );251 suite.setParallel( ParallelMode.TESTS );252 suite.setConfigFailurePolicy( FailurePolicy.SKIP );253 254 suite.addListener( br.com.safra.automation.selenium.listeners.255 TestListener.class.getCanonicalName() );256 257 suite.addListener( br.com.safra.automation.selenium.listeners.258 SuiteListener.class.getCanonicalName() );259 260 int i = 0;261 for ( ArrayList<XmlClass> singleList : classes )262 {263 String className = singleList.get( 0 ).getName();264 LOG.info( "Gerando os <test> para a classe :" + className );265 for ( String deviceInfo : devices )266 {267 LOG.info( "Gerando um <test> para o dispositivo '" + deviceInfo + "' ..." );268269 XmlTest test = new XmlTest(suite, i);270 test.setName( "Test-MobileCenter-" + className + "-" + deviceInfo );271 Map<String, String> parameters = new HashMap<String, String>();272 parameters.put( "udid", deviceInfo );273 test.setParameters(parameters);274 if ( groups != null )275 {276 test.setGroups( groups );277 }278 test.setXmlClasses( singleList );279 280 i++;281 }282 }283 284 LOG.info( "Número de threads em paralelo na suÃte: " + threadCount );285 suite.setThreadCount( threadCount );286 return suite;287 }288 289 /**Prepara uma suÃte de testng para abastecimento de massa de teste.290 * 291 * @param testClassNames os nomes canônicos das classes de teste.292 * @param groups os grupos a ser incluÃdos, ou null se nenhum.293 * @param devices os udids dos dispositivos onde será feita a execução.294 * @param threadCount a quantidade de threads simultâneas.295 * @param dispositivosDeMassa os udids dos dispositivos que296 * se quer abastecer de massa, cujos usuários serão usados para logar.297 * @return suÃte de Testng298 */299 protected XmlSuite setUpMassa(300 List<String> testClassNames,301 XmlGroups groups,302 List<String> devices,303 int threadCount,304 List<String> dispositivosDeMassa )305 {306 if ( threadCount <= 0 )307 {308 String message = "à necessário definir o número de threads.";309 throw new AutomationException( message );310 }311 312 ArrayList<ArrayList<XmlClass>> classes = new ArrayList<ArrayList<XmlClass>>();313 for ( String className : testClassNames )314 {315 ArrayList<XmlClass> singleList = new ArrayList<XmlClass>();316 singleList.add( new XmlClass(className) );317 classes.add( singleList );318 }319 320 XmlSuite suite = new XmlSuite();321 suite.setName( "MobileCenterSuite" );322 suite.setParallel( ParallelMode.TESTS );323 suite.setConfigFailurePolicy( FailurePolicy.SKIP );324 325 suite.addListener( br.com.safra.automation.selenium.listeners.326 TestListener.class.getCanonicalName() );327 328 suite.addListener( br.com.safra.automation.selenium.listeners.329 SuiteListener.class.getCanonicalName() );330 331 int i = 0;332 for ( ArrayList<XmlClass> singleList : classes )333 {334 String className = singleList.get( 0 ).getName();335 LOG.info( "Gerando os <test> para a classe :" + className );336 for ( String deviceInfo : dispositivosDeMassa )337 {338 LOG.info( "Gerando um <test> para o dispositivo '" + deviceInfo + "' ..." );339340 XmlTest test = new XmlTest(suite, i);341 test.setName( "Test-MobileCenter-" + className + "-" + deviceInfo );342 Map<String, String> parameters = new HashMap<String, String>();343 parameters.put( "udid", devices.get( 0 ) );344 parameters.put( "dispositivoDeMassa", deviceInfo.toString() );345 test.setParameters(parameters);346 if ( groups != null )347 {348 test.setGroups( groups );349 }350 test.setXmlClasses( singleList );351 352 i++;353 }354 }355 356 LOG.info( "Número de threads em paralelo na suÃte: " + threadCount );357 suite.setThreadCount( threadCount );358 return suite;359 }360}
...
Source:SimpleBaseTest.java
...163 return include;164 }165 protected static XmlGroups createXmlGroups(XmlSuite suite, String...includedGroupNames) {166 XmlGroups xmlGroups = createGroupIncluding(includedGroupNames);167 suite.setGroups(xmlGroups);168 return xmlGroups;169 }170 protected static XmlGroups createXmlGroups(XmlTest test, String...includedGroupNames) {171 XmlGroups xmlGroups = createGroupIncluding(includedGroupNames);172 test.setGroups(xmlGroups);173 return xmlGroups;174 }175 private static XmlGroups createGroupIncluding(String...groupNames) {176 XmlGroups xmlGroups = new XmlGroups();177 XmlRun xmlRun = new XmlRun();178 for (String group : groupNames) {179 xmlRun.onInclude(group);180 }181 xmlGroups.setRun(xmlRun);182 return xmlGroups;183 }184 protected static XmlTest createXmlTest(XmlSuite suite, String name, String... classes) {185 XmlTest result = createXmlTest(suite, name);186 int index = 0;...
Source:TestNGMethod.java
...171 testClass.setBeforeTestMethods(clone(tc.getBeforeTestMethods()));172 testClass.setAfterTestMethod(clone(tc.getAfterTestMethods()));173 clone.m_testClass= testClass;174 clone.setDate(getDate());175 clone.setGroups(getGroups());176 clone.setGroupsDependedUpon(getGroupsDependedUpon(), Collections.<String>emptyList());177 clone.setMethodsDependedUpon(getMethodsDependedUpon());178 clone.setAlwaysRun(isAlwaysRun());179 clone.m_beforeGroups= getBeforeGroups();180 clone.m_afterGroups= getAfterGroups();181 clone.m_currentInvocationCount= m_currentInvocationCount;182 clone.setMissingGroup(getMissingGroup());183 clone.setThreadPoolSize(getThreadPoolSize());184 clone.setDescription(getDescription());185 clone.setEnabled(getEnabled());186 clone.setParameterInvocationCount(getParameterInvocationCount());187 clone.setInvocationCount(getInvocationCount());188 clone.m_successPercentage = getSuccessPercentage();189 clone.setTimeOut(getTimeOut());190 clone.setRetryAnalyzer(getRetryAnalyzer());
...
Source:BeforeGroupsTest.java
...60 private static void createXmlTest(XmlSuite xmlSuite, String name, String group) {61 XmlTest xmlTest = new XmlTest(xmlSuite);62 xmlTest.setName(name);63 xmlTest.setClasses(Collections.singletonList(new XmlClass(SampleTestClass.class)));64 xmlTest.setGroups(groups(group));65 }66 private static XmlGroups groups(String group) {67 XmlGroups xmlGroups = new XmlGroups();68 XmlRun xmlRun = new XmlRun();69 xmlRun.onInclude(group);70 xmlGroups.setRun(xmlRun);71 return xmlGroups;72 }73 private static void runTest(XmlSuite.ParallelMode mode) throws IOException {74 XmlSuite suite = createXmlSuite("sample_suite");75 String pkg = BaseClassWithBeforeGroups.class.getPackage().getName();76 Class<?>[] classes = findClassesInPackage(pkg);77 XmlTest xmlTest = createXmlTestWithPackages(suite, "sample_test", classes);78 xmlTest.addIncludedGroup("regression");...
Source:DriverScript.java
...88 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();104 logger.info("Generated " + fileName + " file: " + file.getAbsolutePath());105 } catch (IOException e) {106 logger.info("Failed to generate " + fileName + " file");
...
Source:IssueTest.java
...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"),56 constructSuite().toXml().getBytes(Charset.defaultCharset()));57 List<String> args = Collections.singletonList(suitefile.toFile().getAbsolutePath());58 int status = exec(Collections.emptyList(), args);59 assertThat(status).isEqualTo(0);60 TimeUnit.MILLISECONDS.sleep(random.nextInt(5000));...
Source:DynamicTestNG.java
...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();...
Source:CommandLineOverridesXml.java
...23 private void runTest(String group, String excludedGroups, List<String> methods) {24 XmlSuite s = createXmlSuite(getClass().getName());25 XmlTest t = createXmlTest(s, "Test", OverrideSampleTest.class.getName());26 TestNG tng = create();27 if (group != null) tng.setGroups(group);28 if (excludedGroups != null) tng.setExcludedGroups(excludedGroups);29 tng.setXmlSuites(Arrays.asList(new XmlSuite[] { s }));30 TestListenerAdapter tla = new TestListenerAdapter();31 tng.addListener(tla);32 tng.run();33 assertTestResultsEqual(tla.getPassedTests(), methods);34 }35}...
setGroups
Using AI Code Generation
1import org.testng.xml.XmlTest;2import org.testng.xml.XmlClass;3import org.testng.xml.XmlGroups;4import org.testng.xml.XmlGroup;5import org.testng.xml.XmlSuite;6import org.testng.xml.XmlPackage;7import org.testng.xml.XmlInclude;8import java.util.ArrayList;9import java.util.Arrays;10import java.util.List;11import java.util.Map;12import java.util.HashMap;13public class TestNGXmlTestSetGroups {14 public static void main(String[] args) {15 XmlTest test = new XmlTest();16 test.setName("TestNGXmlTestSetGroups");17 XmlGroups groups = new XmlGroups();18 XmlGroup group = new XmlGroup();19 group.setName("group1");20 XmlInclude include = new XmlInclude("testMethod1");21 List<XmlInclude> includeList = new ArrayList<XmlInclude>();22 includeList.add(include);23 group.setIncludedMethods(includeList);24 List<XmlGroup> groupList = new ArrayList<XmlGroup>();25 groupList.add(group);26 groups.setRun(groupList);27 test.setGroups(groups);28 System.out.println(test.toXml());29 XmlSuite suite = new XmlSuite();30 suite.setName("TestNGXmlTestSetGroupsSuite");31 suite.addTest(test);32 System.out.println(suite.toXml());33 }34}
setGroups
Using AI Code Generation
1XmlTest test = context.getCurrentXmlTest();2test.setGroups(groups);3test.setTests(tests);4XmlSuite suite = context.getSuite().getXmlSuite();5suite.setIncludedGroups(groups);6suite.setIncludedTests(tests);7XmlSuite suite = context.getSuite().getXmlSuite();8suite.setIncludedGroups(groups);9suite.setIncludedTests(tests);10XmlSuite suite = context.getSuite().getXmlSuite();11suite.setIncludedGroups(groups);12suite.setIncludedTests(tests);13XmlSuite suite = context.getSuite().getXmlSuite();14suite.setIncludedGroups(groups);15suite.setIncludedTests(tests);16XmlSuite suite = context.getSuite().getXmlSuite();17suite.setIncludedGroups(groups);18suite.setIncludedTests(tests);19XmlSuite suite = context.getSuite().getXmlSuite();20suite.setIncludedGroups(groups);
setGroups
Using AI Code Generation
1package com.example;2import org.testng.annotations.Test;3public class TestClass {4 @Test(groups = { "group1" })5 public void testMethod1() {6 }7 @Test(groups = { "group2" })8 public void testMethod2() {9 }10}11package com.example;12import org.testng.annotations.Test;13public class TestClass {14 @Test(groups = { "group1" })15 public void testMethod1() {16 }17 @Test(groups = { "group2" })18 public void testMethod2() {19 }20}21package com.example;22import org.testng.annotations.Test;23public class TestClass {24 @Test(groups = { "group1" })25 public void testMethod1() {26 }27 @Test(groups = { "group2" })28 public void testMethod2() {29 }30}31package com.example;32import org.testng.annotations.Test;33public class TestClass {34 @Test(groups = { "group1" })35 public void testMethod1() {36 }37 @Test(groups = { "group2" })38 public void testMethod2() {39 }40}41package com.example;42import org.testng.annotations.Test;43public class TestClass {44 @Test(groups = { "group1" })45 public void testMethod1() {46 }47 @Test(groups = { "group2" })48 public void testMethod2() {49 }50}51package com.example;52import org.testng.annotations.Test;53public class TestClass {54 @Test(groups = { "group1" })55 public void testMethod1() {56 }57 @Test(groups = { "group2" })
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!!