【问题标题】:Generate Selenium Cucumber reports without overwriting the previous Maven test output result?生成 Selenium Cucumber 报告而不覆盖之前的 Maven 测试输出结果?
【发布时间】:2019-01-27 16:16:12
【问题描述】:

我需要在每次运行时将每个报告文件分别保存在报告目录中。我们如何通过连接当前日期和时间以及文件名来创建报告文件。 我在我的 TestRunnerTest.java 中使用以下代码在 cucumber-selenium 框架中生成 Extentreport。

package testRunner;

import java.io.File;
import java.io.IOException;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.vimalselvam.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(

    features = "src/test/resources"
            , glue= {"stepDefinition"}
    , plugin = { "com.vimalselvam.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"}, 
            monochrome = true
    )
public class TestRunnerTest {
public static WebDriver driver;
private static TestRunnerTest sharedInstance = new TestRunnerTest();

 private TestRunnerTest() { }

 public static TestRunnerTest getInstance() {
        return sharedInstance;
    }

 @BeforeClass
    public static void before() {   

           System.setProperty("webdriver.chrome.driver", 
"E:\\ChromeDriverNew\\chromedriver.exe");
           driver=new ChromeDriver();

           driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);   
    }
 @AfterClass
    public static void after() {

     Reporter.loadXMLConfig(new File("config/report.xml"));  
     driver.quit();
    }
}

这是我的 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>
</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>

谁能回答如何使用 Selenium-Cucumber Maven 框架生成每次执行的报告日志。提前致谢

【问题讨论】:

    标签: java maven selenium junit cucumber


    【解决方案1】:

    您可以实现自己的黄瓜Plugin 来装饰ExtentCucumberFormatter 并使用不同的文件名对其进行初始化。查看现有插件以获取有关如何执行此操作的提示。

    【讨论】:

    • 感谢您的回复。但是请您回答我如何保存每次执行的报告文件而不覆盖之前的报告文件。
    • 通过创建一个每次写入不同文件的插件。
    • 其实我只是一个初学者,我对这个框架有基本的了解。所以你能帮我解决这个问题吗?
    • 那你解决不了这个问题。您可能需要找一位更有经验的同事来帮助您。
    【解决方案2】:

    我不确定您是否有解决方案,但以下有两种方法可以解决。

    1 .将 tetsRunner.java 文件中 plugin 的值设置为 ExtentCucumberFormatter 即,plugin = { "com.cucumber.listener.ExtentCucumberFormatter:"} 并执行 tetscase 。默认情况下,报告将在您的项目目录中的 Output 文件夹下创建(只需从 Project Explorer 刷新您的项目,将生成输出文件夹)

    1. 在@BeforeClass Hooker 中设置如下动态输出文件路径,如下在testRunner 文件中。

      @CucumberOptions(
      features = "src/test/java/Features"
      ,glue= {"StepDefinations"}
      ,plugin = { "com.cucumber.listener.ExtentCucumberFormatter:"}
      ,monochrome = true
      )
      public class testRunner {
      @BeforeClass
      public static void setup() {
      ExtentProperties extentProperties = ExtentProperties.INSTANCE;
      String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
      String userDir =System.getProperty("user.dir"); 
      extentProperties.setReportPath(userDir+ 
                                   "/target/cucumberreports/report_"+timeStamp+".html");
      }
      }
      

    【讨论】:

      【解决方案3】:

      在您的 POM(插件部分)中,您可以执行以下操作:

          <configuration>
               <sourceJsonReportDirectory>${project.build.directory}/cucumber-report</sourceJsonReportDirectory>
               <generatedHtmlReportDirectory>${project.build.directory}/generated-report/cluecumber_report_${maven.build.timestamp}</generatedHtmlReportDirectory>
      </configuration>
      

      使用 Maven.build.timestamp 将生成带有日期戳 (UTM 00:00) 的文件夹

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-19
        • 1970-01-01
        • 1970-01-01
        • 2019-02-11
        • 1970-01-01
        • 1970-01-01
        • 2019-08-07
        • 1970-01-01
        相关资源
        最近更新 更多