获取控制台输出附加到诱惑报告的屏幕截图-selenium webdriver、testng、maven

Get console output an screenshot attached to allure report- selenium webdriver, testng, maven

我正在使用诱惑报告。我的报告生成正确,但我的屏幕截图和控制台输出未附加到报告中。

这是我的 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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.testproject</groupId>
    <artifactId>tests</artifactId>
    <version>0.1-SNAPSHOT</version>
    <properties>
        <aspectj.version>1.8.10</aspectj.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.0-BETA14</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

请查看 的截图 它显示状态 pass/fail/broken 和 运行 测试所用的时间。但是控制台输出没有附加到它。失败测试用例的屏幕截图也存储在文件夹 surefire-reports/screenshots 中。我也需要附加它们以吸引 reports.Can 有人请帮助知道我需要添加什么才能获得此输出?

谢谢!!

我假设您正在使用 TestNG 侦听器获取失败屏幕截图并且您可以访问 testNG 侦听器。否则请参阅如何通过覆盖 TestListenerAdapter 创建 TestNG 侦听器 (http://testng.org/doc/documentation-main.html#listeners-testng-xml)。

  1. 看来你可以创建一个像下面这样的方法来保存截图

    @Attachment(value = "Page screenshot", type = "image/png")
    public byte[] saveScreenshot() {
        // Use selenium screenshot code to take screenshot and convert into     byte[]
        return byte[];
    }
    

    然后您可以在测试失败时用于截取屏幕截图的 testNG 侦听器中调用此方法。

    @Override
    public void onTestFailure(ITestResult testResult) {
        saveScreenshot();
        super.onTestFailure(testResult);
    }
    
  2. 为了让控制台输出报告,创建以下方法

    @Attachment
    public String logOutput(List<String> outputList) {
        String output = ""; 
        for (String o : outputList) 
            output += o + "<br/>"; 
        return output
    }
    

    在testNG监听器中调用onTest(success, failure, skip)方法和onConfiguration(success, failure, skip)方法

    @Override
    public void onTestSuccess(ITestResult testResult) {
        // Reporter.getOutput(testResult)will give the output logged in testng reporter
        logOutput(Reporter.getOutput(testResult));
        super.onTestSuccess(testResult);
    }
    

https://github.com/allure-framework/allure1/wiki/Attachments