Best Testng code snippet using org.testng.Interface IConfigurationListener
Source:CarinaListenerChain.java
1/*******************************************************************************2 * Copyright 2020-2022 Zebrunner Inc (https://www.zebrunner.com).3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *******************************************************************************/16package com.qaprosoft.carina.core.foundation.listeners;17import java.lang.invoke.MethodHandles;18import java.lang.reflect.Field;19import java.util.List;20import org.slf4j.Logger;21import org.slf4j.LoggerFactory;22import org.testng.IConfigurationListener;23import org.testng.ITestNGMethod;24import org.testng.ITestResult;25import com.google.common.collect.Lists;26import com.nordstrom.automation.testng.ListenerChain;27public class CarinaListenerChain extends ListenerChain {28 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());29 // The IConfigurationListener interface has two methods with the same meaning:30 // 1. void beforeConfiguration(ITestResult tr)31 // 2. void beforeConfiguration(ITestResult tr, ITestNGMethod tm)32 //33 // The only difference is that the version with two arguments also passes the actual test method34 // for which a configuration method is going to be invoked, but the first version don't.35 //36 // The Zebrunner agent expects the second version of the method to be invoked.37 // This is basically necessary to correlate a test method with corresponding to it @BeforeMethod and @AfterMethod methods.38 //39 // The problem is that Nordstrom's ListenerChain class doesn't decorate this method.40 // As a result, the Zebrunner agent misses invocations of @BeforeMethod and @AfterMethod methods.41 @Override42 public void beforeConfiguration(ITestResult tr, ITestNGMethod tm) {43 try {44 Field configListenersField = this.getClass().getSuperclass().getDeclaredField("configListeners");45 configListenersField.setAccessible(true);46 List<IConfigurationListener> listeners = (List<IConfigurationListener>) configListenersField.get(this);47 invokeListeners(tr, tm, listeners);48 } catch (NoSuchFieldException | IllegalAccessException e) {49 LOGGER.error("Could not invoke configuration listeners.", e);50 }51 }52 private void invokeListeners(ITestResult tr, ITestNGMethod tm, List<IConfigurationListener> listeners) {53 synchronized (listeners) {54 for (IConfigurationListener configListener : Lists.reverse(listeners)) {55 configListener.beforeConfiguration(tr, tm);56 }57 }58 }59}...
Source:TestNGConfigurationListener.java
1/*2 * Copyright 2011 the original author or authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.gradle.api.internal.tasks.testing.testng;17import org.testng.ITestResult;18/**19 * Our version of TestNG's IConfigurationListener. Can be adapted to20 * org.testng.internal.IConfigurationListener (from TestNG < 6.2) or21 * org.testng.IConfigurationListener2 (from TestNG >= 6.2). Became22 * necessary because TestNG 6.2 changed the package name of the former23 * interface from org.testng.internal to org.testng.24 * 25 * @see TestNGListenerAdapterFactory26 */27public interface TestNGConfigurationListener {28 /**29 * Invoked whenever a configuration method succeeded.30 */31 void onConfigurationSuccess(ITestResult itr);32 /**33 * Invoked whenever a configuration method failed.34 */35 void onConfigurationFailure(ITestResult itr);36 /**37 * Invoked whenever a configuration method was skipped.38 */39 void onConfigurationSkip(ITestResult itr);40 /**41 * Invoked before a configuration method is invoked. 42 * 43 * Note: This method is only invoked for TestNG 6.2 or higher.44 */45 void beforeConfiguration(ITestResult tr);46}...
Interface IConfigurationListener
Using AI Code Generation
1package org.testng;2public interface IConfigurationListener extends ITestListener {3 void onConfigurationSuccess(ITestResult itr);4 void onConfigurationFailure(ITestResult itr);5 void onConfigurationSkip(ITestResult itr);6}7package org.testng;8public interface IInvokedMethodListener extends ITestListener {9 void beforeInvocation(IInvokedMethod method, ITestResult testResult);10 void afterInvocation(IInvokedMethod method, ITestResult testResult);11}12package org.testng;13public interface IHookable {14 void run(IHookCallBack callBack, ITestResult testResult);15}16package org.testng;17public interface IHookCallBack {18 Object runTestMethod(ITestResult testResult);19}20package org.testng;21public interface ITestListener {22 void onTestStart(ITestResult result);23 void onTestSuccess(ITestResult result);24 void onTestFailure(ITestResult result);25 void onTestSkipped(ITestResult result);26 void onTestFailedButWithinSuccessPercentage(ITestResult result);27 void onStart(ITestContext context);28 void onFinish(ITestContext context);29}30package org.testng;31public interface ITestResult {32 int SUCCESS = 1;33 int FAILURE = 2;34 int SKIP = 3;35 int STARTED = 16;36 int SUCCESS_PERCENTAGE_FAILURE = 17;37 int SUCCESS_PERCENTAGE_FAILURE = 18;38 int getParameterIndex();39 Object getAttribute(String name);40 void setAttribute(String name, Object value);41 Object removeAttribute(String name);42 Set<String> getAttributeNames();43 boolean hasAttributes();44 Object getHost();45 void setHost(Object host);46 ITestNGMethod getMethod();47 Object[] getParameters();48 String getName();49 int getStatus();50 void setStatus(int status);51 long getStartMillis();52 void setStartMillis(long millis);53 long getEndMillis();54 void setEndMillis(long millis);55 IClass getTestClass();56 ITestContext getTestContext();57 Throwable getThrowable();58 void setThrowable(Throwable throwable);59 Object getInstance();60 void setInstance(Object
Interface IConfigurationListener
Using AI Code Generation
1package org.testng;2public interface IConfigurationListener extends ITestListener {3 public void onConfigurationSuccess(ITestResult itr);4 public void onConfigurationFailure(ITestResult itr);5 public void onConfigurationSkip(ITestResult itr);6}7package org.testng;8public interface ITestListener {9 public void onTestStart(ITestResult result);10 public void onTestSuccess(ITestResult result);11 public void onTestFailure(ITestResult result);12 public void onTestSkipped(ITestResult result);13 public void onTestFailedButWithinSuccessPercentage(ITestResult result);14 public void onStart(ITestContext context);15 public void onFinish(ITestContext context);16}17package org.testng;18public interface IInvokedMethodListener extends ITestListener {19 public void beforeInvocation(IInvokedMethod method, ITestResult testResult);20 public void afterInvocation(IInvokedMethod method, ITestResult testResult);21}22package org.testng;23public interface IInvokedMethod {24 public ITestResult getTestResult();25 public ITestMethod getTestMethod();26 public Object[] getParameters();27 public boolean isTestMethod();28 public boolean isConfigurationMethod();29}30package org.testng;31public interface IResultMap {32 public Object[] getAllMethods();33 public Object[] getAllResults();34 public Object[] getAllWithTimeout(long timeout);35 public Object[] getAllFailedButWithinSuccessPercentageMethods();36 public int size();37}38package org.testng;39public interface IResultListener extends ITestListener {40 public void onConfigurationSuccess(ITestResult itr);41 public void onConfigurationFailure(ITestResult itr);42 public void onConfigurationSkip(ITestResult itr);43 public void onTestStart(ITestResult result);44 public void onTestSuccess(ITestResult result);45 public void onTestFailure(ITestResult result);46 public void onTestSkipped(ITestResult result);47 public void onTestFailedButWithinSuccessPercentage(ITestResult result);48 public void onStart(ITestContext context);49 public void onFinish(ITestContext context);50}51package org.testng;52public interface IRetryAnalyzer {53 public boolean retry(ITestResult result);54}55package org.testng;56public interface IClassListener {57 public void onBeforeClass(ITestClass testClass);58 public void onAfterClass(ITestClass testClass);59}60package org.testng;61public interface IClass {62 public Object[] getInstances(boolean create);63 public Object[] getInstances();
Interface IConfigurationListener
Using AI Code Generation
1public class Configuration implements IConfigurationListener {2 public void onConfigurationSuccess(ITestResult itr) {3 System.out.println("Configuration success");4 }5 public void onConfigurationFailure(ITestResult itr) {6 System.out.println("Configuration failure");7 }8 public void onConfigurationSkip(ITestResult itr) {9 System.out.println("Configuration skip");10 }11}12public class Test implements ITestListener {13 public void onTestStart(ITestResult itr) {14 System.out.println("Test start");15 }16 public void onTestSuccess(ITestResult itr) {17 System.out.println("Test success");18 }19 public void onTestFailure(ITestResult itr) {20 System.out.println("Test failure");21 }22 public void onTestSkipped(ITestResult itr) {23 System.out.println("Test skip");24 }25}26public class TestMethod implements IInvokedMethodListener {27 public void beforeInvocation(IInvokedMethod iim, ITestResult itr) {28 System.out.println("Test method start");29 }30 public void afterInvocation(IInvokedMethod iim, ITestResult itr) {31 System.out.println("Test method finish");32 }33}34public class Execution implements IExecutionListener {35 public void onExecutionStart() {36 System.out.println("Execution start");37 }38 public void onExecutionFinish() {39 System.out.println("Execution finish");40 }41}42public class Reporter implements IReporter {43 public void generateReport(List<XmlSuite> list, List<ISuite> list1, String string) {44 System.out.println("Report generate");45 }46}47public class Suite implements ISuiteListener {48 public void onStart(ISuite is) {49 System.out.println("Suite start");50 }51 public void onFinish(ISuite is) {52 System.out.println("Suite
Interface IConfigurationListener
Using AI Code Generation
1package testng;2import org.testng.IConfigurationListener;3import org.testng.IInvokedMethod;4import org.testng.IInvokedMethodListener;5import org.testng.ITestResult;6public class TestNGListener implements IInvokedMethodListener, IConfigurationListener {7 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {8 String textMsg = "About to begin executing following method : " + returnMethodName(method.getTestMethod());9 System.out.println(textMsg);10 }11 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {12 String textMsg = "Completed executing following method : " + returnMethodName(method.getTestMethod());13 System.out.println(textMsg);14 }15 public void onConfigurationSuccess(ITestResult itr) {16 System.out.println("onConfigurationSuccess -> " + itr.getMethod().getMethodName());17 }18 public void onConfigurationSkip(ITestResult itr) {19 System.out.println("onConfigurationSkip -> " + itr.getMethod().getMethodName());20 }21 public void onConfigurationFailure(ITestResult itr) {22 System.out.println("onConfigurationFailure -> " + itr.getMethod().getMethodName());23 }24 private String returnMethodName(ITestNGMethod method) {25 return method.getRealClass().getSimpleName() + "." + method.getMethodName();26 }27}28package testng;29import org.testng.annotations.AfterMethod;30import org.testng.annotations.BeforeMethod;31import org.testng.annotations.Test;32public class TestNGListenerDemo {33 public void beforeMethod() {34 System.out.println("beforeMethod");35 }
Interface IConfigurationListener
Using AI Code Generation
1public class TestNGCustomListener implements IConfigurationListener {2 public void onConfigurationSuccess(ITestResult iTestResult) {3 System.out.println("onConfigurationSuccess");4 }5 public void onConfigurationFailure(ITestResult iTestResult) {6 System.out.println("onConfigurationFailure");7 }8 public void onConfigurationSkip(ITestResult iTestResult) {9 System.out.println("onConfigurationSkip");10 }11}12public class TestNGCustomListener implements ITestListener {13 public void onTestStart(ITestResult iTestResult) {14 System.out.println("onTestStart");15 }16 public void onTestSuccess(ITestResult iTestResult) {17 System.out.println("onTestSuccess");18 }19 public void onTestFailure(ITestResult iTestResult) {20 System.out.println("onTestFailure");21 }22 public void onTestSkipped(ITestResult iTestResult) {23 System.out.println("onTestSkipped");24 }25 public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {26 System.out.println("onTestFailedButWithinSuccessPercentage");27 }28 public void onStart(ITestContext iTestContext) {29 System.out.println("onStart");30 }31 public void onFinish(ITestContext iTestContext) {32 System.out.println("onFinish");33 }34}35public class TestNGCustomListener implements IInvokedMethodListener {36 public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {37 System.out.println("beforeInvocation");38 }39 public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {40 System.out.println("afterInvocation");41 }42}43public class TestNGCustomListener implements ISuiteListener {44 public void onStart(ISuite iSuite) {45 System.out.println("onStart");46 }47 public void onFinish(ISuite iSuite) {
Interface IConfigurationListener
Using AI Code Generation
1{2 public void onConfigurationSuccess(ITestResult itr) {3 System.out.println("onConfigurationSuccess method " + itr.getName());4 }5 public void onConfigurationFailure(ITestResult itr) {6 System.out.println("onConfigurationFailure method " + itr.getName());7 }8 public void onConfigurationSkip(ITestResult itr) {9 System.out.println("onConfigurationSkip method " + itr.getName());10 }11}12{13 public void beforeInvocation(IInvokedMethod iim, ITestResult itr) {14 System.out.println("beforeInvocation method " + iim.getTestMethod().getMethodName());15 }16 public void afterInvocation(IInvokedMethod iim, ITestResult itr) {17 System.out.println("afterInvocation method " + iim.getTestMethod().getMethodName());18 }19}20{21 public void onTestStart(ITestResult itr) {22 System.out.println("onTestStart method " + itr.getName());23 }24 public void onTestSuccess(ITestResult itr) {25 System.out.println("onTestSuccess method " + itr.getName());26 }27 public void onTestFailure(ITestResult itr) {28 System.out.println("onTestFailure method " + itr.getName());29 }30 public void onTestSkipped(ITestResult itr) {31 System.out.println("onTestSkipped method " + itr.getName());32 }33 public void onTestFailedButWithinSuccessPercentage(ITestResult itr) {34 System.out.println("onTestFailedButWithinSuccessPercentage " + itr.getName());35 }36 public void onStart(ITestContext itc) {37 System.out.println("onStart method " + itc.getName());38 }39 public void onFinish(ITestContext itc) {40 System.out.println("onFinish method " + itc.getName());41 }42}
Interface IConfigurationListener
Using AI Code Generation
1package com.test;2import org.testng.IConfigurationListener;3import org.testng.ITestResult;4import org.testng.annotations.AfterMethod;5import org.testng.annotations.BeforeMethod;6import org.testng.annotations.Test;7import org.testng.annotations.Listeners;8@Listeners(com.test.TestNGListener.class)9public class TestNGListener implements IConfigurationListener {10 public void onConfigurationSuccess(ITestResult itr) {11 System.out.println("onConfigurationSuccess method " + itr.getName());12 }13 public void onConfigurationFailure(ITestResult itr) {14 System.out.println("onConfigurationFailure method " + itr.getName());15 }16 public void onConfigurationSkip(ITestResult itr) {17 System.out.println("onConfigurationSkip method " + itr.getName());18 }19 public void beforeMethod() {20 System.out.println("beforeMethod");21 }22 public void afterMethod() {23 System.out.println("afterMethod");24 }25 public void testMethod1() {26 System.out.println("testMethod1");27 }28 public void testMethod2() {29 System.out.println("testMethod2");30 }31}
Interface IConfigurationListener
Using AI Code Generation
1public class ConfigurationListener implements IConfigurationListener{2 public void onConfigurationSuccess(ITestResult itr) {3 Reporter.log("onConfigurationSuccess method of ConfigurationListener is invoked.", true);4 }5 public void onConfigurationFailure(ITestResult itr) {6 Reporter.log("onConfigurationFailure method of ConfigurationListener is invoked.", true);7 }8 public void onConfigurationSkip(ITestResult itr) {9 Reporter.log("onConfigurationSkip method of ConfigurationListener is invoked.", true);10 }11}12public class InvokedMethodListener implements IInvokedMethodListener{13 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {14 Reporter.log("beforeInvocation method of InvokedMethodListener is invoked.", true);15 }16 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {17 Reporter.log("afterInvocation method of InvokedMethodListener is invoked.", true);18 }19}20public class TestListener implements ITestListener{21 public void onTestStart(ITestResult result) {22 Reporter.log("onTestStart method of TestListener is invoked.", true);23 }24 public void onTestSuccess(ITestResult result) {25 Reporter.log("onTestSuccess method of TestListener is invoked.", true);26 }27 public void onTestFailure(ITestResult result) {28 Reporter.log("onTestFailure method of TestListener is invoked.", true);29 }30 public void onTestSkipped(ITestResult result) {31 Reporter.log("onTestSkipped method of TestListener is invoked.", true);32 }33 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {34 Reporter.log("onTestFailedButWithinSuccessPercentage method of TestListener is invoked.", true);35 }36 public void onStart(ITestContext context) {37 Reporter.log("onStart method of TestListener is invoked.", true);38 }39 public void onFinish(ITestContext context) {40 Reporter.log("onFinish method of TestListener is invoked.", true);41 }42}
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!!