Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Selenium by (7k points)

I'm presently working on a Selenium/testng/java/gradle based project with ThreadLocal approach for webdriver and extenttest objects. Whenever the testcase fails, I'm using a RetryListener to rerun failed testcase for another  time. If it is passes for 2nd time, my results are still showing as "Fail" in extent report. I Created a listener which extends TestListenerAdapter and have written the logic in onFinish Method to remove the 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;

    }

}

And unable to proceed further with duplicate test removal.

1 Answer

0 votes
by (31.9k points)

You should delete previous reports, when you rerun selenium script for getting reports.

Want to learn Selenium, checkout our Selenium training.

Browse Categories

...