• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / What is TestNG listener and How to implement in Selenium

What is TestNG listener and How to implement in Selenium

March 24, 2015 by Mukesh Otwani 23 Comments

TestNG Listener

What is TestNG Listener

In last post, we have discussed WebDriver Listener and hope you understood the actual concept of listeners, if not I will strongly recommend you that please go through WebDriver Listener  also.

This is a very common question also in interviews that what is the actual difference between TestNG Listener and Webdriver Listener.

Let’s discuss now- When we talk about TestNG Listener we will use ITestListener Interface.

This interface has some methods which we need to override as per our requirement.

 

Recently I posted a video on youtube about Listeners hope it will help.

 

For example- take some method of ITestListener

 

1-@Override
    public void onFinish(ITestContext arg0) {
                
    }

 

 2-    @Override
    public void onStart(ITestContext arg0) {
        
       
    }

 

 3- @Override
    public void onTestFailure(ITestResult arg0) {
        

    }

 

etc..

 

How to implement TestNG Listener

Its not that tough as compared to WebDriver Listeners we simply have to implement ITestListener interface and override all method as per requirement and its not required that you should have some statements in all methods but yes you should use some of that method Wisely

 

Scenario-

1-If testcase is failing then what action should be performed

2- If testcase is skipped then what should be action requested and so far..

 

 

Step 1- Create a simple class and implement ITestListener listener

 

 

TestNG Listener

 

 

Step 2- Override all unimplemented methods

 

 

TestNG Listener

 

 

Once you implement all your java program will look like

 

