当前位置: 首页 > 知识库问答 >
问题:

区段报告只显示testng maven selenium java框架的最后一次运行结果

柳钟展
2023-03-14
mvn test -Dgroups=group1 
  <property>
    <name>listener</name>
    <value>util.listener.TestExtentListener</value>
  </property>


   <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
    <forkCount>2</forkCount>
    <reuseForks>true</reuseForks>
public class ExtentManager {

private static ExtentReports extent;
private static String reportFileName = "Test-Automaton-Report"+".html";
private static String fileSeperator = System.getProperty("file.separator");
private static String reportFilepath = System.getProperty("user.dir") +fileSeperator+ "TestReport";
private static String reportFileLocation =  reportFilepath +fileSeperator+ reportFileName;


public static ExtentReports getInstance() {
    if (extent == null)
        createInstance();
    return extent;
}

//Create an extent report instance
public static ExtentReports createInstance() {
    String fileName = getReportPath(reportFilepath);

    ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
    htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
    htmlReporter.config().setChartVisibilityOnOpen(true);
    htmlReporter.config().setTheme(Theme.STANDARD);
    htmlReporter.config().setDocumentTitle(reportFileName);
    htmlReporter.config().setEncoding("utf-8");
    htmlReporter.config().setReportName(reportFileName);
    htmlReporter.config().setTimeStampFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'");

    extent = new ExtentReports();

    extent.attachReporter(htmlReporter);
    //Set environment details
    extent.setSystemInfo("OS", "Mac");
    extent.setSystemInfo("AUT", "QA");

    return extent;
 }

//Create the report path
private static String getReportPath (String path) {
    File testDirectory = new File(path);
    if (!testDirectory.exists()) {
        if (testDirectory.mkdir()) {
            System.out.println("Directory: " + path + " is created!" );
            return reportFileLocation;
        } else {
            System.out.println("Failed to create directory: " + path);
            return System.getProperty("user.dir");
        }
    } else {
        System.out.println("Directory already exists: " + path);
    }
    return reportFileLocation;
}
public class ExtentTestManager {
static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
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.flush();
}

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

public class TestExtentListener implements ITestListener {

ExtentTest test;
private static ThreadLocal<ExtentTest> extentTestThreadLocal = new ThreadLocal<ExtentTest>();


public void onStart(ITestContext context) {
    System.out.println("*** Test Suite " + context.getName() + " started ***");
}

public void onFinish(ITestContext context) {
    System.out.println(("*** Test Suite " + context.getName() + " ending ***"));
    ExtentTestManager.endTest();
    ExtentManager.getInstance().flush();
}

public void onTestStart(ITestResult result) {
    System.out.println(("*** Running test method " + result.getMethod().getMethodName() + "..."));
    test = ExtentTestManager.startTest(result.getMethod().getMethodName());
    extentTestThreadLocal.set(test);
}

public void onTestSuccess(ITestResult result) {
    System.out.println("*** Executed " + result.getMethod().getMethodName() + " test successfully...");
    extentTestThreadLocal.get().log(Status.PASS, "Test passed");
}

public void onTestFailure(ITestResult result) {
    System.out.println("*** Test execution " + result.getMethod().getMethodName() + " failed...");
    extentTestThreadLocal.get().log(Status.FAIL, "Test Failed");
}

public void onTestSkipped(ITestResult result) {
    System.out.println("*** Test " + result.getMethod().getMethodName() + " skipped...");
    extentTestThreadLocal.get().log(Status.SKIP, "Test Skipped");
}

public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    System.out.println("*** Test failed but within percentage % " + result.getMethod().getMethodName());
}

}

共有1个答案

龙星辰
2023-03-14

您可能没有使用java中的thread local类来使扩展报告线程安全。否则,对象将被重写,而扩展报告只显示活动的测试结果。

您可以这样做:

ExtentReports extent = ExtentReportGenerator.ExtentReport();
    ExtentTest test;;

    private static ThreadLocal<ExtentTest> extent_test = new ThreadLocal<ExtentTest>();

要了解更多的细节,你可以参考这个博客。

 类似资料:
  • 我希望一个进程在我启动我的网络服务后运行,然后每隔30分钟左右运行一次(我现在用较小的延迟测试它,只是为了看看它是否工作),但是我的进程从来不会运行超过一次。我做错了什么? 这是我的密码:

  • 我用的是Jasper reports 6.17,我在Jasper Studio里做了一个列表。 列表中只有两个名为“test1”和“test2”的项目,但只显示了一个,我不知道为什么。生成的PDF仅显示“test2”,为什么缺少“test1”?如果我添加10项,第一行将丢失。 jrxml文件是: java文件是:

  • 我想创建一个底部工作表对话框。看起来像下面的图像在这里输入图像描述 我尝试过许多方法,如SharedPreferences。但它不起作用

  • 本文向大家介绍JS使用cookie实现DIV提示框只显示一次的方法,包括了JS使用cookie实现DIV提示框只显示一次的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JS使用cookie实现DIV提示框只显示一次的方法。分享给大家供大家参考,具体如下: 这里运用JavaScript的cookie技术,控制网页上的提示DIV只显示一次,也就是当用户是第一次打开网页的时候才显示,第二次

  • 问题内容: 问题 我已经将一个长期运行的任务划分为多个逻辑子任务,因此我可以在每个子任务完成时报告结果。但是,我正在尝试报告将永远无法完成的任务的结果(而不是不断产生价值),并且正在使用现有的解决方案来做到这一点。 背景 我正在为我编写的某些Python程序构建Web界面。用户可以通过Web表单提交作业,然后返回查看该作业的进度。 假设我有两个函数,每个函数都可以通过单独的形式进行访问: :执行大

  • ap.alert(OPTION | content, CALLBACK) 显示 alert 警告框。可直接传入一个字符串作为 OPTION.content 参数。 OPTION 参数说明 名称 类型 必填 描述 title String 否 alert框的标题 content String 是 alert框的内容 buttonText String 否 按钮文字,默认’确定’ CALLBACK 参