1package com.HFramework.utility;2import org.testng.IInvokedMethod;3import org.testng.IInvokedMethodListener;4import org.testng.ISuite;5import org.testng.ISuiteListener;6import org.testng.ITestContext;7import org.testng.ITestListener;8import org.testng.ITestNGMethod;9import org.testng.ITestResult;10import org.testng.Reporter;11public class Listener implements ITestListener, ISuiteListener, IInvokedMethodListener {12 /*13 * What is Listeners in Selenium WebDriver? 14 * Listener is defined as interface that modifes the default TestNG's behavior. As the name suggests Listeners15 * "listen" to the event defined in the selenium script and behave accordingly.16 * It is used in selenium by implementing Listeners Interface. It allows17 * customizing TestNG reports or logs. There are many types of TestNG listeners18 * available19 * 20 * Types of Listeners in TestNG There are many types of listeners which allows21 * you to change the TestNG's behavior.22 * 23 * Below are the few TestNG listeners:24 * 25 * IAnnotationTransformer , IAnnotationTransformer2 , IConfigurable ,26 * IConfigurationListener , IExecutionListener, IHookable ,27 * IInvokedMethodListener , IInvokedMethodListener2 , IMethodInterceptor ,28 * IReporter, ISuiteListener, ITestListener . Above Interface are called TestNG29 * Listeners. These interfaces are used in selenium to generate logs or30 * customize the Testing reports.31 */32 // This belongs to ISuiteListener and will execute before the Suite start33 public void onStart(ISuite isuitestart) {34 Reporter.log("About to begin executing Suite " + isuitestart.getName(), true);35 }36 // This belongs to ISuiteListener and will execute, once the Suite is finished37 public void onFinish(ISuite isuiteend) {38 Reporter.log("About to end executing Suite " + isuiteend.getName(), true);39 }40 // This belongs to ITestListener and will execute before starting of Test41 // set/batch42 public void onStart(ITestContext iteststart) {43 Reporter.log("About to begin executing Test " + iteststart.getName(), true);44 }45 // This belongs to ITestListener and will execute, once the Test set/batch is46 // finished47 public void onFinish(ITestContext itestend) {48 Reporter.log("Completed executing test " + itestend.getName(), true);49 }50 // This belongs to ITestListener and will execute only when the test is pass51 public void onTestSuccess(ITestResult itestpass) {52 // This is calling the printTestResults method53 printTestResults(itestpass);54 }55 // This belongs to ITestListener and will execute only on the event of fail test56 public void onTestFailure(ITestResult itestfail) {57 // This is calling the printTestResults method58 printTestResults(itestfail);59 }60 // This belongs to ITestListener and will execute before the main test start61 // (@Test)62 public void onTestStart(ITestResult mainiteststart) {63 System.out.println("The execution of the main test starts now");64 }65 // This belongs to ITestListener and will execute only if any of the main66 // test(@Test) get skipped67 public void onTestSkipped(ITestResult mainitestskiped) {68 printTestResults(mainitestskiped);69 }70 // This is just a piece of shit, ignore this71 public void onTestFailedButWithinSuccessPercentage(ITestResult successpercentage) {72 73 System.out.println(successpercentage);74 }75 // This is the method which will be executed in case of test pass or fail76 // This will provide the information on the test77 private void printTestResults(ITestResult result) {78 Reporter.log("Test Method resides in " + result.getTestClass().getName(), true);79 if (result.getParameters().length != 0) {80 String params = null;81 for (Object parameter : result.getParameters()) {82 params += parameter.toString() + ",";83 }84 Reporter.log("Test Method had the following parameters : " + params, true);85 }86 String status = null;87 switch (result.getStatus()) {88 case ITestResult.SUCCESS:89 status = "Pass";90 break;91 case ITestResult.FAILURE:92 status = "Failed";93 break;94 case ITestResult.SKIP:95 status = "Skipped";96 }97 Reporter.log("Test Status: " + status, true);98 }99 // This belongs to IInvokedMethodListener and will execute before every method100 // including @Before @After @Test101 public void beforeInvocation(IInvokedMethod methodname, ITestResult itestresults) {102 String textMsg = "About to begin executing following method : " + returnMethodName(methodname.getTestMethod());103 Reporter.log(textMsg, true);104 }105 // This belongs to IInvokedMethodListener and will execute after every method106 // including @Before @After @Test107 public void afterInvocation(IInvokedMethod arg1, ITestResult arg2) {108 String textMsg = "Completed executing following method : " + returnMethodName(arg1.getTestMethod());109 Reporter.log(textMsg, true);110 }111 // This will return method names to the calling function112 private String returnMethodName(ITestNGMethod method) {113 return method.getRealClass().getSimpleName() + "." + method.getMethodName();114 }115}...