Extent Report 4.1.6 & Selenium不显示最新的测试结果。

问题描述 投票:0回答:1

我正在做一个基于Seleniumtestngjavagradle的项目,对webdriver和extenttest对象使用ThreadLocal方法。每当我的测试用例失败时,我就使用RetryListener来重新运行失败的测试用例1次。如果第2次通过了,我的结果还是显示为 "Fail"(注意,所有的迭代都被记录在html报告的单个测试节点中)。在stackoverflow上有很多关于这个问题的讨论(所有的讨论都有点老了)。虽然,我尝试了一些那里的逻辑。但是没有任何帮助。

我创建了一个实现ITestListener的监听器,并在onFinish方法中写了删除重复失败的逻辑。

我的监听器类。

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

public class TestListener extends Reporter implements ITestListener {
    @Override
    public void onFinish(ITestContext context) {
        Iterator<ITestResult> failedTestCases = context.getFailedTests().getAllResults().iterator();
        while (failedTestCases.hasNext()) {
            ITestResult failedTestCase = failedTestCases.next();
            ITestNGMethod method = failedTestCase.getMethod();
            if (context.getFailedTests().getResults(method).size() > 1) {
                System.out.println("failed test case remove as dup:" + failedTestCase.getTestClass().toString());
                failedTestCases.remove();
                extent.removeTest(test);
            } else {
                if (context.getPassedTests().getResults(method).size() > 0) {
                    System.out.println(
                            "failed test case remove as pass retry:" + failedTestCase.getTestClass().toString());
                    failedTestCases.remove();
                }
            }
        }
    }

    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) {

    }

}

我的报告器类:

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");
        }
    }

线程本地类,用于Extent测试

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;
    }

}

我发现我总是从测试监听器中得到失败测试的大小为1。所以,无法继续删除重复测试。

  • Extent Report - 4.1.6
  • Selenium - 3.141.59
  • Testng - 7.1.0

请提供一个关于上述问题的想法。

java selenium-webdriver testng extentreports selenium-extent-report
1个回答
0
投票

当你重新运行Selenium脚本来获取报告时,你应该删除以前的报告

© www.soinside.com 2019 - 2024. All rights reserved.