What is Listener?
As the name suggests Listeners "listen" to the event defined in the selenium script and behave accordingly. Listener is defined as interface that modifies the default TestNG's behavior.
Purpose of Listeners:
Listener is defined as interface that modifies the default TestNG's behavior. As the name suggests Listeners "listen" to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs. There are many types of TestNG listeners available.
There are two types of listeners:
As the name suggests Listeners "listen" to the event defined in the selenium script and behave accordingly. Listener is defined as interface that modifies the default TestNG's behavior.
Purpose of Listeners:
Listener is defined as interface that modifies the default TestNG's behavior. As the name suggests Listeners "listen" to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs. There are many types of TestNG listeners available.
There are two types of listeners:
- WebDriver Listeners – events triggered by web driver.
- TestNG Listeners - events triggered by TestNG.
TestNG Listeners:
- IAnnotationTransformer ,
- IAnnotationTransformer2 ,
- IConfigurable ,
- IConfigurationListener ,
- IExecutionListener,
- IHookable ,
- IInvokedMethodListener ,
- IInvokedMethodListener2 ,
- IMethodInterceptor ,
- IReporter,
- ISuiteListener,
- ITestListener
In this post, we will implement the ITestListener.
ITestListener has following methods
- OnStart- OnStart method is called when any Test starts.
- onTestSuccess- onTestSuccess method is called on the success of any Test.
- onTestFailure- onTestFailure method is called on the failure of any Test.
- onTestSkipped- onTestSkipped method is called on skipped of any Test.
- onTestFailedButWithinSuccessPercentage- method is called each time Test fails but is within success percentage.
- onFinish- onFinish method is called after all Tests are executed.
How to Implement: there are two ways to implement
- Class Level
- Suite Level (in testng.xml)
Lets have a look Class Level first:
1. Class Level
Created Listener class which implements ITestListener interface
package com.TestNGListners;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class ListnerClass implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println("Test Method " +result.getName() +" Starts");
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test Method " +result.getName() +" Passed");
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test Method " +result.getName() +" Failed");
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println("Test Method " +result.getName() +" Skipped");
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
}
@Override
public void onStart(ITestContext context) {
System.out.println("Test " +context.getName() +" Starts");
}
@Override
public void onFinish(ITestContext context) {
System.out.println("Test " +context.getName() +" Ends");
}
}
Test Class: in this class providing, @Listeners at class level
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(ListnerClass.class)
public class TestClass {
@Test
public void testCase1() {
Assert.assertTrue(true);
}
@Test
public void testCase2() {
Assert.assertTrue(true);
}
@Test
public void testCase3() {
Assert.assertTrue(false);
}
@Test(dependsOnMethods = "testCase3")
public void testCase4() {
Assert.assertTrue(true);
}
}
After executing the above class output will be seen as below:
Output:
Output:
Test Default test Starts
Test Method testCase1 Starts
Test Method testCase1 Passed
Test Method testCase3 Starts
Test Method testCase3 Failed
Test Method testCase2 Starts
Test Method testCase2 Passed
Test Method testCase4 Skipped
Test Default test Ends
PASSED: testCase1
PASSED: testCase2
FAILED: testCase3
java.lang.AssertionError: expected [true] but found [false]
Suite Level (in testng.xml)
Test Class: now we are adding one more test class as below
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestClass1 {
@Test
public void testCase5() {
Assert.assertTrue(true);
}
@Test
public void testCase6() {
Assert.assertTrue(true);
}
testng.xml: Configured <listeners> inside suite tab in testng.xml and included both classes (TestClass and TestClass1 in xml below file)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SmokeSuite">
<listeners>
<listener class-name="com.TestNGListners.ListnerClass"></listener>
</listeners>
<test name="SmokeTest">
<classes>
<class name="com.TestNGListners.TestClass"/>
<class name="com.TestNGListners.TestClass1"/>
</classes>
</test> <!-- SmokeTest -->
</suite> <!-- SmokeSuite -->
}
Output:
Test SmokeTest Starts
Test SmokeTest Starts
Test Method testCase1 Starts
Test Method testCase1 Starts
Test Method testCase1 Passed
Test Method testCase1 Passed
Test Method testCase3 Starts
Test Method testCase3 Starts
Test Method testCase3 Failed
Test Method testCase3 Failed
Test Method testCase2 Starts
Test Method testCase2 Starts
Test Method testCase2 Passed
Test Method testCase2 Passed
Test Method testCase4 Skipped
Test Method testCase4 Skipped
Test Method testCase5 Starts
Test Method testCase5 Starts
Test Method testCase5 Passed
Test Method testCase5 Passed
Test Method testCase6 Starts
Test Method testCase6 Starts
Test Method testCase6 Passed
Test Method testCase6 Passed
Test SmokeTest Ends
Test SmokeTest Ends
===============================================
SmokeSuite
Total tests run: 6, Failures: 1, Skips: 1
===============================================
Please refer below YouTube video on TestNG listeners
No comments:
Post a Comment