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
package listeners; public class ListenerTestNG { }
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)
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.
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”
@Listeners(listeners.ListenerTestNG.class)
The complete “ListenerTestNGTestCase” class after adding Listener annotation is mentioned below:
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
<listeners> <listener class-name="listeners.listenerTestNG"/> </listeners>
Final testng.xml file will be like this:
<xml version="1.0" encoding="UFT-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <listeners> <listener class-name="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.
how to explain page object model framework in interview for 3 years experience.
please give me the format.
Hi Abhishek, please check this link.
https://www.softwaretestingmaterial.com/explain-test-automation-framework/
There is a video link too in that blog post.