Best Testng code snippet using org.testng.Interface IObjectFactory2
Source:TestExecutionService.java
1/*2 * The MIT License (MIT)3 *4 * Copyright (c) 2016 NovaTec Consulting GmbH5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in all14 * copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22 * SOFTWARE.23 */24package info.novatec.smoketest.core.service.testing;25import com.beust.jcommander.internal.Lists;26import com.google.common.base.Strings;27import com.google.inject.Injector;28import info.novatec.smoketest.core.SmokeTestConfiguration;29import info.novatec.smoketest.core.application.Environment;30import org.testng.IObjectFactory2;31import org.testng.IReporter;32import org.testng.TestNG;33import javax.inject.Inject;34import java.util.Arrays;35import java.util.List;36/**37 * TestNG implementation of {@link ITestExecutionService}.38 *39 * @author Claudio Waldvogel (claudio.waldvogel@novatec-gmbh.de)40 */41public class TestExecutionService implements ITestExecutionService {42 /**43 * The very last fallback output directory name.44 */45 private static final String DEFAULT_OUTPUT_DIRECTORY = "test-results";46 /**47 * The Gucie Injector which is used to enable dependency injection to test classes.48 */49 private Injector injector;50 /**51 * The running {@link Environment}.52 */53 private Environment<?> environment;54 /**55 * The effective {@link SmokeTestConfiguration}.56 */57 private SmokeTestConfiguration configuration;58 /**59 * The {@link TestNG} instance.60 */61 private TestNG testNG;62 /**63 * The list of all applied {@link IReporter}s.64 */65 private List<IReporter> reporters;66 /**67 * The output directory name.68 */69 private String outputDirectory = DEFAULT_OUTPUT_DIRECTORY;70 /**71 * Creates a new TestExecutionService.72 *73 * @param injector74 * The Gucie {@link Injector}75 * @param environment76 * The {@link Environment}77 * @param configuration78 * The {@link SmokeTestConfiguration}79 */80 @Inject81 public TestExecutionService(final Injector injector,82 final Environment environment,83 final SmokeTestConfiguration configuration) {84 this.injector = injector;85 this.environment = environment;86 this.configuration = configuration;87 this.reporters = Lists.newArrayList();88 }89 //-------------------------------------------------------------90 // Interface Implementation: ITestExecutionService91 //-------------------------------------------------------------92 @Override93 public void execute() {94 execute(environment.getTests());95 }96 @Override97 public void execute(Class<?>... tests) {98 execute(Arrays.asList(tests));99 }100 @Override101 public void execute(List<Class<?>> tests) {102 initializeOutputDirectory();103 initializeTestNG();104 testNG.setTestClasses(tests.toArray(new Class[tests.size()]));105 testNG.run();106 }107 @Override108 public void addReporter(Class<? extends IReporter> clazz) {109 addReporter(environment.getInjector().getInstance(clazz), false);110 }111 @Override112 public void addReporter(IReporter reporter) {113 addReporter(reporter, true);114 }115 @Override116 public void setReportDirectory(String outputDirectory) {117 this.outputDirectory = outputDirectory;118 }119 //-------------------------------------------------------------120 // Methods: Internals121 //-------------------------------------------------------------122 /**123 * Actually adds the reporter. If the the reporter was not instantiated with the Injector, the dependency injection124 * are injected125 *126 * @param reporter127 * The reporter to be added128 * @param performInjection129 * Flag to indicate if dependencies should be injected130 */131 private void addReporter(final IReporter reporter,132 boolean performInjection) {133 if (performInjection) {134 this.environment.getInjector().injectMembers(reporter);135 }136 this.reporters.add(reporter);137 }138 /**139 * Set the output directory.140 */141 private void initializeOutputDirectory() {142 if (Strings.isNullOrEmpty(outputDirectory)) {143 if (!Strings.isNullOrEmpty(configuration.getReportDirectory())) {144 outputDirectory = configuration.getReportDirectory();145 } else {146 outputDirectory = DEFAULT_OUTPUT_DIRECTORY;147 }148 }149 }150 /**151 * Initializes the TestNG test suite.152 */153 private void initializeTestNG() {154 if (testNG == null) {155 testNG = new TestNG(false);156 testNG.setDefaultSuiteName(environment.getName());157 testNG.setDefaultTestName("Unnamed (@Test missing)");158 testNG.setOutputDirectory(outputDirectory);159 //Add all reports as listener to the testNG engine160 reporters.forEach(reporter -> testNG.addListener(reporter));161 //We enable dependency injection to test classes by defining an IObjectFactory2162 //which instantiates all test classes by using the Gucie Injector163 testNG.setObjectFactory((IObjectFactory2) clazz -> this.injector.getInstance(clazz));164 }165 }166}...
Source:Testosterone.java
1package info.stasha.testosterone.jersey.testng;2import info.stasha.testosterone.SuperTestosterone;3import info.stasha.testosterone.TestInstrumentation;4import info.stasha.testosterone.Utils;5import info.stasha.testosterone.testng.AfterClassAnnotation;6import info.stasha.testosterone.testng.BeforeClassAnnotation;7import java.util.logging.Level;8import java.util.logging.Logger;9import org.testng.IObjectFactory2;10import org.testng.ITestObjectFactory;11import org.testng.annotations.ObjectFactory;12/**13 * TestNG Testosterone14 *15 * @author stasha16 */17public interface Testosterone extends info.stasha.testosterone.jersey.junit4.Testosterone {18 @ObjectFactory19 default ITestObjectFactory getFactory() {20 return new TestObjectFactory();21 }22 /**23 * Factory for creating new test class instances.24 *25 * @return26 */27 public static class TestObjectFactory implements IObjectFactory2 {28 @Override29 public Object newInstance(Class<?> cls) {30 try {31 if (!Utils.isTestosterone(cls)) {32 return cls.newInstance();33 }34 35 return TestInstrumentation.testClass((Class<? extends SuperTestosterone>) cls,36 new BeforeClassAnnotation(), new AfterClassAnnotation()).newInstance();37 } catch (Throwable ex) {38 Logger.getLogger(TestObjectFactory.class.getName()).log(Level.SEVERE, null, ex);39 throw new RuntimeException(ex);40 }41 }42 }43}...
Source:IObjectFactory2.java
1package org.testng;2/**3 * Factory used to create all test instances. This object factory only receives the class4 * in parameter.5 *6 * @see org.testng.IObjectFactory7 *8 * @author Cedric Beust <cedric@beust.com>9 *10 * @since 5.14.611 */12public interface IObjectFactory2 extends ITestObjectFactory {13 Object newInstance(Class<?> cls);14}...
Interface IObjectFactory2
Using AI Code Generation
1import org.testng.IObjectFactory2;2import org.testng.ITestContext;3import org.testng.ITestNGMethod;4import org.testng.internal.ObjectFactoryImpl;5import org.testng.internal.annotations.IAnnotationFinder;6import org.testng.internal.annotations.IAnnotationTransformer;7import org.testng.internal.annotations.IAnnotationTransformer2;8import org.testng.internal.annotations.IAnnotationTransformer3;9public class ObjectFactory implements IObjectFactory2, IAnnotationTransformer3 {10 private final ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();11 public Object newInstance(ITestNGMethod method, ITestContext context, IAnnotationFinder finder, long skipFailedInvocationCounts) {12 return objectFactory.newInstance(method, context, finder, skipFailedInvocationCounts);13 }14 public void addClass(Class<?> testClass) {15 objectFactory.addClass(testClass);16 }17 public void removeClass(Class<?> testClass) {18 objectFactory.removeClass(testClass);19 }20 public void transform(IAnnotationTransformer transformer, Class testClass, Constructor testConstructor, Method testMethod) {21 objectFactory.transform(transformer, testClass, testConstructor, testMethod);22 }23 public void transform(IAnnotationTransformer2 transformer, Class testClass, Constructor testConstructor, Method testMethod) {24 objectFactory.transform(transformer, testClass, testConstructor, testMethod);25 }26 public void transform(IAnnotationTransformer3 transformer, Class testClass, Constructor testConstructor, Method testMethod) {27 objectFactory.transform(transformer, testClass, testConstructor, testMethod);28 }29}30import org.testng.IObjectFactory2;31import org.testng.ITestContext;32import org.testng.ITestNGMethod;33import org.testng.internal.ObjectFactoryImpl;34import org.testng.internal.annotations.IAnnotationFinder;35import org.testng.internal.annotations.IAnnotationTransformer;36import org.testng.internal.annotations.IAnnotationTransformer2;37import org.testng.internal.annotations.IAnnotationTransformer3;38public class ObjectFactory implements IObjectFactory2, IAnnotationTransformer3 {39 private final ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();40 public Object newInstance(ITestNGMethod method, ITestContext context, IAnnotationFinder finder, long skipFailedInvocationCounts) {41 return objectFactory.newInstance(method, context, finder, skipFailedInvocationCounts);42 }
Interface IObjectFactory2
Using AI Code Generation
1import org.testng.IObjectFactory2;2import org.testng.ITestContext;3import org.testng.ITestNGMethod;4import org.testng.internal.ObjectFactoryImpl;5import org.testng.xml.XmlTest;6public class MyObjectFactory implements IObjectFactory2 {7 private ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();8 public Object newInstance(Class classToInstantiate, ITestContext context, ITestNGMethod method, Object[] parameters, Method methodToInvoke, XmlTest test) {9 return objectFactory.newInstance(classToInstantiate, context, method, parameters, methodToInvoke, test);10 }11 public Object newInstance(Class classToInstantiate, ITestContext context, ITestNGMethod method, Object[] parameters, Method methodToInvoke, XmlTest test, boolean inject) {12 return objectFactory.newInstance(classToInstantiate, context, method, parameters, methodToInvoke, test, inject);13 }14 public Object newInstance(Class classToInstantiate, ITestContext context, ITestNGMethod method, Object[] parameters, Method methodToInvoke, XmlTest test, boolean inject, long timeout) {15 return objectFactory.newInstance(classToInstantiate, context, method, parameters, methodToInvoke, test, inject, timeout);16 }17 public Object newInstance(Class classToInstantiate, ITestContext context, ITestNGMethod method, Object[] parameters, Method methodToInvoke, XmlTest test, boolean inject, long timeout, boolean skipFailedInvocationCounts) {18 return objectFactory.newInstance(classToInstantiate, context, method, parameters, methodToInvoke, test, inject, timeout, skipFailedInvocationCounts);19 }20}21import org.testng.IObjectFactory;22import org.testng.ITestContext;23import org.testng.ITestNGMethod;24import org.testng.internal.ObjectFactoryImpl;25import org.testng.xml.XmlTest;26public class MyObjectFactory implements IObjectFactory {27 private ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();28 public Object newInstance(Class classToInstantiate, ITestContext context) {29 return objectFactory.newInstance(classToInstantiate, context);30 }31 public Object newInstance(Class classToInstantiate, ITestContext context, ITestNGMethod method, Object[] parameters, Method methodToInvoke, XmlTest test) {32 return objectFactory.newInstance(classToInstantiate, context,
Interface IObjectFactory2
Using AI Code Generation
1package com.testng;2import org.testng.IObjectFactory;3import org.testng.IObjectFactory2;4import org.testng.ITestContext;5import org.testng.ITestNGMethod;6import org.testng.annotations.ObjectFactory;7import org.testng.internal.ObjectFactoryImpl;8import org.testng.internal.annotations.IAnnotationFinder;9import org.testng.internal.annotations.IAnnotationTransformer2;10import org.testng.internal.annotations.IAnnotationTransformer3;11import org.testng.internal.annotations.IAnnotationTransformer;12import org.testng.internal.annotations.IAnnotationFinder;13import org.testng.internal.annotations.IAnnotationTransformer2;14import org.testng.internal.annotations.IAnnotationTransformer3;15import org.testng.internal.annotations.IAnnotationTransformer;16import org.testng.internal.annotations.IAnnotationFinder;17import org.testng.internal.annotations.IAnnotationTransformer2;18import org.testng.internal.annotations.IAnnotationTransformer3;19import org.testng.internal.annotations.IAnnotationTransformer;20import org.testng.internal.annotations.IAnnotationFinder;21import org.testng.internal.annotations.IAnnotationTransformer2;22import org.testng.internal.annotations.IAnnotationTransformer3;23import org.testng.internal.annotations.IAnnotationTransformer;24import org.testng.internal.annotations.IAnnotationFinder;25import org.testng.internal.annotations.IAnnotationTransformer2;26import org.testng.internal.annotations.IAnnotationTransformer3;27import org.testng.internal.annotations.IAnnotationTransformer;28import org.testng.internal.annotations.IAnnotationFinder;29import org.testng.internal.annotations.IAnnotationTransformer2;30import org.testng.internal.annotations.IAnnotationTransformer3;31import org.testng.internal.annotations.IAnnotationTransformer;32import org.testng.internal.annotations.IAnnotationFinder;33import org.testng.internal.annotations.IAnnotationTransformer2;34import org.testng.internal.annotations.IAnnotationTransformer3;35import org.testng.internal.annotations.IAnnotationTransformer;36import org.testng.internal.annotations.IAnnotationFinder;37import org.testng.internal.annotations.IAnnotationTransformer2;38import org.testng.internal.annotations.IAnnotationTransformer3;39import org.testng.internal.annotations.IAnnotationTransformer;40import org.testng.internal.annotations.IAnnotationFinder;41import org.testng.internal.annotations.IAnnotationTransformer2;42import org.testng.internal.annotations.IAnnotationTransformer3;43import org.testng.internal.annotations.IAnnotationTransformer;44import org.testng.internal.annotations.IAnnotationFinder;45import org.testng.internal.annotations.IAnnotationTransformer2;46import org.testng.internal.annotations.IAnnotationTransformer3;47import org.testng.internal.annotations.IAnnotationTransformer;48import org.testng.internal.annotations.IAnnotationFinder;49import org.testng.internal.annotations.IAnnotationTransformer2;50import org.testng.internal.annotations.IAnnotationTransformer3;51import org.testng.internal.annotations.IAnnotationTransformer;52import org.testng.internal.annotations.IAnnotationFinder;53import org.testng.internal.annotations.IAnnotationTransformer2;54import org.testng.internal.annotations.IAnnotationTransformer3;55import
Interface IObjectFactory2
Using AI Code Generation
1public class ObjectFactory implements IObjectFactory2 {2 private static final Logger logger = Logger.getLogger(ObjectFactory.class);3 private static final String BROWSER = "browser";4 private static final String BROWSER_VERSION = "browserVersion";5 private static final String PLATFORM = "platform";6 private static final String PLATFORM_VERSION = "platformVersion";7 private static final String SCREEN_RESOLUTION = "screenResolution";8 private static final String SELENIUM_HOST = "seleniumHost";9 private static final String SELENIUM_PORT = "seleniumPort";10 private static final String SELENIUM_BROWSER_URL = "seleniumBrowserURL";11 private static final String SELENIUM_TIMEOUT = "seleniumTimeout";12 private static final String SELENIUM_IMPLICIT_WAIT = "seleniumImplicitWait";13 private static final String SELENIUM_PAGE_LOAD_TIMEOUT = "seleniumPageLoadTimeout";14 private static final String SELENIUM_SCRIPT_TIMEOUT = "seleniumScriptTimeout";15 private static final String SELENIUM_HUB_URL = "seleniumHubURL";16 private static final String SELENIUM_HUB_PORT = "seleniumHubPort";17 private static final String SELENIUM_HUB_BROWSER_URL = "seleniumHubBrowserURL";18 private static final String SELENIUM_HUB_TIMEOUT = "seleniumHubTimeout";19 private static final String SELENIUM_HUB_IMPLICIT_WAIT = "seleniumHubImplicitWait";20 private static final String SELENIUM_HUB_PAGE_LOAD_TIMEOUT = "seleniumHubPageLoadTimeout";21 private static final String SELENIUM_HUB_SCRIPT_TIMEOUT = "seleniumHubScriptTimeout";22 private static final String SELENIUM_HUB_MAX_INSTANCES = "seleniumHubMaxInstances";23 private static final String SELENIUM_HUB_PLATFORM = "seleniumHubPlatform";24 private static final String SELENIUM_HUB_BROWSER = "seleniumHubBrowser";25 private static final String SELENIUM_HUB_BROWSER_VERSION = "seleniumHubBrowserVersion";26 private static final String SELENIUM_HUB_SCREEN_RESOLUTION = "seleniumHubScreenResolution";27 private static final String SELENIUM_HUB_PLATFORM_VERSION = "seleniumHubPlatformVersion";28 private static final String SELENIUM_HUB_PLATFORM_NAME = "seleniumHubPlatformName";
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!!