Saturday, March 23, 2019

ITestListener in TestNG Selenium




Test file

 package learning;  
 import org.testng.Assert;  
 import org.testng.annotations.Listeners;  
 import org.testng.annotations.Test;  
 @Listeners(MyListeners.class)  
 public class ListenerExample {  
      @Test  
      public void testToFail() {  
           Assert.assertTrue(false);  
      }  
      public void testToPass(){  
           Assert.assertTrue(true);  
      }  
 }  


Listener class

 package learning;  
 import org.testng.ITestContext;  
 import org.testng.ITestListener;  
 import org.testng.ITestResult;  
 import org.testng.SkipException;  
 public class MyListeners implements ITestListener {  
      @Override  
      public void onTestStart(ITestResult result) {  
           System.out.println("Test started " + result);  
      }  
      @Override  
      public void onTestSuccess(ITestResult result) {  
           System.out.println("This test passed " + result);  
      }  
      @Override  
      public void onTestFailure(ITestResult result) {  
           System.out.println("This test failed " + result);  
      }  
      @Override  
      public void onTestSkipped(ITestResult result) {  
           System.out.println("Test is Skipped " + result);  
           throw new SkipException("Skipping this exception");  
      }  
      @Override  
      public void onTestFailedButWithinSuccessPercentage(ITestResult result) {  
      }  
      @Override  
      public void onStart(ITestContext context) {  
           System.out.println("Test started");  
      }  
      @Override  
      public void onFinish(ITestContext context) {  
           System.out.println("Test finnished");  
      }  
 }  


TestNG.xml

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite">  
      <listeners>  
           <listener class-name="learning.MyListeners" />  
      </listeners>  
      <test name="Test">  
           <classes>  
                <class name="learning.HilightExample" />  
           </classes>  
      </test> <!-- Test -->  
 </suite> <!-- Suite -->  

No comments:

Post a Comment