How to use getInstance method of org.testng.Interface ITestNGMethod class

Best Testng code snippet using org.testng.Interface ITestNGMethod.getInstance

Source:Invoker.java Github

copy

Full Screen

...156 System.currentTimeMillis(),157 m_testContext);158 IConfigurationAnnotation configurationAnnotation= null;159 try {160 Object inst = tm.getInstance();161 if (inst == null) {162 inst = instance;163 }164 Class<?> objectClass= inst.getClass();165 Method method= tm.getMethod();166 // Only run the configuration if167 // - the test is enabled and168 // - the Configuration method belongs to the same class or a parent169 configurationAnnotation = AnnotationHelper.findConfiguration(m_annotationFinder, method);170 boolean alwaysRun= isAlwaysRun(configurationAnnotation);171 if(MethodHelper.isEnabled(objectClass, m_annotationFinder) || alwaysRun) {172 if (MethodHelper.isEnabled(configurationAnnotation)) {173 if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && !alwaysRun) {174 handleConfigurationSkip(tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);175 continue;176 }177 log(3, "Invoking " + Utils.detailedMethodName(tm, true));178 Object[] parameters = Parameters.createConfigurationParameters(tm.getMethod(),179 params,180 parameterValues,181 currentTestMethod,182 m_annotationFinder,183 suite,184 m_testContext,185 testMethodResult);186 testResult.setParameters(parameters);187 Object newInstance = null != instance ? instance: inst;188 runConfigurationListeners(testResult, true /* before */);189 invokeConfigurationMethod(newInstance, tm,190 parameters, testResult);191 // TODO: probably we should trigger the event for each instance???192 testResult.setEndMillis(System.currentTimeMillis());193 runConfigurationListeners(testResult, false /* after */);194 }195 else {196 log(3,197 "Skipping "198 + Utils.detailedMethodName(tm, true)199 + " because it is not enabled");200 }201 } // if is enabled202 else {203 log(3,204 "Skipping "205 + Utils.detailedMethodName(tm, true)206 + " because "207 + objectClass.getName()208 + " is not enabled");209 }210 }211 catch(InvocationTargetException ex) {212 handleConfigurationFailure(ex, tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);213 } catch(Throwable ex) { // covers the non-wrapper exceptions214 handleConfigurationFailure(ex, tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);215 }216 } // for methods217 }218 /**219 * Marks the current <code>TestResult</code> as skipped and invokes the listeners.220 */221 private void handleConfigurationSkip(ITestNGMethod tm,222 ITestResult testResult,223 IConfigurationAnnotation annotation,224 ITestNGMethod currentTestMethod,225 Object instance,226 XmlSuite suite) {227 recordConfigurationInvocationFailed(tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);228 testResult.setStatus(ITestResult.SKIP);229 runConfigurationListeners(testResult, false /* after */);230 }231 /**232 * Is the <code>IConfiguration</code> marked as alwaysRun.233 */234 private boolean isAlwaysRun(IConfigurationAnnotation configurationAnnotation) {235 if(null == configurationAnnotation) {236 return false;237 }238 boolean alwaysRun= false;239 if ((configurationAnnotation.getAfterSuite()240 || configurationAnnotation.getAfterTest()241 || configurationAnnotation.getAfterTestClass()242 || configurationAnnotation.getAfterTestMethod()243 || configurationAnnotation.getBeforeTestMethod()244 || configurationAnnotation.getBeforeTestClass()245 || configurationAnnotation.getBeforeTest()246 || configurationAnnotation.getBeforeSuite())247 && configurationAnnotation.getAlwaysRun())248 {249 alwaysRun= true;250 }251 return alwaysRun;252 }253 private void handleConfigurationFailure(Throwable ite,254 ITestNGMethod tm,255 ITestResult testResult,256 IConfigurationAnnotation annotation,257 ITestNGMethod currentTestMethod,258 Object instance,259 XmlSuite suite)260 {261 Throwable cause= ite.getCause() != null ? ite.getCause() : ite;262 if(isSkipExceptionAndSkip(cause)) {263 testResult.setThrowable(cause);264 handleConfigurationSkip(tm, testResult, annotation, currentTestMethod, instance, suite);265 return;266 }267 Utils.log("", 3, "Failed to invoke configuration method "268 + tm.getRealClass().getName() + "." + tm.getMethodName() + ":" + cause.getMessage());269 handleException(cause, tm, testResult, 1);270 runConfigurationListeners(testResult, false /* after */);271 //272 // If in TestNG mode, need to take a look at the annotation to figure out273 // what kind of @Configuration method we're dealing with274 //275 if (null != annotation) {276 recordConfigurationInvocationFailed(tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);277 }278 }279 /**280 * @return All the classes that belong to the same <test> tag as @param cls281 */282 private XmlClass[] findClassesInSameTest(Class<?> cls, XmlSuite suite) {283 Map<String, XmlClass> vResult= Maps.newHashMap();284 String className= cls.getName();285 for(XmlTest test : suite.getTests()) {286 for(XmlClass testClass : test.getXmlClasses()) {287 if(testClass.getName().equals(className)) {288 // Found it, add all the classes in this test in the result289 for(XmlClass thisClass : test.getXmlClasses()) {290 vResult.put(thisClass.getName(), thisClass);291 }292 // Note: we need to iterate through the entire suite since the same293 // class might appear in several <test> tags294 }295 }296 }297 XmlClass[] result= vResult.values().toArray(new XmlClass[vResult.size()]);298 return result;299 }300 /**301 * Record internally the failure of a Configuration, so that we can determine302 * later if @Test should be skipped.303 */304 private void recordConfigurationInvocationFailed(ITestNGMethod tm,305 IClass testClass,306 IConfigurationAnnotation annotation,307 ITestNGMethod currentTestMethod,308 Object instance,309 XmlSuite suite) {310 // If beforeTestClass or afterTestClass failed, mark either the config method's311 // entire class as failed, or the class under tests as failed, depending on312 // the configuration failure policy313 if (annotation.getBeforeTestClass() || annotation.getAfterTestClass()) {314 // tm is the configuration method, and currentTestMethod is null for BeforeClass315 // methods, so we need testClass316 if (m_continueOnFailedConfiguration) {317 setClassInvocationFailure(testClass.getRealClass(), instance);318 } else {319 setClassInvocationFailure(tm.getRealClass(), instance);320 }321 }322 // If before/afterTestMethod failed, mark either the config method's entire323 // class as failed, or just the current test method as failed, depending on324 // the configuration failure policy325 else if (annotation.getBeforeTestMethod() || annotation.getAfterTestMethod()) {326 if (m_continueOnFailedConfiguration) {327 setMethodInvocationFailure(currentTestMethod, instance);328 } else {329 setClassInvocationFailure(tm.getRealClass(), instance);330 }331 }332 // If beforeSuite or afterSuite failed, mark *all* the classes as failed333 // for configurations. At this point, the entire Suite is screwed334 else if (annotation.getBeforeSuite() || annotation.getAfterSuite()) {335 m_suiteState.failed();336 }337 // beforeTest or afterTest: mark all the classes in the same338 // <test> stanza as failed for configuration339 else if (annotation.getBeforeTest() || annotation.getAfterTest()) {340 setClassInvocationFailure(tm.getRealClass(), instance);341 XmlClass[] classes= findClassesInSameTest(tm.getRealClass(), suite);342 for(XmlClass xmlClass : classes) {343 setClassInvocationFailure(xmlClass.getSupportClass(), instance);344 }345 }346 String[] beforeGroups= annotation.getBeforeGroups();347 if(null != beforeGroups && beforeGroups.length > 0) {348 for(String group: beforeGroups) {349 m_beforegroupsFailures.put(group, Boolean.FALSE);350 }351 }352 }353 /**354 * @return true if this class or a parent class failed to initialize.355 */356 private boolean classConfigurationFailed(Class<?> cls) {357 for (Class<?> c : m_classInvocationResults.keySet()) {358 if (c == cls || c.isAssignableFrom(cls)) {359 return true;360 }361 }362 return false;363 }364 /**365 * @return true if this class has successfully run all its @Configuration366 * method or false if at least one of these methods failed.367 */368 private boolean confInvocationPassed(ITestNGMethod method, ITestNGMethod currentTestMethod,369 IClass testClass, Object instance) {370 boolean result= true;371 Class<?> cls = testClass.getRealClass();372 if(m_suiteState.isFailed()) {373 result= false;374 }375 else {376 if (classConfigurationFailed(cls)) {377 if (! m_continueOnFailedConfiguration) {378 result = !classConfigurationFailed(cls);379 } else {380 result = !m_classInvocationResults.get(cls).contains(instance);381 }382 }383 // if method is BeforeClass, currentTestMethod will be null384 else if (m_continueOnFailedConfiguration &&385 currentTestMethod != null &&386 m_methodInvocationResults.containsKey(currentTestMethod)) {387 result = !m_methodInvocationResults.get(currentTestMethod).contains(getMethodInvocationToken(currentTestMethod, instance));388 }389 else if (! m_continueOnFailedConfiguration) {390 for(Class<?> clazz: m_classInvocationResults.keySet()) {391// if (clazz == cls) {392 if(clazz.isAssignableFrom(cls)) {393 result= false;394 break;395 }396 }397 }398 }399 // check if there are failed @BeforeGroups400 String[] groups= method.getGroups();401 if(null != groups && groups.length > 0) {402 for(String group: groups) {403 if(m_beforegroupsFailures.containsKey(group)) {404 result= false;405 break;406 }407 }408 }409 return result;410 }411 // Creates a token for tracking a unique invocation of a method on an instance.412 // Is used when configFailurePolicy=continue.413 private Object getMethodInvocationToken(ITestNGMethod method, Object instance) {414 return String.format("%s+%d", instance.toString(), method.getCurrentInvocationCount());415 }416 /**417 * Effectively invokes a configuration method on all passed in instances.418 * TODO: Should change this method to be more like invokeMethod() so that we can419 * handle calls to {@code IInvokedMethodListener} better.420 *421 * @param targetInstance the instance to invoke the configuration method on422 * @param tm the configuration method423 * @param params the parameters needed for method invocation424 * @param testResult425 * @throws InvocationTargetException426 * @throws IllegalAccessException427 */428 private void invokeConfigurationMethod(Object targetInstance,429 ITestNGMethod tm,430 Object[] params,431 ITestResult testResult)432 throws InvocationTargetException, IllegalAccessException433 {434 // Mark this method with the current thread id435 tm.setId(ThreadUtil.currentThreadInfo());436 {437 InvokedMethod invokedMethod= new InvokedMethod(targetInstance,438 tm,439 params,440 System.currentTimeMillis(),441 testResult);442 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);443 m_notifier.addInvokedMethod(invokedMethod);444 try {445 Reporter.setCurrentTestResult(testResult);446 Method method = tm.getMethod();447 //448 // If this method is a IConfigurable, invoke its run() method449 //450 IConfigurable configurableInstance =451 IConfigurable.class.isAssignableFrom(tm.getMethod().getDeclaringClass()) ?452 (IConfigurable) targetInstance : m_configuration.getConfigurable();453 if (configurableInstance != null) {454 MethodInvocationHelper.invokeConfigurable(targetInstance, params, configurableInstance, method,455 testResult);456 }457 else {458 //459 // Not a IConfigurable, invoke directly460 //461 if (MethodHelper.calculateTimeOut(tm) <= 0) {462 MethodInvocationHelper.invokeMethod(method, targetInstance, params);463 }464 else {465 MethodInvocationHelper.invokeWithTimeout(tm, targetInstance, params, testResult);466 if (!testResult.isSuccess()) {467 // A time out happened468 throwConfigurationFailure(testResult, testResult.getThrowable());469 throw testResult.getThrowable();470 }471 }472 }473 }474 catch (InvocationTargetException | IllegalAccessException ex) {475 throwConfigurationFailure(testResult, ex);476 throw ex;477 } catch (Throwable ex) {478 throwConfigurationFailure(testResult, ex);479 throw new TestNGException(ex);480 }481 finally {482 Reporter.setCurrentTestResult(testResult);483 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);484 Reporter.setCurrentTestResult(null);485 }486 }487 }488 private void throwConfigurationFailure(ITestResult testResult, Throwable ex)489 {490 testResult.setStatus(ITestResult.FAILURE);;491 testResult.setThrowable(ex.getCause() == null ? ex : ex.getCause());492 }493 private void runInvokedMethodListeners(InvokedMethodListenerMethod listenerMethod, IInvokedMethod invokedMethod,494 ITestResult testResult)495 {496 if ( noListenersPresent() ) {497 return;498 }499 InvokedMethodListenerInvoker invoker = new InvokedMethodListenerInvoker(listenerMethod, testResult, m_testContext);500 for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {501 invoker.invokeListener(currentListener, invokedMethod);502 }503 }504 private boolean noListenersPresent() {505 return (m_invokedMethodListeners == null) || (m_invokedMethodListeners.size() == 0);506 }507 // pass both paramValues and paramIndex to be thread safe in case parallel=true + dataprovider.508 private ITestResult invokeMethod(Object instance,509 final ITestNGMethod tm,510 Object[] parameterValues,511 int parametersIndex,512 XmlSuite suite,513 Map<String, String> params,514 ITestClass testClass,515 ITestNGMethod[] beforeMethods,516 ITestNGMethod[] afterMethods,517 ConfigurationGroupMethods groupMethods,518 FailureContext failureContext) {519 TestResult testResult = new TestResult();520 //521 // Invoke beforeGroups configurations522 //523 invokeBeforeGroupsConfigurations(testClass, tm, groupMethods, suite, params,524 instance);525 //526 // Invoke beforeMethods only if527 // - firstTimeOnly is not set528 // - firstTimeOnly is set, and we are reaching at the first invocationCount529 //530 invokeConfigurations(testClass, tm,531 filterConfigurationMethods(tm, beforeMethods, true /* beforeMethods */),532 suite, params, parameterValues,533 instance, testResult);534 //535 // Create the ExtraOutput for this method536 //537 InvokedMethod invokedMethod = null;538 try {539 testResult.init(testClass, instance,540 tm,541 null,542 System.currentTimeMillis(),543 0,544 m_testContext);545 testResult.setParameters(parameterValues);546 testResult.setHost(m_testContext.getHost());547 testResult.setStatus(ITestResult.STARTED);548 invokedMethod= new InvokedMethod(instance,549 tm,550 parameterValues,551 System.currentTimeMillis(),552 testResult);553 // Fix from ansgarkonermann554 // invokedMethod is used in the finally, which can be invoked if555 // any of the test listeners throws an exception, therefore,556 // invokedMethod must have a value before we get here557 runTestListeners(testResult);558 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);559 m_notifier.addInvokedMethod(invokedMethod);560 Method thisMethod = tm.getConstructorOrMethod().getMethod();561 if(confInvocationPassed(tm, tm, testClass, instance)) {562 log(3, "Invoking " + tm.getRealClass().getName() + "." + tm.getMethodName());563 Reporter.setCurrentTestResult(testResult);564 // If this method is a IHookable, invoke its run() method565 IHookable hookableInstance =566 IHookable.class.isAssignableFrom(tm.getRealClass()) ?567 (IHookable) instance : m_configuration.getHookable();568 if (MethodHelper.calculateTimeOut(tm) <= 0) {569 if (hookableInstance != null) {570 MethodInvocationHelper.invokeHookable(instance,571 parameterValues, hookableInstance, thisMethod, testResult);572 } else {573 // Not a IHookable, invoke directly574 MethodInvocationHelper.invokeMethod(thisMethod, instance,575 parameterValues);576 }577 testResult.setStatus(ITestResult.SUCCESS);578 } else {579 // Method with a timeout580 MethodInvocationHelper.invokeWithTimeout(tm, instance, parameterValues, testResult, hookableInstance);581 }582 }583 else {584 testResult.setStatus(ITestResult.SKIP);585 }586 }587 catch(InvocationTargetException ite) {588 testResult.setThrowable(ite.getCause());589 testResult.setStatus(ITestResult.FAILURE);590 }591 catch(ThreadExecutionException tee) { // wrapper for TestNGRuntimeException592 Throwable cause= tee.getCause();593 if(TestNGRuntimeException.class.equals(cause.getClass())) {594 testResult.setThrowable(cause.getCause());595 }596 else {597 testResult.setThrowable(cause);598 }599 testResult.setStatus(ITestResult.FAILURE);600 }601 catch(Throwable thr) { // covers the non-wrapper exceptions602 testResult.setThrowable(thr);603 testResult.setStatus(ITestResult.FAILURE);604 }605 finally {606 // Set end time ASAP607 testResult.setEndMillis(System.currentTimeMillis());608 ExpectedExceptionsHolder expectedExceptionClasses609 = new ExpectedExceptionsHolder(m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));610 List<ITestResult> results = Lists.<ITestResult>newArrayList(testResult);611 handleInvocationResults(tm, results, expectedExceptionClasses, failureContext);612 // If this method has a data provider and just failed, memorize the number613 // at which it failed.614 // Note: we're not exactly testing that this method has a data provider, just615 // that it has parameters, so might have to revisit this if bugs get reported616 // for the case where this method has parameters that don't come from a data617 // provider618 if (testResult.getThrowable() != null && parameterValues.length > 0) {619 tm.addFailedInvocationNumber(parametersIndex);620 }621 //622 // Increment the invocation count for this method623 //624 tm.incrementCurrentInvocationCount();625 // Run invokedMethodListeners after updating TestResult626 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);627 runTestListeners(testResult);628 //629 // Invoke afterMethods only if630 // - lastTimeOnly is not set631 // - lastTimeOnly is set, and we are reaching the last invocationCount632 //633 invokeConfigurations(testClass, tm,634 filterConfigurationMethods(tm, afterMethods, false /* beforeMethods */),635 suite, params, parameterValues,636 instance,637 testResult);638 //639 // Invoke afterGroups configurations640 //641 invokeAfterGroupsConfigurations(testClass, tm, groupMethods, suite,642 params, instance);643 // Reset the test result last. If we do this too early, Reporter.log()644 // invocations from listeners will be discarded645 Reporter.setCurrentTestResult(null);646 }647 return testResult;648 }649 void collectResults(ITestNGMethod testMethod, Collection<ITestResult> results) {650 for (ITestResult result : results) {651 // Collect the results652 final int status = result.getStatus();653 if(ITestResult.SUCCESS == status) {654 m_notifier.addPassedTest(testMethod, result);655 }656 else if(ITestResult.SKIP == status) {657 m_notifier.addSkippedTest(testMethod, result);658 }659 else if(ITestResult.FAILURE == status) {660 m_notifier.addFailedTest(testMethod, result);661 }662 else if(ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {663 m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, result);664 }665 else {666 assert false : "UNKNOWN STATUS:" + status;667 }668 }669 }670 /**671 * The array of methods contains @BeforeMethods if isBefore if true, @AfterMethods672 * otherwise. This function removes all the methods that should not be run at this673 * point because they are either firstTimeOnly or lastTimeOnly and we haven't reached674 * the current invocationCount yet675 */676 private ITestNGMethod[] filterConfigurationMethods(ITestNGMethod tm,677 ITestNGMethod[] methods, boolean isBefore)678 {679 List<ITestNGMethod> result = Lists.newArrayList();680 for (ITestNGMethod m : methods) {681 ConfigurationMethod cm = (ConfigurationMethod) m;682 if (isBefore) {683 if (! cm.isFirstTimeOnly() ||684 (cm.isFirstTimeOnly() && tm.getCurrentInvocationCount() == 0))685 {686 result.add(m);687 }688 }689 else {690 int current = tm.getCurrentInvocationCount();691 boolean isLast = false;692 // If we have parameters, set the boolean if we are about to run693 // the last invocation694 if (tm.getParameterInvocationCount() > 0) {695 isLast = current == tm.getParameterInvocationCount() * tm.getTotalInvocationCount();696 }697 // If we have invocationCount > 1, set the boolean if we are about to698 // run the last invocation699 else if (tm.getTotalInvocationCount() > 1) {700 isLast = current == tm.getTotalInvocationCount();701 }702 if (! cm.isLastTimeOnly() || (cm.isLastTimeOnly() && isLast)) {703 result.add(m);704 }705 }706 }707 return result.toArray(new ITestNGMethod[result.size()]);708 }709 /**710 * invokeTestMethods() eventually converge here to invoke a single @Test method.711 * <p/>712 * This method is responsible for actually invoking the method. It decides if the invocation713 * must be done:714 * <ul>715 * <li>through an <code>IHookable</code></li>716 * <li>directly (through reflection)</li>717 * <li>in a separate thread (in case it needs to timeout)718 * </ul>719 *720 * <p/>721 * This method is also responsible for invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup722 * if it is the case for the passed in @Test method.723 */724 protected ITestResult invokeTestMethod(Object instance,725 final ITestNGMethod tm,726 Object[] parameterValues,727 int parametersIndex,728 XmlSuite suite,729 Map<String, String> params,730 ITestClass testClass,731 ITestNGMethod[] beforeMethods,732 ITestNGMethod[] afterMethods,733 ConfigurationGroupMethods groupMethods,734 FailureContext failureContext)735 {736 // Mark this method with the current thread id737 tm.setId(ThreadUtil.currentThreadInfo());738 ITestResult result = invokeMethod(instance, tm, parameterValues, parametersIndex, suite, params,739 testClass, beforeMethods, afterMethods, groupMethods,740 failureContext);741 return result;742 }743 /**744 * Filter all the beforeGroups methods and invoke only those that apply745 * to the current test method746 */747 private void invokeBeforeGroupsConfigurations(ITestClass testClass,748 ITestNGMethod currentTestMethod,749 ConfigurationGroupMethods groupMethods,750 XmlSuite suite,751 Map<String, String> params,752 Object instance)753 {754 synchronized(groupMethods) {755 List<ITestNGMethod> filteredMethods = Lists.newArrayList();756 String[] groups = currentTestMethod.getGroups();757 Map<String, List<ITestNGMethod>> beforeGroupMap = groupMethods.getBeforeGroupsMap();758 for (String group : groups) {759 List<ITestNGMethod> methods = beforeGroupMap.get(group);760 if (methods != null) {761 filteredMethods.addAll(methods);762 }763 }764 ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[filteredMethods.size()]);765 //766 // Invoke the right groups methods767 //768 if(beforeMethodsArray.length > 0) {769 // don't pass the IClass or the instance as the method may be external770 // the invocation must be similar to @BeforeTest/@BeforeSuite771 invokeConfigurations(null, beforeMethodsArray, suite, params,772 null, /* no parameter values */773 null);774 }775 //776 // Remove them so they don't get run again777 //778 groupMethods.removeBeforeGroups(groups);779 }780 }781 private void invokeAfterGroupsConfigurations(ITestClass testClass,782 ITestNGMethod currentTestMethod,783 ConfigurationGroupMethods groupMethods,784 XmlSuite suite,785 Map<String, String> params,786 Object instance)787 {788 // Skip this if the current method doesn't belong to any group789 // (only a method that belongs to a group can trigger the invocation790 // of afterGroups methods)791 if (currentTestMethod.getGroups().length == 0) {792 return;793 }794 // See if the currentMethod is the last method in any of the groups795 // it belongs to796 Map<String, String> filteredGroups = Maps.newHashMap();797 String[] groups = currentTestMethod.getGroups();798 synchronized(groupMethods) {799 for (String group : groups) {800 if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {801 filteredGroups.put(group, group);802 }803 }804 if(filteredGroups.isEmpty()) {805 return;806 }807 // The list of afterMethods to run808 Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();809 // Now filteredGroups contains all the groups for which we need to run the afterGroups810 // method. Find all the methods that correspond to these groups and invoke them.811 Map<String, List<ITestNGMethod>> map = groupMethods.getAfterGroupsMap();812 for (String g : filteredGroups.values()) {813 List<ITestNGMethod> methods = map.get(g);814 // Note: should put them in a map if we want to make sure the same afterGroups815 // doesn't get run twice816 if (methods != null) {817 for (ITestNGMethod m : methods) {818 afterMethods.put(m, m);819 }820 }821 }822 // Got our afterMethods, invoke them823 ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[afterMethods.size()]);824 // don't pass the IClass or the instance as the method may be external825 // the invocation must be similar to @BeforeTest/@BeforeSuite826 invokeConfigurations(null, afterMethodsArray, suite, params,827 null, /* no parameter values */828 null);829 // Remove the groups so they don't get run again830 groupMethods.removeAfterGroups(filteredGroups.keySet());831 }832 }833 private Object[] getParametersFromIndex(Iterator<Object[]> parametersValues, int index) {834 while (parametersValues.hasNext()) {835 Object[] parameters = parametersValues.next();836 if (index == 0) {837 return parameters;838 }839 index--;840 }841 return null;842 }843 int retryFailed(Object instance,844 final ITestNGMethod tm,845 XmlSuite suite,846 ITestClass testClass,847 ITestNGMethod[] beforeMethods,848 ITestNGMethod[] afterMethods,849 ConfigurationGroupMethods groupMethods,850 List<ITestResult> result,851 int failureCount,852 ExpectedExceptionsHolder expectedExceptionHolder,853 ITestContext testContext,854 Map<String, String> parameters,855 int parametersIndex) {856 final FailureContext failure = new FailureContext();857 failure.count = failureCount;858 do {859 failure.instances = Lists.newArrayList ();860 Map<String, String> allParameters = Maps.newHashMap();861 /**862 * TODO: This recreates all the parameters every time when we only need863 * one specific set. Should optimize it by only recreating the set needed.864 */865 ParameterBag bag = createParameters(tm, parameters,866 allParameters, suite, testContext, null /* fedInstance */);867 Object[] parameterValues =868 getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);869 result.add(invokeMethod(instance, tm, parameterValues, parametersIndex, suite,870 allParameters, testClass, beforeMethods, afterMethods, groupMethods, failure));871 }872 while (!failure.instances.isEmpty());873 return failure.count;874 }875 private ParameterBag createParameters(ITestNGMethod testMethod,876 Map<String, String> parameters,877 Map<String, String> allParameterNames,878 XmlSuite suite,879 ITestContext testContext,880 Object fedInstance)881 {882 Object instance;883 if (fedInstance != null) {884 instance = fedInstance;885 }886 else {887 instance = testMethod.getInstance();888 }889 ParameterBag bag = handleParameters(testMethod,890 instance, allParameterNames, parameters, null, suite, testContext, fedInstance, null);891 return bag;892 }893 /**894 * Invoke all the test methods. Note the plural: the method passed in895 * parameter might be invoked several times if the test class it belongs896 * to has more than one instance (i.e., if an @Factory method has been897 * declared somewhere that returns several instances of this TestClass).898 * If no @Factory method was specified, testMethod will only be invoked899 * once.900 * <p/>901 * Note that this method also takes care of invoking the beforeTestMethod902 * and afterTestMethod, if any.903 *904 * Note (alex): this method can be refactored to use a SingleTestMethodWorker that905 * directly invokes906 * {@link #invokeTestMethod(Object, ITestNGMethod, Object[], int, XmlSuite, Map, ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods, FailureContext)}907 * and this would simplify the implementation (see how DataTestMethodWorker is used)908 */909 @Override910 public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,911 XmlSuite suite,912 Map<String, String> testParameters,913 ConfigurationGroupMethods groupMethods,914 Object instance,915 ITestContext testContext)916 {917 // Potential bug here if the test method was declared on a parent class918 assert null != testMethod.getTestClass()919 : "COULDN'T FIND TESTCLASS FOR " + testMethod.getRealClass();920 if (!MethodHelper.isEnabled(testMethod.getMethod(), m_annotationFinder)) {921 // return if the method is not enabled. No need to do any more calculations922 return Collections.emptyList();923 }924 // By the time this testMethod to be invoked,925 // all dependencies should be already run or we need to skip this method,926 // so invocation count should not affect dependencies check927 final String okToProceed = checkDependencies(testMethod, testContext.getAllTestMethods());928 if (okToProceed != null) {929 //930 // Not okToProceed. Test is being skipped931 //932 ITestResult result = registerSkippedTestResult(testMethod, null, System.currentTimeMillis(),933 new Throwable(okToProceed));934 m_notifier.addSkippedTest(testMethod, result);935 return Collections.singletonList(result);936 }937 final Map<String, String> parameters =938 testMethod.findMethodParameters(testContext.getCurrentXmlTest());939 // For invocationCount > 1 and threadPoolSize > 1 run this method in its own pool thread.940 if (testMethod.getInvocationCount() > 1 && testMethod.getThreadPoolSize() > 1) {941 return invokePooledTestMethods(testMethod, suite, parameters, groupMethods, testContext);942 }943 long timeOutInvocationCount = testMethod.getInvocationTimeOut();944 //FIXME: Is this correct?945 boolean onlyOne = testMethod.getThreadPoolSize() > 1 ||946 timeOutInvocationCount > 0;947 int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();948 ExpectedExceptionsHolder expectedExceptionHolder =949 new ExpectedExceptionsHolder(m_annotationFinder, testMethod,950 new RegexpExpectedExceptionsHolder(m_annotationFinder, testMethod));951 final ITestClass testClass= testMethod.getTestClass();952 final List<ITestResult> result = Lists.newArrayList();953 final FailureContext failure = new FailureContext();954 final ITestNGMethod[] beforeMethods = filterMethods(testClass, testClass.getBeforeTestMethods(), CAN_RUN_FROM_CLASS);955 final ITestNGMethod[] afterMethods = filterMethods(testClass, testClass.getAfterTestMethods(), CAN_RUN_FROM_CLASS);956 while(invocationCount-- > 0) {957 if(false) {958 // Prevent code formatting959 }960 //961 // No threads, regular invocation962 //963 else {964 // Used in catch statement965 long start = System.currentTimeMillis();966 Map<String, String> allParameterNames = Maps.newHashMap();967 ParameterBag bag = createParameters(testMethod,968 parameters, allParameterNames, suite, testContext, instance);969 if (bag.hasErrors()) {970 final ITestResult tr = bag.errorResult;971 tr.setStatus(ITestResult.SKIP);972 runTestListeners(tr);973 m_notifier.addSkippedTest(testMethod, tr);974 result.add(tr);975 continue;976 }977 Iterator<Object[]> allParameterValues = bag.parameterHolder.parameters;978 int parametersIndex = 0;979 try {980 List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();981 if (bag.parameterHolder.origin == ParameterOrigin.ORIGIN_DATA_PROVIDER &&982 bag.parameterHolder.dataProviderHolder.annotation.isParallel()) {983 while (allParameterValues.hasNext()) {984 Object[] parameterValues = injectParameters(allParameterValues.next(),985 testMethod.getMethod(), testContext, null /* test result */);986 TestMethodWithDataProviderMethodWorker w =987 new TestMethodWithDataProviderMethodWorker(this,988 testMethod, parametersIndex,989 parameterValues, instance, suite, parameters, testClass,990 beforeMethods, afterMethods, groupMethods,991 expectedExceptionHolder, testContext, m_skipFailedInvocationCounts,992 invocationCount, failure.count, m_notifier);993 workers.add(w);994 // testng387: increment the param index in the bag.995 parametersIndex++;996 }997 PoolService<List<ITestResult>> ps =998 new PoolService<>(suite.getDataProviderThreadCount());999 List<List<ITestResult>> r = ps.submitTasksAndWait(workers);1000 for (List<ITestResult> l2 : r) {1001 result.addAll(l2);1002 }1003 } else {1004 while (allParameterValues.hasNext()) {1005 Object[] parameterValues = injectParameters(allParameterValues.next(),1006 testMethod.getMethod(), testContext, null /* test result */);1007 List<ITestResult> tmpResults = Lists.newArrayList();1008 try {1009 tmpResults.add(invokeTestMethod(instance,1010 testMethod,1011 parameterValues,1012 parametersIndex,1013 suite,1014 parameters,1015 testClass,1016 beforeMethods,1017 afterMethods,1018 groupMethods, failure));1019 }1020 finally {1021 if (failure.instances.isEmpty()) {1022 result.addAll(tmpResults);1023 } else {1024 for (Object failedInstance : failure.instances) {1025 List<ITestResult> retryResults = Lists.newArrayList();1026 failure.count = retryFailed(1027 failedInstance, testMethod, suite, testClass, beforeMethods,1028 afterMethods, groupMethods, retryResults,1029 failure.count, expectedExceptionHolder,1030 testContext, parameters, parametersIndex);1031 result.addAll(retryResults);1032 }1033 }1034 //1035 // If we have a failure, skip all the1036 // other invocationCounts1037 //1038 if (failure.count > 01039 && (m_skipFailedInvocationCounts1040 || testMethod.skipFailedInvocations())) {1041 while (invocationCount-- > 0) {1042 result.add(registerSkippedTestResult(testMethod, instance, System.currentTimeMillis(), null));1043 }1044 break;1045 }1046 }// end finally1047 parametersIndex++;1048 }1049 }1050 }1051 catch (Throwable cause) {1052 ITestResult r =1053 new TestResult(testMethod.getTestClass(),1054 instance,1055 testMethod,1056 cause,1057 start,1058 System.currentTimeMillis(),1059 m_testContext);1060 r.setStatus(TestResult.FAILURE);1061 result.add(r);1062 runTestListeners(r);1063 m_notifier.addFailedTest(testMethod, r);1064 } // catch1065 }1066 }1067 return result;1068 } // invokeTestMethod1069 private ITestResult registerSkippedTestResult(ITestNGMethod testMethod, Object instance,1070 long start, Throwable throwable) {1071 ITestResult result =1072 new TestResult(testMethod.getTestClass(),1073 instance,1074 testMethod,1075 throwable,1076 start,1077 System.currentTimeMillis(),1078 m_testContext);1079 result.setStatus(TestResult.SKIP);1080 runTestListeners(result);1081 return result;1082 }1083 /**1084 * Gets an array of parameter values returned by data provider or the ones that1085 * are injected based on parameter type. The method also checks for {@code NoInjection}1086 * annotation1087 * @param parameterValues parameter values from a data provider1088 * @param method method to be invoked1089 * @param context test context1090 * @param testResult test result1091 */1092 private Object[] injectParameters(Object[] parameterValues, Method method,1093 ITestContext context, ITestResult testResult)1094 throws TestNGException {1095 List<Object> vResult = Lists.newArrayList();1096 int i = 0;1097 int numValues = parameterValues.length;1098 int numParams = method.getParameterTypes().length;1099 if (numValues > numParams && ! method.isVarArgs()) {1100 throw new TestNGException("The data provider is trying to pass " + numValues1101 + " parameters but the method "1102 + method.getDeclaringClass().getName() + "#" + method.getName()1103 + " takes " + numParams);1104 }1105 // beyond this, numValues <= numParams1106 for (Class<?> cls : method.getParameterTypes()) {1107 Annotation[] annotations = method.getParameterAnnotations()[i];1108 boolean noInjection = false;1109 for (Annotation a : annotations) {1110 if (a instanceof NoInjection) {1111 noInjection = true;1112 break;1113 }1114 }1115 Object injected = Parameters.getInjectedParameter(cls, method, context, testResult);1116 if (injected != null && ! noInjection) {1117 vResult.add(injected);1118 } else {1119 try {1120 if (method.isVarArgs()) vResult.add(parameterValues);1121 else vResult.add(parameterValues[i++]);1122 } catch (ArrayIndexOutOfBoundsException ex) {1123 throw new TestNGException("The data provider is trying to pass " + numValues1124 + " parameters but the method "1125 + method.getDeclaringClass().getName() + "#" + method.getName()1126 + " takes " + numParams1127 + " and TestNG is unable in inject a suitable object", ex);1128 }1129 }1130 }1131 return vResult.toArray(new Object[vResult.size()]);1132 }1133 private ParameterBag handleParameters(ITestNGMethod testMethod,1134 Object instance,1135 Map<String, String> allParameterNames,1136 Map<String, String> parameters,1137 Object[] parameterValues,1138 XmlSuite suite,1139 ITestContext testContext,1140 Object fedInstance,1141 ITestResult testResult)1142 {1143 try {1144 return new ParameterBag(1145 Parameters.handleParameters(testMethod,1146 allParameterNames,1147 instance,1148 new Parameters.MethodParameters(parameters,1149 testMethod.findMethodParameters(testContext.getCurrentXmlTest()),1150 parameterValues,1151 testMethod.getMethod(), testContext, testResult),1152 suite,1153 m_annotationFinder,1154 fedInstance));1155 }1156// catch(TestNGException ex) {1157// throw ex;1158// }1159 catch(Throwable cause) {1160 return new ParameterBag(1161 new TestResult(1162 testMethod.getTestClass(),1163 instance,1164 testMethod,1165 cause,1166 System.currentTimeMillis(),1167 System.currentTimeMillis(),1168 m_testContext));1169 }1170 }1171 /**1172 * Invokes a method that has a specified threadPoolSize.1173 */1174 private List<ITestResult> invokePooledTestMethods(ITestNGMethod testMethod,1175 XmlSuite suite,1176 Map<String, String> parameters,1177 ConfigurationGroupMethods groupMethods,1178 ITestContext testContext)1179 {1180 //1181 // Create the workers1182 //1183 List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();1184 // Create one worker per invocationCount1185 for (int i = 0; i < testMethod.getInvocationCount(); i++) {1186 // we use clones for reporting purposes1187 ITestNGMethod clonedMethod= testMethod.clone();1188 clonedMethod.setInvocationCount(1);1189 clonedMethod.setThreadPoolSize(1);1190 MethodInstance mi = new MethodInstance(clonedMethod);1191 workers.add(new SingleTestMethodWorker(this,1192 mi,1193 suite,1194 parameters,1195 testContext,1196 m_classListeners));1197 }1198 return runWorkers(testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite,1199 parameters);1200 }1201 static class FailureContext {1202 int count = 0;1203 List<Object> instances = Lists.newArrayList();1204 }1205 void handleInvocationResults(ITestNGMethod testMethod,1206 List<ITestResult> result,1207 ExpectedExceptionsHolder expectedExceptionsHolder,1208 FailureContext failure)1209 {1210 //1211 // Go through all the results and create a TestResult for each of them1212 //1213 List<ITestResult> resultsToRetry = Lists.newArrayList();1214 for (ITestResult testResult : result) {1215 Throwable ite= testResult.getThrowable();1216 int status= testResult.getStatus();1217 boolean handled = false;1218 // Exception thrown?1219 if (ite != null) {1220 // Invocation caused an exception, see if the method was annotated with @ExpectedException1221 if (expectedExceptionsHolder != null) {1222 if (expectedExceptionsHolder.isExpectedException(ite)) {1223 testResult.setStatus(ITestResult.SUCCESS);1224 status = ITestResult.SUCCESS;1225 } else {1226 if (isSkipExceptionAndSkip(ite)){1227 status = ITestResult.SKIP;1228 } else {1229 testResult.setThrowable(expectedExceptionsHolder.wrongException(ite));1230 status = ITestResult.FAILURE;1231 }1232 }1233 } else {1234 handleException(ite, testMethod, testResult, failure.count++);1235 handled = true;1236 status = testResult.getStatus();1237 }1238 }1239 // No exception thrown, make sure we weren't expecting one1240 else if(status != ITestResult.SKIP && expectedExceptionsHolder != null) {1241 TestException exception = expectedExceptionsHolder.noException(testMethod);1242 if (exception != null) {1243 testResult.setThrowable(exception);1244 status= ITestResult.FAILURE;1245 }1246 }1247 IRetryAnalyzer retryAnalyzer = testMethod.getRetryAnalyzer();1248 boolean willRetry = retryAnalyzer != null && status == ITestResult.FAILURE && failure.instances != null && retryAnalyzer.retry(testResult);1249 if (willRetry) {1250 resultsToRetry.add(testResult);1251 failure.count++;1252 failure.instances.add(testResult.getInstance());1253 testResult.setStatus(ITestResult.SKIP);1254 } else {1255 testResult.setStatus(status);1256 if (status == ITestResult.FAILURE && !handled) {1257 handleException(ite, testMethod, testResult, failure.count++);1258 }1259 }1260 collectResults(testMethod, Collections.singleton(testResult));1261 } // for results1262 removeResultsToRetryFromResult(resultsToRetry, result, failure);1263 }1264 private boolean isSkipExceptionAndSkip(Throwable ite) {1265 return SkipException.class.isAssignableFrom(ite.getClass()) && ((SkipException) ite).isSkip();1266 }1267 private void removeResultsToRetryFromResult(List<ITestResult> resultsToRetry,1268 List<ITestResult> result, FailureContext failure) {1269 if (resultsToRetry != null) {1270 for (ITestResult res : resultsToRetry) {1271 result.remove(res);1272 failure.count--;1273 }1274 }1275 }1276 /**1277 * To reduce thread contention and also to correctly handle thread-confinement1278 * this method invokes the @BeforeGroups and @AfterGroups corresponding to the current @Test method.1279 */1280 private List<ITestResult> runWorkers(ITestNGMethod testMethod,1281 List<IWorker<ITestNGMethod>> workers,1282 int threadPoolSize,1283 ConfigurationGroupMethods groupMethods,1284 XmlSuite suite,1285 Map<String, String> parameters)1286 {1287 // Invoke @BeforeGroups on the original method (reduce thread contention,1288 // and also solve thread confinement)1289 ITestClass testClass= testMethod.getTestClass();1290 Object[] instances = testClass.getInstances(true);1291 for(Object instance: instances) {1292 invokeBeforeGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1293 }1294 long maxTimeOut= -1; // 10 seconds1295 for(IWorker<ITestNGMethod> tmw : workers) {1296 long mt= tmw.getTimeOut();1297 if(mt > maxTimeOut) {1298 maxTimeOut= mt;1299 }1300 }1301 ThreadUtil.execute(workers, threadPoolSize, maxTimeOut, true);1302 //1303 // Collect all the TestResults1304 //1305 List<ITestResult> result = Lists.newArrayList();1306 for (IWorker<ITestNGMethod> tmw : workers) {1307 if (tmw instanceof TestMethodWorker) {1308 result.addAll(((TestMethodWorker)tmw).getTestResults());1309 }1310 }1311 for(Object instance: instances) {1312 invokeAfterGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1313 }1314 return result;1315 }1316 /**1317 * Checks to see of the test method has certain dependencies that prevents1318 * TestNG from executing it1319 * @param testMethod test method being checked for1320 * @return error message or null if dependencies have been run successfully1321 */1322 private String checkDependencies(ITestNGMethod testMethod,1323 ITestNGMethod[] allTestMethods)1324 {1325 // If this method is marked alwaysRun, no need to check for its dependencies1326 if (testMethod.isAlwaysRun()) {1327 return null;1328 }1329 // Any missing group?1330 if (testMethod.getMissingGroup() != null1331 && !testMethod.ignoreMissingDependencies()) {1332 return "Method " + testMethod + " depends on nonexistent group \"" + testMethod.getMissingGroup() + "\"";1333 }1334 // If this method depends on groups, collect all the methods that1335 // belong to these groups and make sure they have been run successfully1336 final String[] groups = testMethod.getGroupsDependedUpon();1337 if (null != groups && groups.length > 0) {1338 // Get all the methods that belong to the group depended upon1339 for (String element : groups) {1340 ITestNGMethod[] methods =1341 MethodGroupsHelper.findMethodsThatBelongToGroup(testMethod,1342 m_testContext.getAllTestMethods(),1343 element);1344 if (methods.length == 0 && !testMethod.ignoreMissingDependencies()) {1345 // Group is missing1346 return "Method " + testMethod + " depends on nonexistent group \"" + element + "\"";1347 }1348 if (!haveBeenRunSuccessfully(testMethod, methods)) {1349 return "Method " + testMethod +1350 " depends on not successfully finished methods in group \"" + element + "\"";1351 }1352 }1353 } // depends on groups1354 // If this method depends on other methods, make sure all these other1355 // methods have been run successfully1356 if (dependsOnMethods(testMethod)) {1357 ITestNGMethod[] methods =1358 MethodHelper.findDependedUponMethods(testMethod, allTestMethods);1359 if (!haveBeenRunSuccessfully(testMethod, methods)) {1360 return "Method " + testMethod + " depends on not successfully finished methods";1361 }1362 }1363 return null;1364 }1365 /**1366 * @return the test results that apply to one of the instances of the testMethod.1367 */1368 private Set<ITestResult> keepSameInstances(ITestNGMethod method, Set<ITestResult> results) {1369 Set<ITestResult> result = Sets.newHashSet();1370 for (ITestResult r : results) {1371 final Object o = method.getInstance();1372 // Keep this instance if 1) It's on a different class or 2) It's on the same class1373 // and on the same instance1374 Object instance = r.getInstance() != null1375 ? r.getInstance() : r.getMethod().getInstance();1376 if (r.getTestClass() != method.getTestClass() || instance == o) result.add(r);1377 }1378 return result;1379 }1380 /**1381 * @return true if all the methods have been run successfully1382 */1383 private boolean haveBeenRunSuccessfully(ITestNGMethod testMethod, ITestNGMethod[] methods) {1384 // Make sure the method has been run successfully1385 for (ITestNGMethod method : methods) {1386 Set<ITestResult> results = keepSameInstances(testMethod, m_notifier.getPassedTests(method));1387 Set<ITestResult> failedAndSkippedMethods = Sets.newHashSet();1388 failedAndSkippedMethods.addAll(m_notifier.getFailedTests(method));1389 failedAndSkippedMethods.addAll(m_notifier.getSkippedTests(method));1390 Set<ITestResult> failedresults = keepSameInstances(testMethod, failedAndSkippedMethods);1391 // If failed results were returned on the same instance, then these tests didn't pass1392 if (failedresults != null && failedresults.size() > 0) {1393 return false;1394 }1395 for (ITestResult result : results) {1396 if(!result.isSuccess()) {1397 return false;1398 }1399 }1400 }1401 return true;1402 }1403// private boolean containsInstance(Set<ITestResult> failedresults, Object[] instances) {1404// for (ITestResult tr : failedresults) {1405// for (Object o : instances) {1406// if (o == tr.getInstance()) {1407// return true;1408// }1409// }1410// }1411// return false;1412// }1413 /**1414 * An exception was thrown by the test, determine if this method1415 * should be marked as a failure or as failure_but_within_successPercentage1416 */1417 private void handleException(Throwable throwable,1418 ITestNGMethod testMethod,1419 ITestResult testResult,1420 int failureCount) {...

Full Screen

Full Screen

Source:MyReporterListener.java Github

copy

Full Screen

...40public class MyReporterListener extends MyReporterListenerAdapter {41 DataUtil util = null;42 Map<String, Object> details;43 public MyReporterListener() {44 util = DataUtil.getInstance();45 details = util.getDetails();46 }47 // ~ Instance fields ------------------------------------------------------48 private PrintWriter m_out;49 private int m_row;50 private Integer m_testIndex;51 private int m_methodIndex;52 private Scanner scanner;53 private int pass;54 private int fail;55 private int skip;56 private int total;57 private String hostaddress;58 @SuppressWarnings("rawtypes")...

Full Screen

Full Screen

Source:RulesListener.java Github

copy

Full Screen

...16 public void apply(T arg);17 }18 @Override19 public void run(IHookCallBack callBack, ITestResult testResult) {20 List<IHookable> hookables = getRules(testResult.getInstance(),21 IHookable.class);22 if (hookables.isEmpty()) {23 callBack.runTestMethod(testResult);24 } else {25 IHookable hookable = hookables.get(0);26 hookables.remove(0);27 for (IHookable iHookable : hookables) {28 hookable = compose(hookable, iHookable);29 }30 hookable.run(callBack, testResult);31 }32 }33 private IHookable compose(final IHookable first, final IHookable second) {34 return new IHookable() {35 @Override36 public void run(final IHookCallBack callBack,37 final ITestResult testResult) {38 first.run(new IHookCallBack() {39 @Override40 public void runTestMethod(ITestResult testResult) {41 second.run(callBack, testResult);42 }43 @Override44 public Object[] getParameters() {45 return callBack.getParameters();46 }47 }, testResult);48 }49 };50 }51 private <T> List<T> getRules(Object object, Class<T> type) {52 List<T> rules = new ArrayList<T>();53 Field[] declaredFields = object.getClass().getFields();54 for (Field field : declaredFields) {55 NGRule annotation = field.getAnnotation(NGRule.class);56 if (annotation != null) {57 try {58 Object fieldContent = field.get(object);59 if (type.isAssignableFrom(field.getType())) {60 @SuppressWarnings("unchecked")61 T rule = (T) fieldContent;62 rules.add(rule);63 }64 } catch (Exception e) {65 e.printStackTrace();66 }67 }68 }69 return rules;70 }71 @Override72 public void onTestStart(final ITestResult result) {73 executeRulesForInstance(new Function0<ITestListener>() {74 @Override75 public void apply(ITestListener listener) {76 listener.onTestStart(result);77 }78 }, result.getInstance());79 }80 @Override81 public void onTestSuccess(final ITestResult result) {82 executeRulesForInstance(new Function0<ITestListener>() {83 @Override84 public void apply(ITestListener listener) {85 listener.onTestSuccess(result);86 }87 }, result.getInstance());88 }89 @Override90 public void onTestFailure(final ITestResult result) {91 executeRulesForInstance(new Function0<ITestListener>() {92 @Override93 public void apply(ITestListener listener) {94 listener.onTestFailure(result);95 }96 }, result.getInstance());97 }98 @Override99 public void onTestSkipped(final ITestResult result) {100 executeRulesForInstance(new Function0<ITestListener>() {101 @Override102 public void apply(ITestListener listener) {103 listener.onTestSkipped(result);104 }105 }, result.getInstance());106 }107 @Override108 public void onTestFailedButWithinSuccessPercentage(final ITestResult result) {109 executeRulesForInstance(new Function0<ITestListener>() {110 @Override111 public void apply(ITestListener listener) {112 listener.onTestFailedButWithinSuccessPercentage(result);113 }114 }, result.getInstance());115 }116 @Override117 public void onStart(final ITestContext context) {118 executeRulesForContext(context,119 new Function0<ITestListener>() {120 @Override121 public void apply(ITestListener listener) {122 listener.onStart(context);123 }124 });125 }126 @Override127 public void onFinish(final ITestContext context) {128 executeRulesForContext(context, new Function0<ITestListener>() {129 @Override130 public void apply(ITestListener listener) {131 listener.onFinish(context);132 }133 });134 }135 private void executeRulesForContext(ITestContext context,136 Function0<ITestListener> action) {137 ITestNGMethod[] allTestMethods = context.getAllTestMethods();138 Set<Object> testInstances = new HashSet<Object>();139 for (ITestNGMethod iTestNGMethod : allTestMethods) {140 testInstances.addAll(Arrays.asList(iTestNGMethod.getInstances()));141 }142 for (Object instance : testInstances) {143 executeRulesForInstance(action, instance);144 }145 }146 private void executeRulesForInstance(Function0<ITestListener> action,147 Object allInst) {148 List<ITestListener> hookables = getRules(allInst, ITestListener.class);149 for (ITestListener listener : hookables) {150 action.apply(listener);151 }152 }153}...

