Extending the TestListenerAdapter class

A second approach to implementing listeners is to extend the TestListenerAdapter class.

The following code shows a sample listener class:

public class SampleListener1 extends TestListenerAdapter {
public void onTestSuccess(ITestResult testResult) {
System.out.println(testResult.getName() + " was a success");
}
public void onTestFailure(ITestResult testResult) {
System.out.println(testResult.getName() + " was a failure");
}
public void onTestSkipped(ITestResult testResult) {
System.out.println(testResult.getName() + " was skipped");
}
}

The test class for the preceding listener will not change. The output is shown here:

Title is : #1 Free CRM software in the cloud for sales and service
checkListeners was a success

With this knowledge, let's have a look at the listener for our framework.

The UtilityListener class, shown in the following code, indicates that we have implemented three interfaces:

public class UtilityListener implements ITestListener, ISuiteListener,
IInvokedMethodListener {
// This will execute before the Suite start
@Override
public void onStart(ISuite arg0) {
Reporter.log("About to start Suite execution" + arg0.getName(),
true);
}
// This will execute when suite has finished
@Override
public void onFinish(ISuite arg0) {
Reporter.log("About to exit executing Suite " + arg0.getName(),
true);
}
// This will execute before start of Test
public void onStart(ITestContext arg0) {
Reporter.log("About to begin executing Test " + arg0.getName(),
true);
}
// This will execute, once the Test is finished
public void onFinish(ITestContext arg0) {
Reporter.log("Completed executing test " + arg0.getName(),
true);
}
// This will execute once the test passes
public void onTestSuccess(ITestResult arg0) {
// This calls the printTestResults method
printTestResults(arg0);
}
// This will execute only on the event of fail
public void onTestFailure(ITestResult arg0) {
printTestResults(arg0);
}
public void onTestStart(ITestResult arg0) {
System.out.println("The execution of the main test starts");
}
// This will execute only if any of the main test skipped
public void onTestSkipped(ITestResult arg0) {
printTestResults(arg0);
}
public void onTestFailedButWithinSuccessPercentage(ITestResult
arg0) {
}

This printTestResults method, shown in the following code, will be executed in case our tests pass or fail:

private void printTestResults(ITestResult result) {
Reporter.log("Test Method resides in "
+ result.getTestClass().getName(), true);
if (result.getParameters().length != 0) {
String params = null;
for (Object parameter : result.getParameters()) {
params += parameter.toString() + ",";
}
Reporter.log(
"Test Method had the following parameters : " + params,true);
}
String status = null;
switch (result.getStatus()) {
case ITestResult.SUCCESS:
status = "Pass";
break;
case ITestResult.FAILURE:
status = "Failed";
break;
case ITestResult.SKIP:
status = "Skipped";
}
Reporter.log("Test Status: " + status, true);
}
public void beforeInvocation(IInvokedMethod arg0, ITestResult
arg1) {
String textMsg = "About to begin executing following method
: " + returnMethodName(arg0.getTestMethod());
Reporter.log(textMsg, true);
}
public void afterInvocation(IInvokedMethod arg0, ITestResult
arg1) {
String textMsg = "Completed executing following method : "
+ returnMethodName(arg0.getTestMethod());
Reporter.log(textMsg, true);
}
private String returnMethodName(ITestNGMethod method) {
return method.getRealClass().getSimpleName() + "."
+ method.getMethodName();
}
}

Let's now explore another important part of the framework: assertions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset