Best Testng code snippet using org.testng.xml.XmlSuite.setConfigFailurePolicy
Source:TestNgSuiteRunner.java
...130 }131 132 XmlSuite suite = new XmlSuite();133 suite.setName("LocalSuite");134 suite.setConfigFailurePolicy( FailurePolicy.SKIP );135 136 suite.addListener(br.com.safra.automation.selenium.listeners137 .TestListener.class.getCanonicalName());138 suite.addListener(br.com.safra.automation.selenium.listeners139 .SuiteListener.class.getCanonicalName());140141 XmlTest test = new XmlTest(suite, 0);142 test.setName( "Test-Local-" + IdTest );143 Map<String, String> parameters = new HashMap<String, String>();144 test.setParameters(parameters);145 test.setXmlClasses( classes );146 147 return suite;148 }149 150 /**Prepara suite de teste para o MobileCenter, com as classes executadas151 * em seqüência, e sobre os dispositivos em paralelo.152 * 153 * @param classes Testar quais classes.154 * @param groups Testar somente as classes de quais grupos, se houver.155 * @param devices Executar em quais dispositivos.156 * @param threadCount Quantos testes em paralelo (em quantos dispositivos157 * ao mesmo tempo).158 * @return159 */160 protected XmlSuite setUpMobileCenterSuite(161 List<String> testClassNames,162 XmlGroups groups,163 List<String> devices,164 int threadCount )165 {166 LOG.info( "Gerando suÃte para execução sobre o MobileCenter em 'COLUNA' ..." );167 168 if ( threadCount <= 0 )169 {170 String message = "à necessário definir o número de threads.";171 throw new AutomationException( message );172 }173 174 ArrayList<XmlClass> classes = new ArrayList<XmlClass>();175 for ( String className : testClassNames )176 {177 classes.add( new XmlClass(className) );178 }179 180 XmlSuite suite = new XmlSuite();181 suite.setName( "MobileCenterSuite" );182 suite.setParallel( ParallelMode.TESTS );183 suite.setConfigFailurePolicy( FailurePolicy.SKIP );184 185 suite.addListener( br.com.safra.automation.selenium.listeners.186 TestListener.class.getCanonicalName() );187 188 suite.addListener( br.com.safra.automation.selenium.listeners.189 SuiteListener.class.getCanonicalName() );190 191 int i = 0;192 for (String deviceInfo : devices) {193 LOG.info("Gerando um <test> para o dispositivo '" + deviceInfo + "' ...");194195 XmlTest test = new XmlTest(suite, i);196 test.setName( "Test-MobileCenter-" + deviceInfo );197 Map<String, String> parameters = new HashMap<String, String>();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 {
...
Source:TestNGTestClassProcessor.java
...100 testNg.setThreadCount(options.getThreadCount());101 }102 Class<?> configFailurePolicyArgType = getConfigFailurePolicyArgType(testNg);103 Object configFailurePolicyArgValue = getConfigFailurePolicyArgValue(testNg);104 invokeVerifiedMethod(testNg, "setConfigFailurePolicy", configFailurePolicyArgType, configFailurePolicyArgValue, DEFAULT_CONFIG_FAILURE_POLICY);105 invokeVerifiedMethod(testNg, "setPreserveOrder", boolean.class, options.getPreserveOrder(), false);106 invokeVerifiedMethod(testNg, "setGroupByInstances", boolean.class, options.getGroupByInstances(), false);107 testNg.setUseDefaultListeners(options.getUseDefaultListeners());108 testNg.setVerbose(0);109 testNg.setGroups(CollectionUtils.join(",", options.getIncludeGroups()));110 testNg.setExcludedGroups(CollectionUtils.join(",", options.getExcludeGroups()));111 //adding custom test listeners before Gradle's listeners.112 //this way, custom listeners are more powerful and, for example, they can change test status.113 for (String listenerClass : options.getListeners()) {114 try {115 testNg.addListener(JavaReflectionUtil.newInstance(applicationClassLoader.loadClass(listenerClass)));116 } catch (Throwable e) {117 throw new GradleException(String.format("Could not add a test listener with class '%s'.", listenerClass), e);118 }119 }120 if (!options.getIncludedTests().isEmpty() || !options.getIncludedTestsCommandLine().isEmpty() || !options.getExcludedTests().isEmpty()) {121 testNg.addListener(new SelectedTestsFilter(options.getIncludedTests(),122 options.getExcludedTests(), options.getIncludedTestsCommandLine()));123 }124 if (!suiteFiles.isEmpty()) {125 testNg.setTestSuites(GFileUtils.toPaths(suiteFiles));126 } else {127 testNg.setTestClasses(testClasses.toArray(new Class<?>[0]));128 }129 testNg.addListener((Object) adaptListener(new TestNGTestResultProcessorAdapter(resultProcessor, idGenerator, clock)));130 testNg.run();131 }132 /**133 * The setter for configFailurePolicy has a different signature depending on TestNG version. This method uses reflection to134 * detect the API and return a reference to the correct argument type.135 * <ul>136 * <li>When TestNG >= 6.9.12, {@link TestNG#setConfigFailurePolicy(org.testng.xml.XmlSuite$FailurePolicy)}</li>137 * <li>When TestNG < 6.9.12, {@link TestNG#setConfigFailurePolicy(String)}</li>138 * </ul></li>139 *140 * @param testNg the TestNG instance141 * @return String.class or org.testng.xml.XmlSuite$FailurePolicy.class142 */143 private Class<?> getConfigFailurePolicyArgType(TestNG testNg) {144 Class<?> failurePolicy;145 try {146 failurePolicy = Class.forName("org.testng.xml.XmlSuite$FailurePolicy", false, testNg.getClass().getClassLoader());147 } catch (ClassNotFoundException e) {148 // new API not found; fallback to legacy String argument149 failurePolicy = String.class;150 }151 return failurePolicy;152 }153 /**154 * The setter for configFailurePolicy has a different signature depending on TestNG version. This method uses reflection to155 * detect the API. If not {@link String}, coerce the spec's string value to the expected enum value using {@link XmlSuite$FailurePolicy#getValidPolicy(String)}156 * <ul>157 * <li>When TestNG >= 6.9.12, {@link TestNG#setConfigFailurePolicy(org.testng.xml.XmlSuite$FailurePolicy)}</li>158 * <li>When TestNG < 6.9.12, {@link TestNG#setConfigFailurePolicy(String)}</li>159 * </ul></li>160 *161 * @param testNg the TestNG instance162 * @return Arg value; might be a String or an enum value of org.testng.xml.XmlSuite$FailurePolicy.class163 */164 private Object getConfigFailurePolicyArgValue(TestNG testNg) {165 Object configFailurePolicyArgValue;166 try {167 Class<?> failurePolicy = Class.forName("org.testng.xml.XmlSuite$FailurePolicy", false, testNg.getClass().getClassLoader());168 Method getValidPolicy = failurePolicy.getMethod("getValidPolicy", String.class);169 configFailurePolicyArgValue = getValidPolicy.invoke(null, options.getConfigFailurePolicy());170 } catch (Exception e) {171 // unable to invoke new API method; fallback to legacy String value172 configFailurePolicyArgValue = options.getConfigFailurePolicy();...
Source:Controller.java
...38 // Create an instance of XML Suite and assign a name for it.39 XmlSuite mySuite = new XmlSuite();40 mySuite.setName("pCloudy Suite");41 mySuite.setParallel("tests");42 //mySuite.setConfigFailurePolicy("continue");43 mySuite.setConfigFailurePolicy(FailurePolicy.CONTINUE);44 // Create a list which can contain the classes that you want to run.45 List<XmlClass> myClasses = new ArrayList<XmlClass>();46 myClasses.add(new XmlClass("com.pcloudy.testng.MakePaymentTest"));47 // Create a list of XmlTests and add the Xmltest you created earlier to it.48 List<XmlTest> allDevicesTests = new ArrayList<XmlTest>();49 Connector con = new Connector("https://device.pcloudy.com/api/");50 // User Authentication over pCloudy51 String authToken = con.authenticateUser("vandhithav@gmail.com","rzf46dk39m4v2ztkqykndnh6");52 ArrayList<MobileDevice> selectedDevices = new ArrayList<>();53 selectedDevices.addAll(con.chooseDevices(authToken, "android", new Version("7.*.*"),new Version("8.*.*"), 1));54 String sessionName = selectedDevices.get(0).display_name + " Appium Session";55 // Select apk in pCloudy Cloud Drive56 File fileToBeUploaded = new File("./My Airtel_com.myairtelapp.apk");57 PDriveFileDTO alreadyUploadedApp = con.getAvailableAppIfUploaded(authToken, fileToBeUploaded.getName());...
Source:TestSuiteGenerator.java
...35public class TestSuiteGenerator {36 static int testcnt = 0;37 public static void main(String[] args) throws Exception {38 XmlSuite xmlSuite = new XmlSuite();39 xmlSuite.setConfigFailurePolicy(FailurePolicy.CONTINUE);40 xmlSuite.setName("od");41 Map<String, String> mpconfiginfo = getConfigInfo(System.getProperty("user.dir") + "/Controller.xlsx");42 xmlSuite.setParallel(ParallelMode.NONE);43 xmlSuite.addListener("com.od.listeners.TestListener");44 XmlTest xmlTest = null;45 xmlSuite.setThreadCount(1);46 /* To add parameter at suite level */47 xmlSuite.setParameters(mpconfiginfo);48 addClasses(xmlSuite, xmlTest, mpconfiginfo.get("browserName"));49 TestNG testng = new TestNG();50 List<XmlSuite> suites = new ArrayList<XmlSuite>();51 suites.add(xmlSuite);52 testng.setXmlSuites(suites);53 Files.write(Paths.get(System.getProperty("user.dir") + "/TestSuite.xml"), xmlSuite.toXml().getBytes());...
Source:TestRunner.java
...67 testng.setDataProviderThreadCount(suites.getNumDataProviderThreads());68 testng.setXmlSuites(xmlSuites);69 testng.setUseDefaultListeners(false);70 testng.setParallel("methods");71 testng.setConfigFailurePolicy("continue");72 testng.setVerbose(100);73 testng.run();74 }75 private static XmlSuite createXmlSuite(String singleSuite) {76 XmlSuite suite = new XmlSuite();77 suite.setName("Selgp QA - " + singleSuite + " tests");78 suite.setParallel("methods");79 suite.setDataProviderThreadCount(suites.getNumDataProviderThreads());80 suite.setThreadCount(suites.getNumThreads());81 suite.setTimeOut(String.valueOf(suites.getTimeout()));82 suite.setConfigFailurePolicy("continue");83 suite.setVerbose(100);84 if (!TestRunner.getInstance().PARAMETER.equals("unset")) {85 String[] keyAndValue = TestRunner.getInstance().PARAMETER.split("=");86 Map<String, String> parameterMap = new HashMap<String, String>();87 parameterMap.put(keyAndValue[0], keyAndValue[1]);88 suite.setParameters(parameterMap);89 }90 List<XmlClass> xmlClasses = null;91 if (!TestRunner.getInstance().CLASS_NAME.equals("unset")) {92 xmlClasses = getClassesFromArguments();93 } else {94 xmlClasses = getClassesForSuite();95 }96 createSuiteXmlTest(suite, TestRunner.getInstance().suites, xmlClasses);...
Source:TestNGRunner.java
...35public class TestNGRunner {36 static int testcnt = 0;37 public static void main(String[] args) throws Exception {38 XmlSuite xmlSuite = new XmlSuite();39 xmlSuite.setConfigFailurePolicy(FailurePolicy.CONTINUE);40 xmlSuite.setName("Actitime");41 Map<String, String> mpconfiginfo = getConfigInfo(System.getProperty("user.dir") + "/Controller.xlsx");42 xmlSuite.setParallel(getParallelMode(mpconfiginfo.get("Parallel")));43 xmlSuite.addListener("com.tyss.acttime.listeners.TestListener");44 XmlTest xmlTest = null;45 int threadcnt = Integer.parseInt(mpconfiginfo.get("ThreadCount"));46 xmlSuite.setThreadCount(threadcnt);47 int noEdgeInstances = Integer.parseInt(mpconfiginfo.get("Edge"));48 int noFFInstances = Integer.parseInt(mpconfiginfo.get("Firefox"));49 int noChromeInstances = Integer.parseInt(mpconfiginfo.get("Chrome"));50 for (int i = 1; i <= noChromeInstances; i++) {51 addClasses(xmlSuite, xmlTest, "Chrome");52 }53 for (int i = 1; i <= noEdgeInstances; i++) {...
Source:FintellixTestAutomation.java
...83 testng.setDefaultSuiteName("Fintellix Default Test Suite");84 testng.setSuiteThreadPoolSize(20);85 testng.setXmlSuites(suites);86 testng.setVerbose(testng_verbocity);87 testng.setConfigFailurePolicy("continue");88 // testng.setThreadCount(50);89 if(enableATUReports){90 List <Class> listenerList = new ArrayList <Class>();91 listenerList.add(ATUReportsListener.class);92 listenerList.add(ConfigurationListener.class);93 listenerList.add(MethodListener.class);94 testng.setListenerClasses(listenerList);95 }96 97 logger.info("03: running testNg Tests");98 testng.run();99 }100101 private XmlSuite createTestSuite(String testDataFile) {
...
Source:IssueTest.java
...10 public void ensureAfterGroupsAreInvokedWithAlwaysRunAttribute() {11 XmlSuite xmlsuite = createXmlSuite("sample_suite", "sample_test", TestClassSample.class);12 xmlsuite.addIncludedGroup("123");13 TestNG testng = create(xmlsuite);14 testng.setConfigFailurePolicy(FailurePolicy.CONTINUE);15 LocalConfigListener listener = new LocalConfigListener();16 testng.addListener(listener);17 testng.run();18 assertThat(listener.getMessages()).containsExactly("after");19 }20}...
setConfigFailurePolicy
Using AI Code Generation
1XmlSuite suite = new XmlSuite();2suite.setName("MySuite");3suite.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);4XmlTest test = new XmlTest(suite);5test.setName("MyTest");6test.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);7XmlClass clazz = new XmlClass("com.test.MyClass");8clazz.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);9XmlMethodSelector selector = new XmlMethodSelector();10selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);11XmlMethodSelector selector = new XmlMethodSelector();12selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);13XmlMethodSelector selector = new XmlMethodSelector();14selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);15XmlMethodSelector selector = new XmlMethodSelector();16selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);17XmlMethodSelector selector = new XmlMethodSelector();18selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);19XmlMethodSelector selector = new XmlMethodSelector();20selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);21XmlMethodSelector selector = new XmlMethodSelector();22selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);23XmlMethodSelector selector = new XmlMethodSelector();24selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);25XmlMethodSelector selector = new XmlMethodSelector();26selector.setConfigFailurePolicy(XmlSuite.ConfigFailurePolicy.CONTINUE);
setConfigFailurePolicy
Using AI Code Generation
1import org.testng.xml.XmlSuite;2public class SetConfigFailurePolicy {3 public static void main(String[] args) {4 XmlSuite suite = new XmlSuite();5 System.out.println("Config failure policy is set to continue");6 }7}
setConfigFailurePolicy
Using AI Code Generation
1public class TestNGConfigFailurePolicy {2 public void test1() {3 System.out.println("Test1");4 }5 public void test2() {6 System.out.println("Test2");7 Assert.assertTrue(false);8 }9 public void test3() {10 System.out.println("Test3");11 Assert.assertTrue(false);12 }13 public void test4() {14 System.out.println("Test4");15 }16}17public class TestNGConfigFailurePolicy {18 public void test1() {19 System.out.println("Test1");20 }21 public void test2() {22 System.out.println("Test2");23 Assert.assertTrue(false);24 }25 public void test3() {26 System.out.println("Test3");27 Assert.assertTrue(false);28 }29 public void test4() {30 System.out.println("Test4");31 }32}33 Assert.assertTrue(false);34 Assert.assertTrue(false);35 Assert.assertTrue(false);36 Assert.assertTrue(false);37 Assert.assertTrue(false);38 Assert.assertTrue(false);39 Assert.assertTrue(false);
setConfigFailurePolicy
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.xml.Parser;3import org.testng.xml.XmlSuite;4import java.io.File;5import java.util.List;6public class SetFailurePolicy {7 public static void main(String[] args) throws Exception {8 TestNG runner=new TestNG();9 List<String> suitefiles= new ArrayList<String>();10 suitefiles.add("C:\\Users\\Vaibhav\\Desktop\\testng.xml");11 runner.setTestSuites(suitefiles);12 XmlSuite suite = new XmlSuite();13 suite.setConfigFailurePolicy(XmlSuite.CONTINUE);14 Parser parser = new Parser(suitefiles.get(0));15 suite = parser.parseToList().get(0);16 System.out.println("Failure Policy of the Test Suite: " + suite.getConfigFailurePolicy());17 runner.run();18 }19}
setConfigFailurePolicy
Using AI Code Generation
1XmlSuite suite = new XmlSuite();2suite.setConfigFailurePolicy("skip");3XmlSuite suite = new XmlSuite();4suite.setConfigFailurePolicy("stop");5XmlSuite suite = new XmlSuite();6suite.setConfigFailurePolicy("skip");7XmlSuite suite = new XmlSuite();8suite.setConfigFailurePolicy("stop");
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!!