Full Screen

Full Screen

Source:TestListener.java Github

copy

Full Screen

...88 */89 @Override90 public void onFinish(ITestContext context) {91 ExtentTestManager.endTest();92 ExtentManager.getInstance().flush();9394 /*try {95 SendMail.send(emailFromAddress, emailList, "Report", emailMsgTxt);96 } catch (MessagingException e) {97 // TODO Auto-generated catch block98 e.printStackTrace();99 }*/100 }101102 /**103 * On test start logging method name in report.104 *105 * @param result the result106 */107 @Override108 public void onTestStart(ITestResult result) {109 ExtentTestManager.startTest(result.getMethod().getMethodName());110 }111112 /**113 * Report log.114 *115 * @param message the message116 */117 public static void reportLog(String message) {118 ExtentTestManager.getTest().log(Status.INFO, message);119 }120121 /**122 * Gets the passcount.123 *124 * @return the passcount125 */126 public static int getPasscount() {127 return passedtests.size();128129 }130131 /**132 * Gets the failcount.133 *134 * @return the failcount135 */136 public static int getFailcount() {137 return failedtests.size();138139 }140141 /**142 * Gets the skipcount.143 *144 * @return the skipcount145 */146 public static int getSkipcount() {147 return skippedtests.size();148149 }150151 /**152 * Gets the driver from base test.153 *154 * @param result the result155 * @return the driver from base test156 * @throws IllegalAccessException the illegal access exception157 */158 @SuppressWarnings("unchecked")159 private WebDriver getDriverFromBaseTest(ITestResult result) throws IllegalAccessException {160 WebDriver driver = null;161162 try {163 Class<? extends ITestResult> testClass = (Class<? extends ITestResult>) result.getInstance().getClass();164165 Class<? extends ITestResult> baseTestClass = (Class<? extends ITestResult>) testClass.getSuperclass();166167 Field driverField = baseTestClass.getDeclaredField("driver");168 driver = (BrowserDriver) driverField.get(result.getInstance());169 return driver;170 } catch (SecurityException | NoSuchFieldException | IllegalArgumentException ex) {171 throw new HtmlElementsException("error getting the driver from base test");172 }173174 }175 176 /**177 * On test failed but within success percentage.178 *179 * @param result the result180 */181 @Override182 public void onTestFailedButWithinSuccessPercentage(ITestResult result) { ...

Full Screen

Full Screen

Source:ReportOrphanedExecutors.java Github

copy

Full Screen

...50 }51 private void reportOrphanedExecutors(ISuite suite)52 {53 Set<?> instances = suite.getAllMethods().stream()54 .map(ITestNGMethod::getInstance)55 .filter(Objects::nonNull)56 .collect(toImmutableSet());57 for (Object instance : instances) {58 reportOrphanedExecutorsOnInstance(instance);59 }60 }61 private void reportOrphanedExecutorsOnInstance(Object instance)62 {63 requireNonNull(instance, "instance is null");64 try {65 for (Class<?> currentClass = instance.getClass(); currentClass != null; currentClass = currentClass.getSuperclass()) {66 Field[] fields = currentClass.getDeclaredFields();67 for (Field field : fields) {68 if (field.isAnnotationPresent(Suppress.class)) {...

Full Screen

Full Screen

Source:ITestResult.java Github

copy

Full Screen

...69 public String getHost();70 /**71 * The instance on which this method was run.72 */73 public Object getInstance();74 /**75 * If this result's related instance implements ITest or use @Test(testName=...), returns its test name, otherwise returns null.76 */77 public String getTestName();78 79 public String getXmlTestName();80 public String getInstanceName();81 82 /**83 * @return the {@link ITestContext} for this test result.84 */85 public ITestContext getTestContext();86}...

Full Screen

Full Screen

Source:IStepResult.java Github

copy

Full Screen

...59 String getHost();60 /**61 * The instance on which this method was run.62 */63 Object getInstance();64 /**65 * If this result's related instance implements IStep or use @Step(testName=...), returns its Step name, otherwise returns null.66 */67 String getStepName();68 String getInstanceName();69 /**70 * @return the {@link IStepContext} for this step result.71 */72 IStepContext getStepContext();73}...

Full Screen

Full Screen

Source:IMethodInstance.java Github

copy

Full Screen

...5 */6public interface IMethodInstance {7 ITestNGMethod getMethod();8 /**9 * @deprecated Use getInstance()10 */11 Object[] getInstances();12 Object getInstance();13}...

Full Screen

Full Screen

getInstance

Using AI Code Generation

copy

Full Screen

1ITestNGMethod[] methods = TestNG.getDefault().getTest().getAllTestMethods();2for (ITestNGMethod method : methods) {3 String methodName = method.getMethodName();4 String className = method.getTestClass().getName();5 System.out.println("methodName = " + methodName);6 System.out.println("className = " + className);7}

Full Screen

Full Screen

getInstance

Using AI Code Generation

copy

Full Screen

1ITestNGMethod method = org.testng.internal.MethodHelper.getInstance().getTestMethod("testMethod", "testClass", "testPackage");2ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");3ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");4ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");5ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");6ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");7ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");8ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");9ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("testMethod", "testClass", "testPackage");10ITestNGMethod method = org.testng.internal.MethodHelper.getMethod("test

