{"id":4008,"date":"2020-07-10T15:24:07","date_gmt":"2020-07-10T09:54:07","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4008"},"modified":"2025-04-03T07:09:32","modified_gmt":"2025-04-03T11:09:32","slug":"testng-listeners-in-selenium","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/testng-listeners-in-selenium\/","title":{"rendered":"TestNG Listeners in Selenium"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Listeners?<\/strong><\/h2>\n\n\n\n<p>Listeners are a very important feature of TestNG, which allow you to customize logs or reports of TestNG. As the name suggests, it listens to certain events and behaves accordingly. We can fully customize the logs using listeners. There are several interfaces in Java that allow you to modify TestNG behavior. These interfaces are known as TestNG Listeners in Selenium WebDriver. <\/p>\n\n\n\n<p>TestNG Listeners also allow us to customize the test logs or reports according to project requirements. <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium testing<\/a> course content often covers TestNG Listeners, as they are essential for tracking events and executing specific actions at different stages of test execution, enhancing control and reporting.<\/p>\n\n\n\n<p>In Selenium WebDriver TestNG Listeners can be implemented at two levels:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Class Level: In this level, for every particular class we implement listeners, no matter how many test cases it includes.<\/li>\n\n\n\n<li>Suite Level: In this level, for a particular suite we implement listeners which include several classes of test cases.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Listeners in Selenium WebDriver<\/strong><\/h2>\n\n\n\n<p>There are several interfaces that allow you to modify TestNG\u2019s behaviour. These interfaces are broadly called \u201cTestNG Listeners\u201d. Below are the few listeners:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>IAnnotation Transformer<\/li>\n\n\n\n<li>IConfigurationListener&nbsp;<\/li>\n\n\n\n<li>IHookable<\/li>\n\n\n\n<li>IInvokedMethodListener<\/li>\n\n\n\n<li>IMethodInterceptor<\/li>\n\n\n\n<li>IReporter<\/li>\n\n\n\n<li>ISuiteListener<\/li>\n\n\n\n<li>ITestListener<\/li>\n\n\n\n<li>IConfigurable<\/li>\n\n\n\n<li>IExecutionListener<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Frequently Used TestNG Listeners With Examples<\/strong><\/h2>\n\n\n\n<p>Below are the most popular and widely used TestNG listeners with examples.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>ITestListener: <\/strong>ITestListener is the most popular TestNG listener in Selenium WebDriver. It provides an easy way to implement interface through a normal Java class, where the class overrides every method that are declared inside the ITestListener. By using TestNG listeners in Selenium WebDriver, you can change the default behavior of your test by adding different events to the methods and also defines a new way of logging or reporting.<\/li>\n<\/ol>\n\n\n\n<p>ITestListener interface will implements Listeners. An ITestListener interface has following methods.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>onTestStart(): <\/strong>An onTestStart() method is invoked before any test method gets started.<\/li>\n\n\n\n<li><strong>onTestSuccess(): <\/strong>An onTestSuccess() method is invoked when any test method succeeds. It is used to indicate that the particular test method has successfully finished its execution.<\/li>\n\n\n\n<li><strong>onTestFailure(): <\/strong>An onTestFailure() method is executed when test method fails. It is used to indicate that the particular test method has failed.<\/li>\n\n\n\n<li><strong>&nbsp;onTestSkipped(): <\/strong>An onTestSkipped() method is executed when each test method is skipped. It is used to indicate that the particular test method has been skipped.&nbsp;<\/li>\n\n\n\n<li><strong>onTestFailedButWithinSuccessPercentage: <\/strong>This method is invoked each time the test method fails but within success percentage.<\/li>\n\n\n\n<li><strong>onStart(): <\/strong>An onStart() method is invoked on the start of any test method.<\/li>\n\n\n\n<li><strong>onFinish(): <\/strong>An onFinish() method is invoked when all test methods gets executed.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Test Example:<\/strong><\/h2>\n\n\n\n<p>In this scenario, we&#8217;re going to automate the login process and incorporate the &#8216;ITestListener&#8217; feature.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We&#8217;ll begin by launching the Chrome browser and navigating to the website <a href=\"http:\/\/www.gmail.com\/\" rel=\"nofollow noopener\" target=\"_blank\">www.gmail.com<\/a>.<\/li>\n\n\n\n<li>After that, we&#8217;ll execute the login procedure.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps for Setting Up a TestNG Listener<\/strong><\/h2>\n\n\n\n<p>Step 1: Open up Eclipse.<\/p>\n\n\n\n<p>Step 2: Create a class called &#8220;TestListener&#8221; that implements the &#8216;ITestListener&#8217; interface. Fill in the missing methods by simply hovering over the highlighted lines.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/Kqww_2AzxWeCh8IkTQNojF5fjHWxtEM0gVpBwWAHgVbFrvMkVyKRKkGMpN1Et8N2hh_WE9kgRmShuGRt7R9FlWR7cNTnZBrJDprze0QQJ5LveGqtmPSRDpVRLQzwrsfqAEnCTJeawedWfNFzcw\" alt=\"listener.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Click the &#8220;Add unimplemented methods&#8221; option. This action will insert several unimplemented methods (without content) into the code, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> ListenerDemo;\n\n<strong>import<\/strong> org.testng.ITestContext;\n<strong>import<\/strong> org.testng.ITestListener;\n<strong>import<\/strong> org.testng.ITestResult;\n\n<strong>public<\/strong> <strong>class<\/strong> ListenerTest <strong>implements<\/strong> ITestListener {\n\n<strong>public<\/strong> <strong>void<\/strong> onTestStart(ITestResult result) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\n\n}\n\n<strong>public<\/strong> <strong>void<\/strong> onTestSuccess(ITestResult result) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\n\n}\n\n<strong>public<\/strong> <strong>void<\/strong> onTestFailure(ITestResult result) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\n\n}\n\n<strong>public<\/strong> <strong>void<\/strong> onTestSkipped(ITestResult result) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\n\n}\n\n<strong>public<\/strong> <strong>void<\/strong> onTestFailedButWithinSuccessPercentage(ITestResult result) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\n\n}\n\n<strong>public<\/strong> <strong>void<\/strong> onStart(ITestContext context) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\n\n}\n\n<strong>public<\/strong> <strong>void<\/strong> onFinish(ITestContext context) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\n\n}\n\n}\n<\/pre>\n\n\n\n<p>Let&#8217;s make some changes to the &#8216;ListenerTest&#8217; class now. Specifically, we&#8217;ll be modifying the following methods:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/PGG5jZGMZY3KO0SAh3ykcb2kfMeFQysvI5BGcPT5jXwb9lI2fFLC2o6fPwFKMiu5_n6VnA2U_B2paPBv2_UQMbZfjXlrWw71xhGLLmdo8FcwDv3roXBfDzlx-XIwAU0txjUyxuFcykXeBbyGsg\" alt=\"methods.PNG\" title=\"\"><\/figure>\n\n\n\n<p>In the updated version, we&#8217;re simply printing the name of the test. This generates logs in the console that make it straightforward for the user to identify whether a test has passed, failed, or been marked as skipped.<\/p>\n\n\n\n<p>After these modifications, the code will appear as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;<br><br><strong>import<\/strong> org.testng.ITestContext;<br><strong>import<\/strong> org.testng.ITestListener;<br><strong>import<\/strong> org.testng.ITestResult;<br><br><strong>public<\/strong> <strong>class<\/strong> ListenerTest <strong>implements<\/strong> ITestListener {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ This method is called when Test case get Started.<br><strong>public<\/strong> <strong>void<\/strong> onTestStart(ITestResult result) {<br>&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(result.getName()+\" test case started\");<br><br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ This method is called when Test case get passed.<br><strong>public<\/strong> <strong>void<\/strong> onTestSuccess(ITestResult result) {<br>&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"The name of the testcase passed is :\"+result.getName());<br><br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ This method is called when Test case get failed,.<br>&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> onTestFailure(ITestResult result) {<br>&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"The name of the testcase failed is :\"+result.getName());<br><br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ This method is called when Test case get Skipped,.<br>&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> onTestSkipped(ITestResult result) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"The name of the testcase Skipped is :\"+result.getName());<br>}<br><br><strong>public<\/strong> <strong>void<\/strong> onTestFailedButWithinSuccessPercentage(ITestResult result) {<br>\/\/ <strong>TODO<\/strong> Auto-generated method stub<br><br>}<br><br><strong>public<\/strong> <strong>void<\/strong> onStart(ITestContext context) {<br>\/\/ <strong>TODO<\/strong> Auto-generated method stub<br><br>}<br><br><strong>public<\/strong> <strong>void<\/strong> onFinish(ITestContext context) {<br>\/\/ <strong>TODO<\/strong> Auto-generated method stub<br><br>}<br><br>}<br><br><br>Step 3: Develop another class called \"TestCases\" to handle the login process. Selenium will utilize this 'TestCases' class to execute the login procedure automatically.<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;<br><br><strong>import<\/strong> org.openqa.selenium.By;<br><strong>import<\/strong> org.openqa.selenium.WebDriver;<br><strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;<br><strong>import<\/strong> org.testng.Assert;<br><strong>import<\/strong> org.testng.annotations.Test;<br><br><strong>public<\/strong> <strong>class<\/strong> TestCases {<br><br>WebDriver driver= <strong>new<\/strong> ChromeDriver();<br><br>\/\/ Test to pass as to verify listeners .<br>@Test<br><strong>public<\/strong> <strong>void<\/strong> Login()<br>{<br>&nbsp; &nbsp; driver.get(\"https:\/\/login.yahoo.com\/config\/login?\");<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-username\")).sendKeys(\"vinodreddy10.thumma@gmail.com\");<br>&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-signin\")).click();<br>&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-passwd\")).sendKeys(\"Oracle11i\");<br>&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-signin\")).click();<br>}<br><br>\/\/ Forcefully failed this test as to verify listener.<br>@Test<br><strong>public<\/strong> <strong>void<\/strong> TestToFail()<br>{<br>&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"This method to test fail\");<br>&nbsp; &nbsp; Assert.<em>assertTrue<\/em>(<strong>false<\/strong>);<br>}<br><br><br>}<br><br><br>Step 4: We'll incorporate this listener into our project class named \"TestCases.\" There are two distinct methods to link the class and interface.<\/pre>\n\n\n\n<p>The initial approach involves using the Listeners annotation (@Listeners), demonstrated as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/BA4CzIhB4PlVcjszHgcaaEi2j16Z7-bRj71-LWahulASoQRUaiSDBwCXqZZjw0EJ2HZ_6Smm_HDlf-VqU6vLH0HTfU1aUwGPzqdNxRqX_zHOxovn6cZ6GPJjYzjmS_1M-q6Tc3UJRJRNoEa2Ag\" alt=\"listenerclass.PNG\" title=\"\"><\/figure>\n\n\n\n<p>We apply this in the &#8220;TestCases&#8221; class, as illustrated below.<\/p>\n\n\n\n<p>Thus, the appearance of the &#8220;TestCases&#8221; class following the implementation of the Listener annotation will be:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;\n\n<strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n<strong>import<\/strong> org.testng.Assert;\n<strong>import<\/strong> org.testng.annotations.Listeners;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n@Listeners(ListenerDemo.ListenerTest.<strong>class<\/strong>)\n\n\n<strong>public<\/strong> <strong>class<\/strong> TestCases {\nWebDriver driver= <strong>new<\/strong> ChromeDriver();\n\n\/\/ Test to pass as to verify listeners .\n@Test\n<strong>public<\/strong> <strong>void<\/strong> Login()\n{\n&nbsp; &nbsp; driver.get(\"https:\/\/login.yahoo.com\/config\/login?\");\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-username\")).sendKeys(\"vinodreddy10.thumma@gmail.com\");\n&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-signin\")).click();\n&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-passwd\")).sendKeys(\"Oracle11i\");\n&nbsp; &nbsp; driver.findElement(By.<em>id<\/em>(\"login-signin\")).click();\n}\n\n\/\/ Forcefully failed this test as to verify listener.\n@Test\n<strong>public<\/strong> <strong>void<\/strong> TestToFail()\n{\n&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"This method to test fail\");\n&nbsp; &nbsp; Assert.<em>assertTrue<\/em>(<strong>false<\/strong>);\n}\n\n\n}\n<\/pre>\n\n\n\n<p>When you run the above class the project structure is shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/UJAnAGrPRQGeRL0bwytXhY7EXOI9IyWvTkPKT1h-TQJ4LNe7k5yiuPa6TfBnk5kNKXmHQ528wTdxYLloGFUZQ1w5PRZdX4no0cuPxEUwLx0PDxN4iIL6RUryDsGpM2ZX0yF_k57v9-szFCyrvA\" alt=\"out.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Step 5: Now, let&#8217;s run the &#8220;TestCases&#8221; class. The methods from the &#8220;ListenerTest&#8221; class will be called automatically depending on how the methods annotated with @Test behave.<\/p>\n\n\n\n<p>Step 6: Take a look at the console output. The logs displayed after running the &#8216;TestCases&#8217; will look something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/S7eyZMVMeMx0lE3QB3pXaAxKES9p2YLT316Wqe-jfrkndIUG-F8pmKmhWEagR4KD1y2o_EFxYpbB6rioNYMTajk5p3aefpfMpTHb4EJyZxeC2TTrr8pYGEMFozTC3-F6kUCl1K6IYd1HFxbb9g\" alt=\"output1.PNG\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using Listeners for Multiple Classes:<\/h2>\n\n\n\n<p>In cases where a project involves <a href=\"https:\/\/www.h2kinfosys.com\/blog\/interfaces-abstract-classes\/\">multiple classes<\/a>, if we need to add listeners, we can handle this by creating a testng.xml file. Within the XML, we can include a listeners tag to achieve the desired functionality.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;&nbsp;&nbsp;\n&lt;!DOCTYPE suite SYSTEM \"http:\/\/testng.org\/testng-1.0.dtd\"&gt;&nbsp;&nbsp;\n&lt;suite name=<em>\"test_suite\"<\/em>&gt;&nbsp;&nbsp;\n&lt;test name=<em>\"Including test cases\"<\/em>&gt;&nbsp;&nbsp;\n&lt;groups&gt;&nbsp;&nbsp;\n&lt;run&gt;&nbsp;&nbsp;\n&lt;include name=<em>\"Include.*\"<\/em>\/&gt;&nbsp;&nbsp;\n&lt;\/run&gt;&nbsp;&nbsp;\n&lt;\/groups&gt;&nbsp;&nbsp;\n&lt;classes&gt;&nbsp;&nbsp;\n&lt;class name=<em>\"com.testing.GroupRegularExpression\"<\/em>\/&gt;&nbsp;&nbsp;\n&lt;\/classes&gt;&nbsp;&nbsp;\n&lt;\/test&gt; &lt;!-- Test --&gt;&nbsp;&nbsp;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;&nbsp;<\/pre>\n\n\n\n<p>This listener is applied consistently across all classes within the entire test suite. Once you execute the XML file mentioned above, the listeners will operate on all the classes specified. You&#8217;re free to declare any number of listener classes as needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. IAnnotationTransformer<\/strong><\/h3>\n\n\n\n<p>IAnnotationTransformer is an interface that gets invoked by TestNG to modify the behavior of the test annotation method in our test class that provides a \u201ctransform\u201d method. The transform method provides parameters:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>annotation:<\/strong>&nbsp;The annotation is&nbsp;read from the test class.<\/li>\n\n\n\n<li><strong>testClass:<\/strong>&nbsp;This parameter would represent the same class if the annotation found on a class.<\/li>\n\n\n\n<li><strong>testConstructor:<\/strong>&nbsp;If the annotation found a constructor, this parameter would represent that same constructor.<\/li>\n\n\n\n<li><strong>testMethod:<\/strong>&nbsp;If the annotation found a method, this parameter would represent that same method.<\/li>\n<\/ol>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;At least one of the parameters will be non-null.<\/p>\n\n\n\n<p>Below is the code that would be executed at the suite level. In this code, we used a (<strong>\u201calwaysRun = true\u201d<\/strong>) parameter in our Test annotation that indicates that the test method would always run even if the parameters on which the method depends fails. However, we would transform this behavior of our test method through IAnnotationTransformer Listener which won\u2019t allow the particular test method to get executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Listeners Class File:<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;\n\n<strong>import<\/strong> java.lang.reflect.Constructor;\n<strong>import<\/strong> java.lang.reflect.Method;\n\n<strong>import<\/strong> org.testng.annotations.ITestAnnotation;\n\n<strong>public<\/strong> <strong>class<\/strong> AnnotationTransformers {\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>boolean<\/strong> isTestRunning(ITestAnnotation ins) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if<\/strong> (ins.getAlwaysRun()) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> <strong>true<\/strong>;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> <strong>false<\/strong>;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if<\/strong> (isTestRunning(annotation)) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;annotation.setEnabled(<strong>false<\/strong>);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Test Class File:<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;\n\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> AnnotationTransformerTests {\n@Test(alwaysRun = <strong>true<\/strong>) <strong>public<\/strong> <strong>void<\/strong> test1() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"This is my first test whose behaviour&nbsp; get changed while executing\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;@Test <strong>public<\/strong> <strong>void<\/strong> test2() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"This is my executed second test\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Console Output Screen:<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/5gfSvq3yGx_fRB98tqlKCegHF27BcLhb3JUWlk6sY5Lw7mbZKxRWlKelJdWoJ6tAwNW21nq7d0vPa4pptQ4zMSfTz60lrjN_TEROI1Jpn2yIDEtc-nv86UjbdsHB_mXzrO61Z2W4AmHF5CSMBw\" alt=\"output2.PNG\" title=\"\"><\/figure>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>ISuiteListener:<\/strong> This ISuiteListener listener in Selenium WebDriver is implemented at a suite level called ISuiteListener. It mainly has two methods:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>onStart:<\/strong>&nbsp;This method is executed prior the test suite.<\/li>\n\n\n\n<li><strong>onFinish:<\/strong>&nbsp;This method is executed post the test suite.<\/li>\n<\/ul>\n\n\n\n<p>This ISuiteListener basically listens to the events that have occurred before and after the execution of the suite. If the parent suite contains child suites then these child suites are executed before running the parent suite.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;Implementing ISuiteListener with java class and adding the unimplemented methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Class: ListenersSuite<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;\n\n<strong>import<\/strong> org.testng.ISuite;\n<strong>import<\/strong> org.testng.ISuiteListener;\n\n<strong>public<\/strong> <strong>class<\/strong> ListenersSuite <strong>implements<\/strong> ISuiteListener {\n<strong>public<\/strong> <strong>void<\/strong> onStart(ISuite suite) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Suite executed onStart\" + suite.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> onFinish(ISuite suite) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Suite executed onFinish\" + suite.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;Creating two test classes and add in two different child suites.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Class 1: Tests1SuiteListeners<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;\n\n<strong>import<\/strong> org.testng.annotations.AfterSuite;\n<strong>import<\/strong> org.testng.annotations.BeforeSuite;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> Tests1SuiteListeners {\n&nbsp;&nbsp;&nbsp;&nbsp;@BeforeSuite <strong>public<\/strong> <strong>void<\/strong> test1() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"BeforeSuite method in Suite1\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;@Test <strong>public<\/strong> <strong>void<\/strong> test2() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Main method 1 Test\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;@AfterSuite <strong>public<\/strong> <strong>void<\/strong> test3() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"AfterSuite method in Suite1\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Class 2: Tests2SuiteListeners<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> Listener_Demo;\n\n<strong>import<\/strong> org.testng.annotations.AfterSuite;\n<strong>import<\/strong> org.testng.annotations.BeforeSuite;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> Tests2SuiteListeners {\n&nbsp;&nbsp;&nbsp;&nbsp;@BeforeSuite <strong>public<\/strong> <strong>void<\/strong> test1() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"BeforeSuite method in Suite2\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;@Test <strong>public<\/strong> <strong>void<\/strong> test2() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Main method 2 Test\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;@AfterSuite <strong>public<\/strong> <strong>void<\/strong> test3() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"AfterSuite method in Suite2\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 3: Adding the test classes to the child suites.<\/strong><\/h2>\n\n\n\n<p><strong>Suite 1: Test Suite One.xml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;\n&lt;suite name=<em>\"Test Suite One\"<\/em>&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;test name=<em>\"Test Method1\"<\/em>&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;classes&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;class name=<em>\"Listener_Demo.Tests1SuiteListeners\"<\/em> \/&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/classes&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/test&gt;\n&lt;\/suite&gt;<\/pre>\n\n\n\n<p><strong>Suite 2: Test Suite Two.xml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;\n&lt;suite name=<em>\"Test Suite Two\"<\/em>&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;test name=<em>\"Test Method2\"<\/em>&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;classes&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;class name=<em>\"Listener_Demo.Tests2SuiteListeners\"<\/em> \/&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/classes&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/test&gt;\n&lt;\/suite&gt;<\/pre>\n\n\n\n<p><strong>Step 4:<\/strong>&nbsp;Create a parent suite xml file and combine above 2 defined suites along with the listeners class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;\n&lt;suite name=<em>\"suiteListener\"<\/em>&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;listeners&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;listener class-name=<em>\"Listener_Demo.ListenersSuite\"<\/em>&gt;&lt;\/listener&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/listeners&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;suite-files&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;suite-file path=<em>\".\/One.xml\"<\/em>&gt;&lt;\/suite-file&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;suite-file path=<em>\".\/Two.xml\"<\/em>&gt;&lt;\/suite-file&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/suite-files&gt;\n&lt;\/suite&gt;\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Console Output Screen:<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/lXPvKr3WXuoVOHoTmzewz0Mze9HCPDb7wOGUqUgx595QCIwbxGvrvx7NvE-N4_HrRhOX27MURcSYK7w1gnx63dJ5QiqD076wVWx4S9GA30tAJV0KL4_tksmJL9MudtAEyxpKCcKoMxLPucT3rQ\" alt=\"listoutput.PNG\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conculsion:<\/strong><\/h2>\n\n\n\n<p>In Selenium WebDriver, listeners are valuable tools for generating logs and customizing TestNG reports, providing greater insight and control over test executions. These TestNG Listeners in Selenium are interfaces that can be implemented in test scripts to automatically perform specific actions based on events such as test start, success, or failure. <\/p>\n\n\n\n<p>By using TestNG Listeners in Selenium, you can streamline the logging process, automate actions like capturing screenshots on failure, and enhance reporting with minimal manual intervention.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>There are different types of listeners and can be used as per requirements.<\/li>\n\n\n\n<li>Listeners are interfaces used in selenium web driver script<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Call to Action<\/strong><\/h2>\n\n\n\n<p>Ready to elevate your automation testing capabilities? With TestNG Listeners in Selenium, you\u2019ll gain the skills to customize test reports, automate logging, and improve test case management. At H2K Infosys, our comprehensive Selenium courses cover TestNG Listeners in-depth, providing hands-on experience with real-world projects.<\/p>\n\n\n\n<p>Join H2K Infosys today to learn how to effectively implement TestNG Listeners in Selenium and take your automation testing skills to the next level!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Listeners? Listeners are a very important feature of TestNG, which allow you to customize logs or reports of TestNG. As the name suggests, it listens to certain events and behaves accordingly. We can fully customize the logs using listeners. There are several interfaces in Java that allow you to modify TestNG behavior. These [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4019,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[1097,1096,1094,1095],"class_list":["post-4008","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-iannotationtransformer","tag-listener-use-for-multiple-classes","tag-testng-listeners","tag-types-of-listeners-in-selenium-webdriver"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4008"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4008\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4019"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}