package testngDemo;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ListenerDemoExample implements ITestListener{

    @Override
    public void onFinish(ITestContext arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onStart(ITestContext arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onTestFailure(ITestResult arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onTestSkipped(ITestResult arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onTestStart(ITestResult arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onTestSuccess(ITestResult arg0) {
        // TODO Auto-generated method stub
        
    }

}

 

So let me implement some of the methods and I will start my test.

 

I will override following method onTestStart, onTestSuccess(), onTestFailure()

 

I have written a small test for facebook login and I am failing this testcase forcefully so that we can check how different events will be fired if testcase pass or fail

 

Program

 

package testngDemo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Test;

public class ListenerDemoExample implements ITestListener{

    // This is dummy tescase for facebook login
    @Test
    public void loginFB(){
       
        WebDriver driver=new FirefoxDriver();
        driver.get("http://www.facebook.com");
        driver.manage().window().maximize();
        driver.findElement(By.id("email")).sendKeys("mukesh@facebook.com");
        driver.findElement(By.id("wronglocator")).sendKeys("dont-tell");
        driver.findElement(By.id("loginbutton")).click();
    }
    
    
    
    
    @Override
    public void onFinish(ITestContext arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onStart(ITestContext arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onTestFailure(ITestResult arg0) {
    
        System.out.println("Screen shot captured====="+arg0.toString());
       
    }

    @Override
    public void onTestSkipped(ITestResult arg0) {
        // TODO Auto-generated method stub
       
       
    }

    @Override
    public void onTestStart(ITestResult arg0) {
        // TODO Auto-generated method stub
        System.out.println("TestCase started====" +arg0.toString());
       
    }

    @Override
    public void onTestSuccess(ITestResult arg0) {
        // TODO Auto-generated method stub
       
        System.out.println("Congrates Testcase has been passed===="+ arg0.toString());
       
    }

}
 

 

 Step 3- It not completed yet now we need to modify our xml file that is known as testng.xml and we have to specify this listener and we have to execute that xml file only.

 

We have predefined listeners tag so that we will use today.

 

<listeners>
<listener class-name="testngDemo.ListenerDemoExample"/>
</listeners>

 

 

so your complete testng.xml file will look like.

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<listeners>
<listener class-name="testngDemo.ListenerDemoExample"/>
</listeners>
  <test name="Test">
    <classes>
      <class name="testngDemo.ListenerDemoExample"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

 

Step 4- Simple run this testng.xml file and analyze the output

 

TestNG Listener
TestNG Listener

 

 

Now if you analyze above output this event automatically getting triggered like once testcase start and testcase failed or testcase pass.

 

Filed Under: Advance Selenium, TestNG Tutorial Tagged With: Listener in Selenium

Reader Interactions

Comments

  1. Deepak Kumar says

    May 6, 2019 at 12:05 PM

    Hi Sir,
    How to create pdf reports using listeners

    Reply
    • Mukesh Otwani says

      May 6, 2019 at 5:02 PM

      Hi Deepak,

      What is your requirement?

      Thanks
      Mukesh

      Reply
  2. Gaurav Khurana says

    January 16, 2017 at 11:45 AM

    So there are 4 ways of logging now i have learned so far from the site.

    Can you please correct me if i am wrong somewhere or anything to be added. Its like a summary

    1) Reporter.log
    A simple print statement

    2) testNG
    The most easiest way since you just have to extend the interface and you get advance logging

    3) WebDriverEventliStener
    This a little complex since you have yo create 2 classes . One extending other class and use different object than driver but logging details are very good liked
    you click on which link, you navigated where

    4) Log4j
    external library, it has 4 different things under it and has its own syntax and classes

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:59 PM

      You missed Extent report 🙂

      Reply
      • Santosh Adhikari says

        March 3, 2020 at 1:37 PM

        Which one will be best ?

        Reply
        • Mukesh Otwani says

          March 3, 2020 at 3:09 PM

          Hi Santosh,

          Extent Report is the best way of reporting. You can add WebDriverEventListener logs, testng logs into extent report only

          Reply
  3. rajeev singh says

    November 8, 2016 at 12:48 PM

    Hi Mukesh,

    i would like to learn a lot from u in the coming future, plz let me know if there is any selenium job in ur city, Banglore, i would like to move to Banglore and would like to learn more and more from you.

    Reply
  4. saipradeep says

    April 15, 2016 at 4:42 PM

    Hi Mukesh,
    Would you let us know how to create a .bat file to run the webdriver script by doubclicking on the bat file?Searched entire youtube but found no luck.

    Reply
    • Mukesh Otwani says

      April 18, 2016 at 11:48 PM

      Hi Pradeep,

      Visit this video for the same https://www.youtube.com/watch?v=1e8I3A49ERc

      Reply
  5. Sathiya says

    April 15, 2016 at 3:55 PM

    Hi Mukesh,

    I am completely new to java and automation world. But after watching your videos , i never felt that i am new and i am still learning. Your videos are awesome to groom the new comers. Whenever i face any difficulties to understand any concepts, i use to search some videos in your name only.

    Reply
    • Mukesh Otwani says

      April 18, 2016 at 11:50 PM

      Thanks a ton Sathiya, let me know if any help from my side.

      Reply
      • Mahesh Bharti says

        September 3, 2016 at 2:48 PM

        Sir please share a video on Framework from scratch.

        Reply
        • Mukesh Otwani says

          September 8, 2016 at 12:00 AM

          Hi Mahesh,

          Below course will help http://learn-automation.usefedora.com/courses/selenium-frameworks-and-selenium-question-answers

          Reply
  6. Pradeep says

    April 14, 2016 at 1:00 PM

    no words from my side about your hardwork to serve the Software industry. awesome mukesh

    Reply
    • Mukesh Otwani says

      April 14, 2016 at 2:07 PM

      Hi Pradeep,

      Thanks a ton mate :). Keep visiting and let me know if any help from my side.

      Reply
  7. aashish mathur says

    March 26, 2016 at 3:42 PM

    hi mukesh, Thanks for the video.
    You mentioned that in next video you will cover TestListner Adaptor. I am unable to find that video. Can You share it please.

    Reply
    • Mukesh Otwani says

      March 27, 2016 at 9:07 PM

      Hi Aashish,

      Video will upload soon.

      Reply
  8. Ashish Mathur says

    March 26, 2016 at 8:41 AM

    Hi Mukesh, People in my office know you by your name name now because of such these videos 🙂
    In the beginning of this video you mentioned by your next post about webdriver Listners and how to extend ListnerAdaptors. I am unable to find that video.

    Reply
    • Mukesh Otwani says

      March 26, 2016 at 1:51 PM

      Hey Ashish,

      Thanks a ton 🙂 You made my day.

      I have not created video for WebDriver listener but post is available you can have a look. Will update video soon.

      http://learn-automation.com/what-is-listeners-in-selenium-webdriver/

      Reply
  9. jayesh hoondlani says

    March 11, 2016 at 2:18 PM

    great work big bro

    Reply
    • Mukesh Otwani says

      March 11, 2016 at 5:08 PM

      Thanks

      Reply
  10. Chandrasekhar Vadde says

    February 2, 2016 at 6:38 PM

    If i just say thanks, really that is very small to you 8055.
    You are rallying great …. sharing this valuable information for free.
    _/\_ ….. a big Thank you (what i can do more than this 🙂 )

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:20 PM

      Hi Chandra,

      You made my day thank you so much.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?