Join 500+ Software Testing professionals. Solve problem and get your problem solved. Register Now!

0
what are testng listeners

Solved 1 Answers 68 Views Programming
0 1
I am using selenium with TestNG. I would like to know what are listeners in TestNG

1 Answer

0
Best answer
0

We generally use TestNG listeners to generate logs. We can either extend 'TestListenerAdapter' or implement Interface 'ITestListener' which is a listener for test running.

What are Listeners in TestNG : -

TestNG listeners are set of class exposed into TestNG framework which we can utilize to modify default TestNG's behavious.

E.g. We use 'ITestListener' or 'TestListenerAdapter' in order to write our own implementation when a Test fails or skips etc.

Below is list of methods

OnTestStart : This method can be  Invoked each time before a test will be invoked.
OnTestSuccess: Invoked each time a test succeeds.
OnTestFailure: Invoked each time a test fails E.g. we can utilize this method to take screenshot everytime test fails.
OnTestSkipped: Invoked each time a test is skipped.
OnTestFinish: Invoked after all the tests have run and all their Configuration methods have been called.

The below is the example program that demonstrates the Logging Listeners.

Step 1: Here is simple TestNG test class, LoggingClass.java, which has @Test methods.

package com.example.logging;

import org.testng.annotations.Test;

public class LoggingClass {

    @Test(priority = 0)
    public void methodAddingNumbers() {
        System.out.println("Helloo.. Im in method adding numbers");
    }

    @Test(priority = 1)
    public void dividedByZero() {
        System.out.println("Helloo.. Im in method divided by zero");
        int e = 1 / 0;
    }

    @Test(dependsOnMethods = { "dividedByZero" })
    public void methodSkip() {
        System.out.println("Helloo.. Im in method skip");
    }

}

In this example, method  "methodAddingNumbers()" will pass but "dividedByZero()" will fail. We can use '@Test(expectedExceptions=ArithmeticException.class)' to catch the exception and then that method will result as 'PASSED'. Method "methodSkip()" will be skipped as method it depends on, dividedByZero, is not passed.

Step 2: Here is 'ListenersClass.java'. We are going to implement TestNG listeners here.

package com.example.logging;

import org.testng.IClass;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class ListenerClass extends TestListenerAdapter {

    @Override
    public void onTestStart(ITestResult tr) {
        log("Test Started...");
    }

    @Override
    public void onTestSuccess(ITestResult TestResult) {

        log("Test '" + TestResult.getName() + "' PASSED");

        // This will print the class name in which the method is present
        log(TestResult.getTestClass());

        // This will print the priority of the method.
        // If the priority is not defined it will print the default priority as
        // 'o'
        log("Priority of this method is " + TestResult.getMethod().getPriority());

        System.out.println("... ...");
    }

    @Override
    public void onTestFailure(ITestResult TestResult) {

        log("Test '" + TestResult.getName() + "' FAILED");
        log("Priority of this method is " + TestResult.getMethod().getPriority());
        System.out.println("... ...");
    }

    @Override
    public void onTestSkipped(ITestResult TestResult) {
        log("Test '" + TestResult.getName() + "' SKIPPED");
        System.out.println("... ...");
    }

    private void log(String methodName) {
        System.out.println(methodName);
    }

    private void log(IClass testClass) {
        System.out.println(testClass);
    }
}

To get list of more methods present inside "TestListenerAdapter" you canfollow this link

Step 3: Here is testng.xml file.

<suite name="Log Suite Example" verbose="1">
    <listeners>
        <listener class-name="com.example.logging.ListenerClass" />
    </listeners>

    <test name="TestNG logs sample" preserve-order="true">
        <classes>
            <class name="com.example.logging.LoggingClass">
                <methods>
                    <include name="methodAddingNumbers" />
                    <include name="dividedByZero" />
                    <include name="methodSkip" />
                </methods>
            </class>
        </classes>
    </test>

</suite>

Ouput :

[TestNG] Running:
  G:\TestNG\TestNGExamples\testng.xml

Test Started....
Helloo.. Im in method adding numbers
Test 'methodAddingNumbers' PASSED
[TestClass name=class com.example.logging.LoggingClass]
Priority of this method is 0
... ...
Test Started....
Helloo.. Im in method divided by zero
Test 'dividedByZero' FAILED
Priority of this method is 1
... ...
Test 'methodSkip' SKIPPED
... ...

===============================================
Log Suite Example
Total tests run: 3, Failures: 1, Skips: 1
===============================================

answered by (151 points)
selected by

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
...