Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 16, 2016

Custom Logger in TestNG





Custom logger in the TestNG will discuss about writing our own logger in TestNG. Logging is one of the most important things to remember in automation testing. Logger will keep on eye of the execution flow that where went wrong in the script or it very useful to debug the script.

To generate custom loggers in testng we need to use Listeners. To implement listeners, need to implement the ITestListener interface in your class. These classes are notified at runtime by TestNG when the test case executes.
For this we need to have at least 2 classes. One is for actual test cases and other is for to implement the ITestListener.
Below is the sample program for test cases class:

import org.testng.Assert;
import org.testng.annotations.Test;

public class loggerTest
{
    @Test
    public void testOne()
    {
        Assert.assertTrue(true);
    }
    
    @Test
    public void testTwo()
    {
        Assert.assertTrue(false);
    }
}

When you execute the above program, the status of the test cases should be listen by the listener class. We have two methods called testOne and testTwo in the above program one is to pass and second is to fail status. While executing the tests, the listener will track the status of the each test..
Below is the sample listener class which will implements the ITestListener

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

public class listenerTest implements ITestListener {

    // It executes for on each test Start
    @Override
    public void onTestStart(ITestResult result) {
        System.out.println("Test Method Started :" + result.getName());
    }

    // It executes for only Passed tests
    @Override
    public void onTestSuccess(ITestResult result) {
        System.out.println("Test Method Passed :" + result.getName());
    }

    // It executes for only Failed tests
    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println("Test Method Failed :" + result.getName());
    }

    // It executes for only Skipped tests
    @Override
    public void onTestSkipped(ITestResult result) {
        System.out.println("Test Method Skipped :" + result.getName());
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        // No need to implement all the methods. Can leave empty.
    }

    // It executes for on Suite start
    @Override
    public void onStart(ITestContext context) {
        System.out.println("Test on Start :" + context.getName());
    }

    // It executes for on Suite finish
    @Override
    public void onFinish(ITestContext context) {
        System.out.println("Test on Finish :" + context.getName());
    }
}

The above class implements the ITestListener. So, it has to implement all the methods which are available in that interface. And it is not mandatory to use all the methods, we can left blank some of the methods which are not relevant to the requirement.
Now we have to create a testng.xml file to execute the above tests and it should have a mandatory tag called listener. It will call the listener class to listen the status of the each and every test which will execute.
Below is the sample program to execute the above program:

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

Output for the above program is:

1

This way we can use custom listeners to log the messages while executing.
Please watch the you tube video for better understanding.



Share this post: on Twitter on Facebook

TestNG Assertions Custom Reporter in TestNG

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

ASSERTIONS

TestNG

TestNG Assertions

PARALLEL EXECUTION OF classes

TestNG

Parallel Execution of Classes In TestNG

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025