I have implemented Custom Listener to capture skipped test cases but its is not capturing them | Selenium Forum
A
avani.garimella Posted on 16/11/2020

I have implemented custom listener to capture skipping of test cases due to dependency on groups:

public void onTestSkipped(ITestResult result) {

System.out.println("-> onTestSkipped: " + result.getName());

ExtentTestManager.getTest().log(LogStatus.SKIP, "Test Skipped",result.getName());

}

package util;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import java.util.HashMap;
import java.util.Map;

/**
* OB: extentTestMap holds the information of thread ids and ExtentTest instances.
* ExtentReports instance created by calling getReporter() method from ExtentManager.
* At startTest() method, an instance of ExtentTest created and put into extentTestMap with current thread id.
* At endTest() method, test ends and ExtentTest instance got from extentTestMap via current thread id.
* At getTest() method, return ExtentTest instance in extentTestMap by using current thread id.
*/
public class ExtentTestManager {
static Map extentTestMap = new HashMap();
static ExtentReports extent = ExtentManager.getInstance();

public static synchronized ExtentTest getTest() {

return (ExtentTest) extentTestMap.get((int) (long) (Thread.currentThread().getId()));
}

public static synchronized void endTest() {
extent.endTest((ExtentTest) extentTestMap.get((int) (long) (Thread.currentThread().getId())));
}

public static synchronized ExtentTest startTest(String testName, String desc) {
ExtentTest test = extent.startTest(testName, desc);
extentTestMap.put((int) (long) (Thread.currentThread().getId()), test);
return test;
}

}

 

This code is printing that the etst is skipped on to teh console but it is not logging into the report and i have no clue why. please help out


A
Ashish Thakur Replied on 19/11/2020

You have to skip the test in extent reports as well.

Have you written the code to skip the test in extent reports as well?


A
avani.garimella Replied on 19/11/2020

Hi Ashish

so where should the code go and what should the code be like. I am a little lost here. could you help


A
Ashish Thakur Replied on 20/11/2020

test.log(Status.SKIP, "Skipping the test");

This can be in test case or listenter

Jut look at testng and project structure videos once


A
avani.garimella Replied on 20/11/2020

So i do have it as part of the listener , still its not capturing it

So what i did now was i went back to the lesson "Passing Parameters into test, Dependencies between tests and groups, Mailing Reports" and got teh code for this lesson which is ProjectStructure and i made the below modifications: Under suiteC package there are two tests TestC and TestCC. So for TestC i changed the group name to abc and made TestCC be dependent on that group abc and i made the TestC deliberately fail so then the TestCC gets skipped . Now what is happening isIt fails TestC and when it comes to TestCC it throws a null pointer exception at the(test.log line in cutomlistener class). The thing is result.getAttribute("reporter")- is returning null so teh test .log throws a null pointer exception

public void onTestSkipped(ITestResult result) {
System.out.println("----Test skipped----");
ExtentTest test = (ExtentTest)result.getAttribute("reporter");
test.log(Status.SKIP, "Test Passed - "+ result.getName());
}

 

java.lang.NullPointerException
at listener.CustomListener.onTestSkipped(CustomListener.java:36)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:61)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1388)
at org.testng.internal.Invoker.registerSkippedTestResult(Invoker.java:1062)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:887)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


A
avani.garimella Replied on 20/11/2020

So when i looked into the code further we are setting the reporter attribute as part of beforemethod annotation. When  a test case is skipped due to dependency the before method is not executed. How to make this work. I badly need a way to capture skipped test cases(due to dependencies) in extent reports


A
avani.garimella Replied on 24/11/2020

ashish and team

i have serached for this scenario accross various platforms but couldnt get a solution. i really need this to work. Everywhere they have done dependsongroups allhave just printed to console but never to a report.


A
Ashish Thakur Replied on 24/11/2020

Sorry for delay

 

1) Are you running code from TestNG.xml

2) Have you registered your listener in testNG.xml. Is your listener extending testnglistneradaptor

3) Is control reaching your listener function? If no then there is config issue in testNG.xml or listener

4) I can see your code base is very different from the one from training. Hope so you have understood the concept from the videos


M
Replied on 25/11/2020

1. I am running the testng.xml

2. yes its is registered

3. yes its reaching the listener function

4. The code base which i posted at the start of the question is different but then i went back to the module and am now using your code base

The code or project am using from your code base is "ProjectStructure" project. I have attached the files which i made modifications to in the attachment add them to your existing project and you will see the below output:

[RemoteTestNG] detected TestNG version 6.14.2
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNG] [WARN] Ignoring duplicate listener : listener.CustomListener
---------------------------------@BeforeMethod
TESTC
Tue Nov 24 22-36-39 PST 2020
eclipse-workspace\ProjectStructure//reports//Tue Nov 24 22-36-39 PST 2020//screenshots
Browser is null
Executing the test in browser null
Starting C null
----Test Failed--------
Failed Test Name - testC
2
null
Result [TestResult name=testC status=FAILURE method=TestC.testC()[pri:0, instance:suitec.TestC@6a01e23] output={null}]
Result java.lang.NullPointerException
----Test Failed--------
Failed Test Name - testC
2
null
Result [TestResult name=testC status=FAILURE method=TestC.testC()[pri:0, instance:suitec.TestC@6a01e23] output={null}]
Result java.lang.NullPointerException
@AfterMethod
----Test skipped----
java.lang.NullPointerException
at listener.CustomListener.onTestSkipped(CustomListener.java:36)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:61)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1388)
at org.testng.internal.Invoker.registerSkippedTestResult(Invoker.java:1062)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:887)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1204)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


A
Ashish Thakur Replied on 25/11/2020

Browser is null
Executing the test in browser null
Starting C null

 

Why are these parameters printing null

Please debug once


A
avani.garimella Replied on 25/11/2020

I wantedly kept those parameters null so that it will make TestC fail which will make the TestCC  skip, which is the objective " skipping of the depedent test case and it getting loggen in the report"


A
Ashish Thakur Replied on 26/11/2020

Can you zip your entire project and send it across


A
avani.garimella Replied on 30/11/2020

attaching the project , please help


A
avani.garimella Replied on 07/12/2020

Still waiting for your solution


A
Ashish Thakur Replied on 14/12/2020

Sorry for late reply.

I have emailed you


A
avani.garimella Replied on 14/12/2020

attaching the project again, could you please try with this


Related Posts