【问题标题】:Create separate report directory on each run using Selenium-Cucumber Maven使用 Selenium-Cucumber Maven 在每次运行时创建单独的报告目录
【发布时间】:2024-05-03 10:50:01
【问题描述】:

我正在使用范围报告在 selenium-cucumber 中生成报告文件。每次运行时,都会通过覆盖先前生成的报告来生成报告。我们如何通过在每次运行时附加时间戳来创建单独的报告文件夹。

给出了我正在使用的 POM 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>selcuc</groupId>
  <artifactId>DemoEurasia</artifactId>
  <version>0.0.1-SNAPSHOT</version>

<properties>
<version.cucumber>3.0.2</version.cucumber>
<timestamp>${maven.build.timestamp}</timestamp>
 <maven.build.timestamp.format>yyyy_MM_dd_HH_mm</maven.build.timestamp.format>
</properties>
 <dependencies>
 <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
 </dependency>

 <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>


<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.5</version>
<scope>provided</scope>
</dependency>

 <dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>

<dependency> 
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>

<dependency> 
    <groupId>info.cukes</groupId> 
    <artifactId>gherkin</artifactId> 
    <version>2.12.2</version> 
</dependency>

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>3.1.5</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>com.vimalselvam</groupId>
    <artifactId>cucumber-extentsreport</artifactId>
    <version>3.1.1</version>
</dependency>

<build>
    <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
               <encoding>UTF-8</encoding>          
            </configuration>
          </plugin> 

          <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.17</version>
    </plugin>                                
    </plugins>
</build>

如果你们能给出答案,那将非常有帮助。提前致谢

【问题讨论】:

    标签: maven selenium junit cucumber-junit selenium-extent-report


    【解决方案1】:

    以下代码将生成当前时间戳的字符串对象(您可以将格式更改为您喜欢的任何格式:

    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
    

    现在,无论您在哪里生成范围报告,都可以在报告名称中传递此字符串。像这样的:

    extent = new ExtentReports (userDir +"\\test-output\\" + timeStamp + ".html", true);
    

    加分:

    您还可以将功能名称添加到您的报告中以获得更好的可访问性。您可以在 @Before 钩子中这样做:

    @Before()
        public void beforeScenario(Scenario scenario)
        {
            String fileName = scenario.getName() + "-" + timeStamp;
            extent = new ExtentReports (userDir + \\test-output\\" + fileName+ ".html", true);
        }
    

    【讨论】:

    • 我已经通过如下更新 TestRunnerTest.java 尝试了这个答案:请纠正我
    • plugin={"com.vimalselvam.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"}) 单色 = true public class TestRunnerTest{ public static String timeStamp=new SimpleDateFormat(" yyyy.MM.dd.HH.mm.ss").format(new Date());私有静态 TestRunnerTest sharedInstance = new TestRunnerTest(); @AfterClass public static void after() {Reporter.loadXMLConfig(new File("config/report.xml")); ExtentReports extent = new ExtentReports (userDir +"\\target\\" + timeStamp + "cucumber-reports/report.html", true);}
    • 但我怀疑我们需要在此声明中作为“userDir”提供什么:ExtentReports extent = new ExtentReports (userDir +"\\target\\" + timeStamp + "cucumber-reports/ report.html", true);} 另外你能解释一下每个参数吗?作为初学者,这对我很有帮助。
    • userDir 只不过是您要存储报告的目录。您必须将其替换为 new ExtentReports ("C:\\someFolder" + fileName + ".html", true);
    • @Milu 如果我的回答对您有帮助,请点击我的回答左侧的空心刻度线接受我的回答 :)
    【解决方案2】:

    很简单!!!!!!

    不要在插件com.cucumber.listener.ExtentCucumberFormatter中提及任何文件夹路径。

    例子:

    plugin = { 
        "pretty", 
        "html:FeaturesReport",
        "html:target/site/cucumber-pretty",
        "json:target/cucumber.json",
    
        "com.cucumber.listener.ExtentCucumberFormatter:",
    },
    
    1. 运行项目并刷新它

    2. 检查报告将在默认文件夹 output/Run_with system time/report.html 中生成

    如果您想在指定路径中生成带有时间戳的报告,只需按照以下步骤操作即可。

    1. 转到 maven 依赖项

    2. 搜索 cucumber-extentsreport.jar

    3. 扩展jar并选择com.cucumber.listener

    4. 复制ExtentProperties类中的整个代码

    5. 在包上并创建名称为ExtentProperties的新枚举

    6. 然后将ExtentProperties类代码粘贴到创建的枚举中

    7. 在下面搜索方法

    ExtentProperties() {
        this.reportPath = "output" + File.separator + "Run_" + System.currentTimeMillis() + File.separator + "report.html";
        this.projectName = "default";
    }
    
    1. 并用以下代码替换
    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
    
    String userDir = System.getProperty("user.dir"); 
    
    ExtentProperties() {
        this.reportPath = "Extent_Reports" + File.separator + "_" + timeStamp.replace(":","_").replace(".","_") + File.separator + "Execution report.html";
        this.projectName = "default";
    }
    
    1. 运行项目然后刷新项目

    2. 检查报告将在指定路径生成,名称为

    Extent_Reports/_2020_06_16_19_14_07/Execution report.html

    如有任何问题,请发表评论

    【讨论】:

      最近更新 更多