LISTENERS:
There are two main listeners i.e. WebDriver Listeners
and TestNG Listeners.
TestNG Listeners:
In TestNG,
there are several listeners that act as interfaces to modify the default
TestNG's behaviors.
As the name suggests Listeners "listen" to
the event defined in the selenium script and behave accordingly. It allows
customizing TestNG reports or logs.
With
the help of Listeners we can modify the behaviour of TestNG. We can create logs
with the help of listeners in TestNG. It is used in selenium by implementing
Listeners Interface. It is required for logging purpose.
Types of Listeners in TestNG
There are many types of listeners which allows you to change
the TestNG's behavior.
1.IAnnotationTransformer
2.IAnnotationTransformer2
3.IConfigurable
4.IConfigurationListener
5.IExecutionListener
6.IHookable
7.IInvokedMethodListener
8.IInvokedMethodListener2
9.IMethodInterceptor
10.IReporter
11.ISuiteListener
12.ITestListener
All Interfaces are called TestNG
Listeners. These interfaces are used in selenium to generate logs or customize
the testing reports.
ITestListener ( I ) :
When
running TestNG tests, one could want to perform some common actions – after
each test has finished successfully, after each failed test, after each skipped
test, or after all the tests have finished running, no matter their result.To
apply such a common behavior to a group of tests, a custom listener can be
created, that implements TestNG’sITestListener interface.
There
are two types of methods to implement: a set of them are related to a single
test’s run (onTestStart, onTestSuccess, onTestFailure, onTestSkipped,
onTestFailedButWithinSuccessPercentage), the other to the whole suite’s run
(onStart, onFinish).
These
methods, depending on their type, have a parameter passed to them: result,
which is of type ITestResult – for the test run, and context, which is of type
ITestContext, for the suite run.
These
types offer different information, for example: ITestResult can tell you when a
test began to run, when it finished running, what status the test had (whether
failed, passed), whereas ITestContext can tell you the list of all the passed
tests, all the failed ones, and so on.
Methods in ITestListener are following below ,
1.OnStart( )- OnStart method is called when any Test starts.
sysntax: public void onTestStart(ITestResult result)
2.onTestSuccess( )-
onTestSuccess method is called on the success of any Test.
syntax: public void onTestSuccess(ITestResult result)
3.onTestFailure( )-
onTestFailure method is called on the failure of any Test.
syntax: public void onTestFailure(ITestResult result)
4.onTestSkipped( )- onTestSkipped method is called on skipped of any
Test.
syntax: public void onTestSkipped(ITestResult result)
5.onTestFailedButWithinSuccessPercentage(
)- method is called each time Test fails but is within success
percentage.
syntax: public void
onTestFailedButWithinSuccessPercentage(ITestResult result)
6.onFinish( )- onFinish
method is called after all Tests are executed.
syntax: public void onFinish(ITestContext context)
Example:
1.Write out Test Cases in
ListenerDemo.java file
package
com.rameshsoft.rameshselenium;
import org.testng.annotations.Listeners;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.Test;
@Listeners(com.rameshsoft.rameshselenium.Listeners.class)
public class
ListenerClassDemo {
@Test
public
void method()
{
System.out.println("Starting
test method");
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver=new
FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
driver.get("https://www.gmail.com");
}
}
2.Write Listener
in Listeners.java file and our class must implement ITestListener interface and
add the unimplemented methods of that interface
package com.rameshsoft.rameshselenium;
import
org.testng.ITestContext;
import
org.testng.ITestListener;
import
org.testng.ITestResult;
public class
Listeners implements ITestListener {
@Override
public
void onTestStart(ITestResult result) {
System.out.println("Test
method started: "+result.getName());
}
@Override
public
void onTestSuccess(ITestResult result) {
System.out.println("Test
method passed: "+result.getName());
}
@Override
public
void onTestFailure(ITestResult result) {
System.out.println("Test
method failed: "+result.getName());
}
@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) {
System.out.println("hi...start");
//
TODO Auto-generated method stub
}
@Override
public
void onFinish(ITestContext context) {
//
TODO Auto-generated method stub
}
}
OUTPUT:
[TestNG] Running:
Hi...start
Test method started: method
Starting test method
Test method passed: method
Configuring Listeners at suite
level:
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="com.rameshsoft.rameshselenium.Listeners"></listener>
</listeners>
<test name="Test">
<classes>
<class name="com.rameshsoft.rameshselenium.ListenerClassDemo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite
-->
No comments:
Post a Comment