Sangeetha
Sangeetha

Reputation: 31

Extent Report 4.1.6 & Selenium not showing latest Test Result

I'm working on a Selenium/testng/java/gradle based project with ThreadLocal approach for webdriver and extenttest objects. Whenever my testcase fails, i am using a RetryListener to rerun the failed testcase for another 1 time. If it is passing for 2nd time, still my results are showing as "Fail" in extent report (Note-All iterations are logged in single test node in html report). There are a lot of discussion in stackoverflow about this (All off them are little old). Though, i tried some of logics from there. But it didn't help.

I Created a listener that extends TestListenerAdapter and wrote the logic in onFinish Method to remove duplicate fails.

My Listener Class:

import java.util.Iterator;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;


public class TestListener  extends TestListenerAdapter {
    private static ExtentReports extent;
    public ExtentTest test;
    public TestListener() {
        
        this.test = CustomExtentTest.getInstance().getExtentTest();
        
    }
    
    @Override
    public void onFinish(ITestContext context) {
        Iterator<ITestResult> skippedTestCases = context.getSkippedTests().getAllResults().iterator();
        while (skippedTestCases.hasNext()) {
            ITestResult skippedTestCase = skippedTestCases.next();
            ITestNGMethod method = skippedTestCase.getMethod();
            if (context.getSkippedTests().getResults(method).size() > 0) {
                System.out.println("Removing:" + skippedTestCase.getTestClass().toString());
                skippedTestCases.remove();
                extent.removeTest(test);
            }
        }
    }

    public void onTestStart(ITestResult result) { 
        
        
    }

    public void onTestSuccess(ITestResult result) { 
        
        
    }

    public void onTestFailure(ITestResult result) {  
        

        
    }

    public void onTestSkipped(ITestResult result) {
        
    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {   }

    public void onStart(ITestContext context) {
        
    }

}

My Reporter class:

public abstract class Reporter{

        public RemoteWebDriver driver;
        public static ExtentReports extent;
        public  ExtentTest test;
        public String testcaseName, testcaseDec, author ; 
        public String category;
        private Logger log=Logger.getLogger(Reporter.class);
        
        @BeforeSuite (alwaysRun = true)
        public void startReport(ITestContext c) throws IOException 
        {
            String timeStamp = new SimpleDateFormat("MM.dd.yyyy.HH.mm.ss").format(new Date());
            System.setProperty("org.freemarker.loggerLibrary", "none");
            try {
                Properties prop=new Properties();
                prop.load(new FileInputStream("./Resources/log4j.properties"));
                PropertyConfigurator.configure(prop);
                } catch (Exception e) {
                log.error(e);
            }
            log.debug("Configuring Extent Report...");
            ExtentSparkReporter reporter;
            String extentreportpath;
            String reportName=this.getClass().getName().substring(29, 33).toUpperCase() +" - Test Report";
            String suiteName = c.getCurrentXmlTest().getSuite().getName()+"-Test Report";
            if (suiteName.contains("Default suite")||suiteName.contains("Failed suite"))
            {
                suiteName =reportName;
            }
            extentreportpath="./reports/"+suiteName+"_"+timeStamp+".html";
            reporter = new ExtentSparkReporter(extentreportpath);
            extent   = new ExtentReports(); 
            extent.attachReporter(reporter);
            extent.setSystemInfo("URL", ReadPropertyFile.getInstance().getPropertyValue("URL"));
            reporter.loadXMLConfig("./Resources/extent-config.xml");
            reporter.config().setTheme(Theme.DARK);
            reporter.config().setReportName(suiteName);
            log.info("Extent Report Configured Successfully");
        }

        @Parameters({"browser"})
        @BeforeClass(alwaysRun = true)
        public ExtentTest report(@Optional("browser") String browser)  
        {
            if(ReadPropertyFile.getInstance().getPropertyValue("RunMode").equalsIgnoreCase("STANDALONE"))
            {
                if(browser.equals("browser")) {
                    browser = ReadPropertyFile.getInstance().getPropertyValue("Browser");
                }
            }
            test = extent.createTest(testcaseName, testcaseDec +" <br /><br />Browser Name: "+browser+" <br /><br />Category: "+category);
            test.assignAuthor(author);
            CustomExtentTest extenttst = CustomExtentTest.getInstance();
            extenttst.setExtentTest(test);
            return test;  
        }



        public abstract long takeSnap();
        
        public void reportStep(String desc,String status,boolean bSnap)
        {
            MediaEntityModelProvider img=null;
            if(bSnap && !status.equalsIgnoreCase("INFO"))
            {
                long snapNumber=100000L;
                snapNumber=takeSnap();
                try
                {
                    img=MediaEntityBuilder.createScreenCaptureFromPath("images/"+snapNumber+".jpg").build();
                }
                catch(IOException e)
                {
                    log.error(e);
                }
            }
            if(status.equalsIgnoreCase("pass"))
            {
                test.log(Status.PASS, desc, img);
            }
            else if(status.equalsIgnoreCase("fail"))
            {
                test.log(Status.FAIL, desc, img);
            }
            else if(status.equalsIgnoreCase("INFO"))
            {
                test.log(Status.INFO, desc,img);
            }
        }

        public void reportStep(String desc,String status)
        {

            reportStep(desc,status,true);
        }


        @AfterSuite (alwaysRun=true )
        public void stopReport() 
        {
            log.debug("Stopping and preparing the report...");
            extent.flush();
            log.info("Report prepared successfully");
        }
    }

ThreadLocal class for Extent Test:

public class CustomExtentTest {
    
    private CustomExtentTest() {        
    }
    
    private static final ThreadLocal<CustomExtentTest> _localStorage = new ThreadLocal<CustomExtentTest>(){
        protected CustomExtentTest initialValue() {
          return new CustomExtentTest();
       }
      };
    
    public static CustomExtentTest getInstance() {
        
        return _localStorage.get();
    }
    
    ExtentTest testextent;

    public ExtentTest getExtentTest() {
        return this.testextent;
    }

    public void setExtentTest(ExtentTest testextent) {
        this.testextent = testextent;
    }

}

I just found that i am always getting size of failed test as 1 from the test listener. So, Unable to proceed further with duplicate test removal.

Kindly provide an idea on above.

Upvotes: 0

Views: 4582

Answers (1)

Justin Lambert
Justin Lambert

Reputation: 978

When you rerun selenium script for getting reports you should delete previous reports

Upvotes: 0

Related Questions