Allure 未附加到 html 报告

Allure not attaching to html report

我正在尝试将屏幕截图附加到我的魅力报告中,但是我失败了。

    @Test (priority = 1, description="SS1 Verify the login section")
@Description ("Verify that user mngr116116 can logon to the system")
public void login()
{
    page = new BankingLandingPage();
    System.out.println("Test Case One in " + getClass().getSimpleName()
            + " with Thread Id:- " + Thread.currentThread().getId());
    page.enterUser("mngr116116");
    page.enterPass("ytUhUdA");
    page.submitBtn();
    page.saveImageAttach();


}

Pageclass中的saveImageAttach代码如下:

    @Attachment
public  byte[] saveImageAttach() {
    try {
        byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        return screenshot;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[0];
}

我错过了什么?

附上报告截图 enter image description here

可能值得注意的是,我的页面中也有以下方法 class,它很有效:

    public void screenShot(String title) throws Exception {
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    String png = ("src/main/resources/screenshot/" + title + ".png");
    FileUtils.copyFile(scrFile, new File(png));
}

所以这似乎是我如何实施@Attachment

<?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>org.framework</groupId>
    <artifactId>framework</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <aspectj.version>1.8.13</aspectj.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.7</version>
        </dependency>


        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>

            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.5.3</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-firefox-driver</artifactId>
                <version>3.5.3</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.14.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-all</artifactId>
                <version>1.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-testng</artifactId>
                <version>2.0-BETA21</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

    <build>
        <plugins>
            <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                    <configuration>
                        <properties>
                            <argLine>
                                -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                            </argLine>

                            <!-- those are the two default values, you can probably skip them -->
                            <forkCount>7</forkCount>
                            <reuseForks>true</reuseForks>
                            <!-- that's what made it work -->

                            <parallel>classes</parallel>
                            <threadCount>10</threadCount>
                            <includes>
                                <include>src/test/java/testsuite/*class</include>
                            </includes>
                        </properties>
                    </configuration>
                    <dependencies>
                        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjweaver</artifactId>
                            <version>1.8.10</version>
                        </dependency>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-testng</artifactId>
                            <version>2.20.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>

                    </configuration>
                </plugin>
            </plugins>
        </build>

    <reporting>
        <excludeDefaults>true</excludeDefaults>
        <plugins>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>2.8</version>
            </plugin>
        </plugins>
    </reporting>
</project>

添加我的 pom 以防万一

请尝试 运行 以下代码:

@Attachment(value = "screenshot", type = "image/png")
public byte[] saveScreenshotPNG (WebDriver driver) {
    return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
}

在 screenShot(..) 中将屏幕截图保存到文件后,将文件转换为 ByteArray。示例(科特林):

fun returnScreenshot(screenshotPath: String): ByteArray {
    val screenFile = File(screenshotPath)
    val screenFileInBytes = ByteArray(screenFile.length().toInt())
    val fis = FileInputStream(screenFile)
    fis.read(screenFileInBytes)
    fis.close()

    return screenFileInBytes
}

然后 return 此屏幕截图在 ByteArray 中带有 @Attachment。 示例 (java):

public interface ReportInterface {
    String screenshotPath();
    @Attachment(value = "current screenshot", type = "image/png")
  default public byte[] showCurrentScreen() {
    return ScreensHelperKt.returnScreenshot(screenshotPath());
  }

通过此 ReportInterface 实施您的测试 class 并将 screenshotPath() 实施到 return 屏幕截图路径。 在测试主体或拆解中或在测试失败后(使用 testRule)调用 screenShot(String title) 和 showCurrentScreenshot()。但无论如何都要确保 showCurrentScreenshot() 运行正常。

希望对你有帮助。