首页 > 解决方案 > 失败场景的黄瓜屏幕截图和生成多个屏幕截图的扩展报告

问题描述

我在处理黄瓜和黄瓜扩展报告时遇到以下问题。我正在使用在场景中的步骤失败时截取屏幕截图并将该屏幕截图嵌入黄瓜详细报告中的方法。该功能运行良好,但当我只想要一个发生故障的页面的屏幕截图时,它会生成多个屏幕截图。如果有人知道如何解决这个问题,请您指点一下。我真的很感激。

提前致谢

这是我的 pom.xml

<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>com.moneris.automation</groupId>
    <artifactId>md01_automation_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>merchant_direct</name>
    <profiles>
        <profile>
            <id>md_01_automation</id>
        </profile>
    </profiles>
    <dependencies>
        <!-- This is what I added for the extended report BEGIN -->
        <dependency>
            <groupId>com.github.mkolisnyk</groupId>
            <artifactId>cucumber-runner</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.mkolisnyk</groupId>
            <artifactId>cucumber-reports</artifactId>
            <version>1.3.3</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>3.8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>1.2.5</version>
        </dependency>

        <dependency>
            <groupId>com.vimalselvam</groupId>
            <artifactId>cucumber-extentsreport</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.41.2</version>
        </dependency>
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.1.5</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-junit</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.7.0.13</version>
            <scope>system</scope>
            <systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>gherkin</artifactId>
            <version>2.12.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.26-incubating</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>

        <!-- This is what I added for the extended report END -->

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

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

        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- It needs this dependency to run TestNG from command line -->
        <dependency>
            <groupId>com.beust</groupId>
            <artifactId>jcommander</artifactId>
            <version>1.72</version>
        </dependency>

        <!-- To take the screenshots of the entire page -->
        <dependency>
            <groupId>ru.yandex.qatools.ashot</groupId>
            <artifactId>ashot</artifactId>
            <version>1.5.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.assertthat/selenium-shutterbug -->
        <dependency>
            <groupId>com.assertthat</groupId>
            <artifactId>selenium-shutterbug</artifactId>
            <version>0.7</version>
        </dependency>

        <!-- Library to compare two text files -->
        <dependency>
            <groupId>com.github.wumpz</groupId>
            <artifactId>diffutils</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.hp.gagawa/gagawa -->
        <dependency>
            <groupId>com.hp.gagawa</groupId>
            <artifactId>gagawa</artifactId>
            <version>1.0.1</version>
        </dependency>


    </dependencies>
    <build>
        <plugins>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptor>src/test/resources/assembly.xml</descriptor>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.moneris.automation.main.Execute</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <fork>true</fork>
                    <executable>C:\Program Files\Java\jdk1.8.0_171\bin\javac.exe</executable>
                    <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.18.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <includes>
                        <include>**/*TestRunner.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.20</version>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>2.0.0</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>test</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>Merchant Direct</projectName>
                            <outputDirectory>${project.build.directory}/site/cucumber-reports</outputDirectory>
                            <cucumberOutput>${project.build.directory}/Extended-Report/cucumber.json</cucumberOutput>
                            <skippedFails>true</skippedFails>
                            <enableFlashCharts>true</enableFlashCharts>
                            <buildNumber>1</buildNumber>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这是我的跑步者课

package com.moneris.automation.runner;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.testng.annotations.AfterSuite;

import com.github.mkolisnyk.cucumber.reporting.CucumberDetailedResults;
import com.github.mkolisnyk.cucumber.reporting.CucumberResultsOverview;
import com.github.mkolisnyk.cucumber.runner.BeforeSuite;
import com.github.mkolisnyk.cucumber.runner.ExtendedCucumberOptions;
import com.github.mkolisnyk.cucumber.runner.ExtendedTestNGRunner;
import com.moneris.automation.counter.HelperClass;
import com.moneris.automation.stepsDefinitions.Drivers;
import com.moneris.automation.utilities.ConfigUtility;

import cucumber.api.CucumberOptions;

    @ExtendedCucumberOptions(jsonReport = "target/Extended-Report/cucumber.json",
    retryCount = 0,
    detailedReport = true,
    detailedAggregatedReport = true,
    overviewReport = true,
    coverageReport = true,
    featureOverviewChart = true,
    overviewChartsReport = true,
    jsonUsageReport = "target/Extended-Report/cucumber-usage.json",
    usageReport = true,
    systemInfoReport = true,
    toPDF = true,
    screenShotLocation="./../",
    screenShotSize = "85%",
    outputFolder = "target/Extended-Report")
    @CucumberOptions(
                strict=true,
                features = "resources/features",
                glue = {"com.moneris.automation.stepsDefinitions"},
                plugin = { 
                        "pretty:STDOUT","html:target/reports/cucumber-pretty",
                        "json:target/Extended-Report/cucumber.json",
                        "usage:target/Extended-Report/cucumber-usage.json"
                        }
                ,
                monochrome = true

                )

public class TestRunner extends ExtendedTestNGRunner  {

    @org.testng.annotations.AfterClass
    public static void setup() throws IOException {

        Drivers.getDriver().quit();

        if (Drivers.browser.equals("chrome"))
            Runtime.getRuntime().exec("taskkill /F /IM ChromeDriver.exe");

    }

    @BeforeSuite
    public void generateDirectories() {

        ConfigUtility.prop();
        String resultPath = ConfigUtility.get("resultPath");

        File dirs = new File(resultPath);
        dirs.mkdirs();


        Date date = new Date();
        String browserFolderName = new SimpleDateFormat("yyyyMMdd_HH.mm.ss")
                .format(date)+
                "_"+StringUtils
                .capitalize(
                Drivers.browser);
        File browserDir = new File(resultPath+"\\"+browserFolderName);
        browserDir.mkdirs();

        HelperClass fc = HelperClass.getInstance();
        fc.browserFolderName = browserFolderName;
    }

    @AfterSuite
    public void generateReport() {

        ConfigUtility.prop();
        String resultPath = ConfigUtility.get("resultPath");

        HelperClass hc = HelperClass.getInstance();

        CucumberResultsOverview results = new CucumberResultsOverview();
        results.setOutputDirectory(resultPath+"/"+hc.browserFolderName);
        results.setOutputName("cucumber-results-overview");
        results.setSourceFile("target/Extended-Report/cucumber.json");
        try {
            results.execute();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        CucumberDetailedResults detailedResult = new CucumberDetailedResults();
        detailedResult.setOutputDirectory(resultPath+"/"+hc.browserFolderName);
        detailedResult.setOutputName("cucumber-detailed");
        detailedResult.setSourceFile("target/Extended-Report/cucumber.json");
        detailedResult.setScreenShotLocation("./Error_Screenshots/");
        detailedResult.setScreenShotWidth("100%");

        try {
            detailedResult.execute();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

这是我用来截屏失败的代码

@After
public void tearDown(Scenario scenario) {

        if (Drivers.getDriver() == null || Drivers.getDriver().toString().contains("(null)"))
            return;
        if (scenario.isFailed() || scenario.getStatus().equalsIgnoreCase("failed")) {
            final byte[] screenshot = ((TakesScreenshot) Drivers.getDriver()).getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png");

        }
    }

这是报告的外观。我只想要一张截图。

标签: cucumberreport

解决方案


推荐阅读