首页 > 解决方案 > 范围报告 4.1.6 和 Selenium 未显示最新的测试结果

问题描述

我正在开发一个基于 Selenium/testng/java/gradle 的项目,该项目使用 ThreadLocal 方法用于 webdriver 和 extenttest 对象。每当我的测试用例失败时,我都会使用 RetryListener 重新运行失败的测试用例 1 次。如果它第二次通过,我的结果仍然在范围报告中显示为“失败”(注意 - 所有迭代都记录在 html 报告中的单个测试节点中)。stackoverflow 中有很多关于此的讨论(所有这些都不太老)。虽然,我从那里尝试了一些逻辑。但这没有帮助。

我创建了一个扩展 TestListenerAdapter 的侦听器,并在 onFinish 方法中编写了逻辑以删除重复失败。

我的听众班:

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

}

我的记者班:

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 类:

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。因此,无法继续进行重复测试删除。

请在上面提供一个想法。

标签: javaselenium-webdrivertestngextentreportsselenium-extent-report

解决方案


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


推荐阅读