Best Testng code snippet using org.testng.SuiteRunner.getObjectFactory
Source:SuiteRunner.java
...110 m_useDefaultListeners = useDefaultListeners;111 m_tmpRunnerFactory= runnerFactory;112 m_methodInterceptors = methodInterceptors != null ? methodInterceptors : new ArrayList<IMethodInterceptor>();113 setOutputDir(outputDir);114 m_objectFactory = m_configuration.getObjectFactory();115 if(m_objectFactory == null) {116 m_objectFactory = suite.getObjectFactory();117 }118 // Add our own IInvokedMethodListener119 m_invokedMethodListeners = Maps.newHashMap();120 if (invokedMethodListener != null) {121 for (IInvokedMethodListener listener : invokedMethodListener) {122 m_invokedMethodListeners.put(listener.getClass(), listener);123 }124 }125 m_invokedMethodListeners.put(getClass(), this);126 m_skipFailedInvocationCounts = suite.skipFailedInvocationCounts();127 if (null != testListeners) {128 m_testListeners.addAll(testListeners);129 }130 if (null != classListeners) {131 m_classListeners.addAll(classListeners);132 }133 m_runnerFactory = buildRunnerFactory();134 // Order the <test> tags based on their order of appearance in testng.xml135 List<XmlTest> xmlTests = m_suite.getTests();136 Collections.sort(xmlTests, new Comparator<XmlTest>() {137 @Override138 public int compare(XmlTest arg0, XmlTest arg1) {139 return arg0.getIndex() - arg1.getIndex();140 }141 });142 for (XmlTest test : xmlTests) {143 TestRunner tr = m_runnerFactory.newTestRunner(this, test, m_invokedMethodListeners.values(), m_classListeners);144 //145 // Install the method interceptor, if any was passed146 //147 for (IMethodInterceptor methodInterceptor : m_methodInterceptors) {148 tr.addMethodInterceptor(methodInterceptor);149 }150 // Reuse the same text reporter so we can accumulate all the results151 // (this is used to display the final suite report at the end)152 tr.addListener(m_textReporter);153 m_testRunners.add(tr);154 // Add the methods found in this test to our global count155 m_allTestMethods.addAll(Arrays.asList(tr.getAllTestMethods()));156 }157 }158 @Override159 public XmlSuite getXmlSuite() {160 return m_suite;161 }162 @Override163 public String getName() {164 return m_suite.getName();165 }166 public void setObjectFactory(ITestObjectFactory objectFactory) {167 m_objectFactory = objectFactory;168 }169 public void setReportResults(boolean reportResults) {170 m_useDefaultListeners = reportResults;171 }172 private void invokeListeners(boolean start) {173 for (ISuiteListener sl : m_listeners.values()) {174 if (start) {175 sl.onStart(this);176 }177 else {178 sl.onFinish(this);179 }180 }181 }182 private void setOutputDir(String outputdir) {183 if (isStringBlank(outputdir) && m_useDefaultListeners) {184 outputdir = DEFAULT_OUTPUT_DIR;185 }186 m_outputDir = (null != outputdir) ? new File(outputdir).getAbsolutePath()187 : null;188 }189 private ITestRunnerFactory buildRunnerFactory() {190 ITestRunnerFactory factory = null;191 if (null == m_tmpRunnerFactory) {192 factory = new DefaultTestRunnerFactory(m_configuration,193 m_testListeners.toArray(new ITestListener[m_testListeners.size()]),194 m_useDefaultListeners, m_skipFailedInvocationCounts);195 }196 else {197 factory = new ProxyTestRunnerFactory(198 m_testListeners.toArray(new ITestListener[m_testListeners.size()]),199 m_tmpRunnerFactory);200 }201 return factory;202 }203 @Override204 public String getParallel() {205 return m_suite.getParallel().toString();206 }207 public String getParentModule() {208 return m_suite.getParentModule();209 }210 @Override211 public String getGuiceStage() {212 return m_suite.getGuiceStage();213 }214 public Injector getParentInjector() {215 return m_parentInjector;216 }217 public void setParentInjector(Injector injector) {218 m_parentInjector = injector;219 }220 @Override221 public void run() {222 invokeListeners(true /* start */);223 try {224 privateRun();225 }226 finally {227 invokeListeners(false /* stop */);228 }229 }230 private void privateRun() {231 // Map for unicity, Linked for guaranteed order232 Map<Method, ITestNGMethod> beforeSuiteMethods= new LinkedHashMap<>();233 Map<Method, ITestNGMethod> afterSuiteMethods = new LinkedHashMap<>();234 IInvoker invoker = null;235 // Get the invoker and find all the suite level methods236 for (TestRunner tr: m_testRunners) {237 // TODO: Code smell. Invoker should belong to SuiteRunner, not TestRunner238 // -- cbeust239 invoker = tr.getInvoker();240 for (ITestNGMethod m : tr.getBeforeSuiteMethods()) {241 beforeSuiteMethods.put(m.getMethod(), m);242 }243 for (ITestNGMethod m : tr.getAfterSuiteMethods()) {244 afterSuiteMethods.put(m.getMethod(), m);245 }246 }247 //248 // Invoke beforeSuite methods (the invoker can be null249 // if the suite we are currently running only contains250 // a <file-suite> tag and no real tests)251 //252 if (invoker != null) {253 if(beforeSuiteMethods.values().size() > 0) {254 invoker.invokeConfigurations(null,255 beforeSuiteMethods.values().toArray(new ITestNGMethod[beforeSuiteMethods.size()]),256 m_suite, m_suite.getParameters(), null, /* no parameter values */257 null /* instance */258 );259 }260 Utils.log("SuiteRunner", 3, "Created " + m_testRunners.size() + " TestRunners");261 //262 // Run all the test runners263 //264 boolean testsInParallel = XmlSuite.ParallelMode.TESTS.equals(m_suite.getParallel());265 if (!testsInParallel) {266 runSequentially();267 }268 else {269 runInParallelTestMode();270 }271// SuitePlan sp = new SuitePlan();272// for (TestRunner tr : m_testRunners) {273// sp.addTestPlan(tr.getTestPlan());274// }275// sp.dump();276 //277 // Invoke afterSuite methods278 //279 if (afterSuiteMethods.values().size() > 0) {280 invoker.invokeConfigurations(null,281 afterSuiteMethods.values().toArray(new ITestNGMethod[afterSuiteMethods.size()]),282 m_suite, m_suite.getAllParameters(), null, /* no parameter values */283 null /* instance */);284 }285 }286 }287 private List<IReporter> m_reporters = Lists.newArrayList();288 private void addReporter(IReporter listener) {289 m_reporters.add(listener);290 }291 void addConfigurationListener(IConfigurationListener listener) {292 m_configuration.addConfigurationListener(listener);293 }294 public List<IReporter> getReporters() {295 return m_reporters;296 }297 private void runSequentially() {298 for (TestRunner tr : m_testRunners) {299 runTest(tr);300 }301 }302 private void runTest(TestRunner tr) {303 tr.run();304 ISuiteResult sr = new SuiteResult(m_suite, tr);305 m_suiteResults.put(tr.getName(), sr);306 }307 /**308 * Implement <suite parallel="tests">.309 * Since this kind of parallelism happens at the suite level, we need a special code path310 * to execute it. All the other parallelism strategies are implemented at the test level311 * in TestRunner#createParallelWorkers (but since this method deals with just one <test>312 * tag, it can't implement <suite parallel="tests">, which is why we're doing it here).313 */314 private void runInParallelTestMode() {315 List<Runnable> tasks= Lists.newArrayList(m_testRunners.size());316 for(TestRunner tr: m_testRunners) {317 tasks.add(new SuiteWorker(tr));318 }319 ThreadUtil.execute(tasks, m_suite.getThreadCount(),320 m_suite.getTimeOut(XmlTest.DEFAULT_TIMEOUT_MS), false);321 }322 private class SuiteWorker implements Runnable {323 private TestRunner m_testRunner;324 public SuiteWorker(TestRunner tr) {325 m_testRunner = tr;326 }327 @Override328 public void run() {329 Utils.log("[SuiteWorker]", 4, "Running XML Test '"330 + m_testRunner.getTest().getName() + "' in Parallel");331 runTest(m_testRunner);332 }333 }334 /**335 * Registers ISuiteListeners interested in reporting the result of the current336 * suite.337 *338 * @param reporter339 */340 protected void addListener(ISuiteListener reporter) {341 m_listeners.put(reporter.getClass(), reporter);342 }343 @Override344 public void addListener(ITestNGListener listener) {345 if (listener instanceof IInvokedMethodListener) {346 IInvokedMethodListener invokedMethodListener = (IInvokedMethodListener) listener;347 m_invokedMethodListeners.put(invokedMethodListener.getClass(), invokedMethodListener);348 }349 if (listener instanceof ISuiteListener) {350 addListener((ISuiteListener) listener);351 }352 if (listener instanceof IReporter) {353 addReporter((IReporter) listener);354 }355 if (listener instanceof IConfigurationListener) {356 addConfigurationListener((IConfigurationListener) listener);357 }358 }359 @Override360 public String getOutputDirectory() {361 return m_outputDir + File.separatorChar + getName();362 }363 @Override364 public Map<String, ISuiteResult> getResults() {365 return m_suiteResults;366 }367 /**368 * FIXME: should be removed?369 *370 * @see org.testng.ISuite#getParameter(java.lang.String)371 */372 @Override373 public String getParameter(String parameterName) {374 return m_suite.getParameter(parameterName);375 }376 /**377 * @see org.testng.ISuite#getMethodsByGroups()378 */379 @Override380 public Map<String, Collection<ITestNGMethod>> getMethodsByGroups() {381 Map<String, Collection<ITestNGMethod>> result = Maps.newHashMap();382 for (TestRunner tr : m_testRunners) {383 ITestNGMethod[] methods = tr.getAllTestMethods();384 for (ITestNGMethod m : methods) {385 String[] groups = m.getGroups();386 for (String groupName : groups) {387 Collection<ITestNGMethod> testMethods = result.get(groupName);388 if (null == testMethods) {389 testMethods = Lists.newArrayList();390 result.put(groupName, testMethods);391 }392 testMethods.add(m);393 }394 }395 }396 return result;397 }398 /**399 * @see org.testng.ISuite#getInvokedMethods()400 */401 @Override402 public Collection<ITestNGMethod> getInvokedMethods() {403 return getIncludedOrExcludedMethods(true /* included */);404 }405 /**406 * @see org.testng.ISuite#getExcludedMethods()407 */408 @Override409 public Collection<ITestNGMethod> getExcludedMethods() {410 return getIncludedOrExcludedMethods(false/* included */);411 }412 private Collection<ITestNGMethod> getIncludedOrExcludedMethods(boolean included) {413 List<ITestNGMethod> result= Lists.newArrayList();414 for (TestRunner tr : m_testRunners) {415 Collection<ITestNGMethod> methods = included ? tr.getInvokedMethods() : tr.getExcludedMethods();416 for (ITestNGMethod m : methods) {417 result.add(m);418 }419 }420 return result;421 }422 @Override423 public IObjectFactory getObjectFactory() {424 return m_objectFactory instanceof IObjectFactory ? (IObjectFactory) m_objectFactory : null;425 }426 @Override427 public IObjectFactory2 getObjectFactory2() {428 return m_objectFactory instanceof IObjectFactory2 ? (IObjectFactory2) m_objectFactory : null;429 }430 /**431 * Returns the annotation finder for the given annotation type.432 * @return the annotation finder for the given annotation type.433 */434 @Override435 public IAnnotationFinder getAnnotationFinder() {436 return m_configuration.getAnnotationFinder();437 }438 public static void ppp(String s) {439 System.out.println("[SuiteRunner] " + s);440 }441 /**...
Source:SuiteDispatcher.java
...111 tmpSuite.setParameters(suite.getParameters());112 tmpSuite.setThreadCount(suite.getThreadCount());113 tmpSuite.setDataProviderThreadCount(suite.getDataProviderThreadCount());114 tmpSuite.setVerbose(suite.getVerbose());115 tmpSuite.setObjectFactory(suite.getObjectFactory());116 XmlTest tmpTest = new XmlTest(tmpSuite);117 tmpTest.setBeanShellExpression(test.getExpression());118 tmpTest.setXmlClasses(test.getXmlClasses());119 tmpTest.setExcludedGroups(test.getExcludedGroups());120 tmpTest.setIncludedGroups(test.getIncludedGroups());121 tmpTest.setJUnit(test.isJUnit());122 tmpTest.setMethodSelectors(test.getMethodSelectors());123 tmpTest.setName(test.getName());124 tmpTest.setParallel(test.getParallel());125 tmpTest.setParameters(test.getLocalParameters());126 tmpTest.setVerbose(test.getVerbose());127 tmpTest.setXmlClasses(test.getXmlClasses());128 tmpTest.setXmlPackages(test.getXmlPackages());129
...
getObjectFactory
Using AI Code Generation
1import org.testng.SuiteRunner2import org.testng.internal.DynamicGraph3import org.testng.internal.DynamicGraph$Node4def suiteRunnerObjectFactory = suiteRunner.getObjectFactory()5def dynamicGraph = suiteRunnerObjectFactory.newInstance(DynamicGraph.class)6def node1 = suiteRunnerObjectFactory.newInstance(DynamicGraph$Node.class, "node1")7def node2 = suiteRunnerObjectFactory.newInstance(DynamicGraph$Node.class, "node2")8dynamicGraph.addEdge(node1, node2)
getObjectFactory
Using AI Code Generation
1[INFO] [TestNG] [TestNG] .getConfigFailurePolicy()=continue2[INFO] [TestNG] [TestNG] .getParallel()=NONE3[INFO] [TestNG] [TestNG] .getSuiteThreadPoolSize()=04[INFO] [TestNG] [TestNG] .getThreadCount()=05[INFO] [TestNG] [TestNG] .getVerbose()=06[INFO] [TestNG] [TestNG] .isAllowReturnValues()=false7[INFO] [TestNG] [TestNG] .isSkipFailedInvocationCounts()=false8[INFO] [TestNG] [TestNG] .isJUnit()=false9[INFO] [TestNG] [TestNG] .isPreserveOrder()=false10[INFO] [TestNG] [TestNG] .isUseDefaultListeners()=false11[INFO] [TestNG] [TestNG] .isXmlTestSuite()=false12[INFO] [TestNG] [TestNG] .getObjectFactory()=org.testng.internal.DefaultObjectFactory@1a1b2c3d13[INFO] [TestNG] [TestNG] .getOutputDirectory()=null14[INFO] [TestNG] [TestNG] .getSuiteFiles()=[/home/runner/work/testng/testng/testng-core/target/surefire/surefirebooter1197936069551000731.jar]15[INFO] [TestNG] [TestNG] .getSuiteXmlFiles()=[]16[INFO] [TestNG] [TestNG] .getTestClasses()=[]17[INFO] [TestNG] [TestNG] .getTestJar()=null18[INFO] [TestNG] [TestNG] .getTestSuites()=[org.testng.SuiteRunner@4e5f6g7h]19[INFO] [TestNG] [TestNG] .getXmlPathInJar()=null20[INFO] [TestNG] [TestNG] .getOutputDirectory()
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!!