TestNG Listeners – Selenium WebDriver | Selenium Tutorial

In this post, we see TestNG listeners. Listeners “listen” to the event defined in the selenium script and behave accordingly. The main purpose of using listeners is to create logs. There are many types of listeners such as WebDriver Listeners and TestNG Listeners.

Here in this post, we see TestNG Listeners. Using TestNG listeners we could generate logs and customize TestNG Reports.

Let’s see how to implement TestNG Listeners.

Step 1: Create a Class “ListenerTestNG” to implement ITestListener methods

Java

1
2
3
4
5
6
package listeners;
public class ListenerTestNG {
 
 
}

Java

1
2
3
4
5
package listeners;
 
public class ListenerTestNG implements ITestListener{
 
}

Mouse hover on ITestListener and import ITestListener Interface and mouse hover on ListenerTestNG and click on “Add unimplemented methods” to add multiple unimplemented methods (without body)

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package listeners;
 
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
 
public class ListenerTestNG implements ITestListener{
 
@Override
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
}
 
@Override
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
}
 
@Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
}
 
@Override
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
}
 
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
}
 
@Override
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
}
 
@Override
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
}
}

Here, I am implementing onTestSuccess, onTestSkipped, onTestFailure methods.

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package listeners;
 
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
 
public class ListenerTestNG implements ITestListener
{
 
    @Override
    public void onFinish(ITestContext Result)
    {
                
    }
 
    @Override
    public void onStart(ITestContext Result)
    {
            
    }
 
    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult Result)
    {
    
    }
 
    // When Test case get failed, this method is called.
    @Override
    public void onTestFailure(ITestResult Result)
    {
    System.out.println(“The name of the testcase failed is :”+Result.getName());
    }
 
    // When Test case get Skipped, this method is called.
    @Override
    public void onTestSkipped(ITestResult Result)
    {
    System.out.println(“The name of the testcase Skipped is :”+Result.getName());
    }
    @Override
    public void onTestStart(ITestResult Result)
    {
  
    }
 
    // When Test case get passed, this method is called.
    @Override
    public void onTestSuccess(ITestResult Result)
    {
    System.out.println(“The name of the testcase passed is :”+Result.getName());
    }
 
}

Step 2: Create another Class “ListenerTestNGTestCase” and write a script (which ever script you prefer). Else copy paste the below mentioned script.

Step 3: Add the listeners annotation (@Listeners) in the Class “ListenerTestNGTestCase

1
@Listeners(listeners.ListenerTestNG.class)

The complete “ListenerTestNGTestCase” class after adding Listener annotation is mentioned below:

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package listeners;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.SkipException;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(listeners.ListenerTestNG.class)
public class ListenerTestNGTestCase {
 
WebDriver driver= new FirefoxDriver();
 
// Test to pass as to verify listeners .
@Test(priority=1)
public void TestToPass()
{
    System.out.println(“This method to pass test”);
    driver.get(“https://www.softwaretestingmaterial.com/100-software-testing-interview-questions/”);
    driver.getTitle();
    driver.quit();
}
//Used skip exception to skip the test
@Test(priority=2)
public void TestToSkip ()
{
            System.out.println(“This method to skip test”);
    throw new SkipException(“Skipping – This is not ready for testing “);
}
// In the above method, we have already closed the browser. So we couldnot get the title here. It is to forcefully fail the test
@Test(priority=3)
public void TestToFail()
{
    driver.getTitle();
    System.out.println(“This method to test fail”);
}
}

Step 4: Execute the “ListenerTestNGTestCase” class. Methods in class “ListenerTestNG” are called automatically according to the behavior of methods annotated as @Test.

Step 5: Verify the Output in the console. You could find the logs in the console.

If you want to use listeners in multiple classes.

Add the below lines of code in the TestNG.xml file

Must Read: TestNG Tutorial

1
2
3
<listeners>
        <listener classname=“listeners.listenerTestNG”/>
</listeners>

Final testng.xml file will be like this:

1
2
3
4
5
6
7
8
9
10
11
12
<xml version=“1.0” encoding=“UFT-8”?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
<suite name=“Suite”>
    <listeners>
        <listener classname=“listeners.listenerTestNG”/>
    </listeners>
    <test name=“Test”>
        <classes>
            <class name=“listeners.ListenerTestNGTestCase”>
        </classes>
    </test>
</suite>

Execute it by right clicking on testng.xml and run as TestNG Suite

If you are not regular reader of SoftwareTestingMaterial.com then I highly recommend you to signup for the free email newsletter using the below link.