Listeners_ErrorUtil | Selenium Forum
M
Posted on 31/01/2017
I am trying to learn listeners with the customListeners java file and ErrorUtil java file. Errorutil is supposed to be a bag for collecting errors. However, my test is not failing when i use to ErrorUtil.addVerificationFailure(t) method. I have added a customListener.java file with afterinvocation method and errorUtil file under my project. The below code should fail the test case since the assertion is failing. The output is printed below code. Please refer that the loginTest is passed in my output.

Code:
public void loginTest(){
System.out.println("Start");
try{
Assert.assertEquals(3, 4);
}catch (Throwable t)
{
System.out.println("Error");
// we need to write the listeners code for failing the test and reporting the errors
ErrorUtil.addVerificationFailure(t);
}
System.out.println("end");
}

Output:
Start
Error
end
PASSED: loginTest

Thanks!

M
Replied on 31/01/2017

it will pass here in eclipse. what ErrorUtil java file does is it changes test in reports.


M
Replied on 01/02/2017

As per Mr.Ashish's video the ErrorUtil has to fail the test case in eclipse as well. I have attached a screenshot from Mr.Ashish's video where the same scenario which i have explained in my previous message fails in eclipse. My report also shows that test case is passed.

Thanks
Archana


M
Replied on 02/02/2017

You have to run from testng.xml by keeping listener in it


M
Replied on 03/02/2017

Yes, I do have a testng_Listeners.xml(its just naming different) in my project with the listener tag pointing to CustomListeners java file. Attached the screenshot for your reference.

Thank you!


M
Replied on 05/02/2017

check id error utils file is called properly.


M
Replied on 13/02/2018

I am also facing the same issue. I am running from testng.xml and result passes in eclipse.

Test Case code :

public class ListenerOverirdeMethodsUse1 {

@Test
public void testA(){
System.out.println("testA starts");
System.out.println("testA ends");
}

@Test
public void testB(){
System.out.println("testB starts");
try{
Assert.fail("TestB failed");
}catch(Throwable t){
System.out.println("Error");
ErrorUtil.addVerificationFailure(t);
}
System.out.println("testB ends");
}

@Test//(enabled=false)
public void testC(){
System.out.println("testC starts");
throw new SkipException("TestC Skipped");

}
}

Results are:
[TestNG] Running:
D:\Programming\JavaLearning\SeleniumLearningQTPSelenium\testng.xml

testA starts
testA ends
Passed TC Name is - testA
testB starts
Error
testB ends
Passed TC Name is - testB
testC starts
Skipped TC Name is - testC

===============================================
LearningTestNG
Total tests run: 3, Failures: 0, Skips: 1
===============================================

Listener Class code is as follows:

public class CustomListener extends TestListenerAdapter implements IInvokedMethodListener{

void onTestFailed(ITestResult tr){
System.out.println("Failed TC Name is - "+tr.getName());
}

public void onTestSkipped(ITestResult tr){
System.out.println("Skipped TC Name is - "+tr.getName());
}

public void onTestSuccess(ITestResult tr){
System.out.println("Passed TC Name is - "+tr.getName());
}

public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
// TODO Auto-generated method stub

}

public void afterInvocation(IInvokedMethod method, ITestResult result) {
Reporter.setCurrentTestResult(result);

if (method.isTestMethod()) {
List<Throwable> verificationFailures = ErrorUtil.getVerificationFailures();
//if there are verification failures...
if (verificationFailures.size() != 0) {
//set the test to failed
result.setStatus(ITestResult.FAILURE);

//if there is an assertion failure add it to verificationFailures
if (result.getThrowable() != null) {
verificationFailures.add(result.getThrowable());
}

int size = verificationFailures.size();
//if there's only one failure just set that
if (size == 1) {
result.setThrowable(verificationFailures.get(0));
} else {
//create a failure message with all failures and stack traces (except last failure)
StringBuffer failureMessage = new StringBuffer("Multiple failures (").append(size).append("):nn");
for (int i = 0; i < size-1; i++) {
failureMessage.append("Failure ").append(i+1).append(" of ").append(size).append(":n");
Throwable t = verificationFailures.get(i);
String fullStackTrace = Utils.stackTrace(t, false)[1];
failureMessage.append(fullStackTrace).append("nn");
}

//final failure
Throwable last = verificationFailures.get(size-1);
failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":n");
failureMessage.append(last.toString());

//set merged throwable
Throwable merged = new Throwable(failureMessage.toString());
merged.setStackTrace(last.getStackTrace());

result.setThrowable(merged);

}
}

}

}
}


M
Replied on 13/02/2018

Hello Admin,

After posting my reply here, i took seriously over your suggestion "check id error utils file is called properly." and found that i have one more ErrorUtil file in different package, which i deleted and than came to know that i was imorting and pointing to wrong ErrorUtil class file, which i deleted.
Pointing to correct file did the trick, the Issue is solved now. Thanks and sorry for spamming on this post.

But I still need to understand the logic/implementation of ErrorUtil's methods and afterInvocation method Responsive image

Regards,
Sanjay


M
Replied on 14/02/2018

you want to learn how is ErrorUtil works? if, so you can read the code or debug the code go to errorutil line by line but I would suggest start using extent reports they're much better no need for errorutil file.