Full Screen

Full Screen

getInstance

Using AI Code Generation

copy

Full Screen

1Class<?> classObj = method.getRealClass();2Object instance = classObj.newInstance();3method.setInstances(instance);4Class<?> classObj = method.getRealClass();5Object instance = classObj.newInstance();6method.setInstances(instance);7TestClass testClass = method.getTestClass();8Class<?> classObj = testClass.getRealClass();9Object instance = classObj.newInstance();10method.setInstances(instance);11Method methodObj = method.getMethod();12Object instance = methodObj.getDeclaringClass().newInstance();13method.setInstances(instance);14Method methodObj = method.getMethodObject();15Object instance = methodObj.getDeclaringClass().newInstance();16method.setInstances(instance);

Full Screen

Full Screen

TestNG tutorial

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.

Chapters

  1. JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
  2. Installing TestNG in Eclipse: Start installing the TestNG Plugin and learn how to set up TestNG in Eclipse to begin constructing a framework for your test project.
  3. Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
  4. Automation using TestNG: Dive into how to install TestNG in this Selenium TestNG tutorial, the fundamentals of developing an automation script for Selenium automation testing.
  5. Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
  6. Creating TestNG XML File: Here is a step-by-step tutorial on creating a TestNG XML file to learn why and how it is created and discover how to run the TestNG XML file being executed in parallel.
  7. Automation with Selenium, Cucumber & TestNG: Explore for an in-depth tutorial on automation using Selenium, Cucumber, and TestNG, as TestNG offers simpler settings and more features.
  8. JUnit Selenium Tests using TestNG: Start running your regular and parallel tests by looking at how to run test cases in Selenium using JUnit and TestNG without having to rewrite the tests.
  9. Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
  10. Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
  11. Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
  12. DataProviders in TestNG: Deep dive into learning more about TestNG's DataProvider and how to effectively use it in our test scripts for Selenium test automation.
  13. Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
  14. TestNG Listeners in Selenium WebDriver: Understand the various TestNG listeners to utilize them effectively for your next plan when working with TestNG and Selenium automation.
  15. TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
  16. TestNG Reporter Log in Selenium: Find out how to use the TestNG Reporter Log and learn how to eliminate the need for external software with TestNG Reporter Class to boost productivity.
  17. TestNG Reports in Jenkins: Discover how to generate TestNG reports in Jenkins if you want to know how to create, install, and share TestNG reports in Jenkins.

Certification

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.

YouTube

Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful