Allure报告中如何显示报错信息

各位老师,烦请指导如下问题,谢谢~

预期结果:想要在用例信息中看到报错信息

实际没有显示:

代码如下:

package com.ceshiren.advance;

import io.qameta.allure.Allure;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.fail;

public class ErrorScreenTest {

public static WebDriver driver;

// 保存错误截图
public void saveScreenShot() throws IOException {
    //添加时间戳
    LocalDate data = LocalDate.now();
    //保存图片
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenshot,new File("src/test/resources/errorimages/"+data+".png"));
    Allure.addAttachment("picture","image/jpeg",new FileInputStream(
            "src/test/resources/errorimages/"+data+".png"
    ),".png");

}

@Test
void errorScreen() throws IOException {

    try {
        driver = new ChromeDriver();
        driver.get("https://ceshiren.com/c/hogwartsinner/6");
        //错误的定位,产生异常信息
        driver.findElement(By.id("123"));
    } catch (Exception e) {
        saveScreenShot();
        fail(e);
    }

}

}

可以把异常信息写入到日志文件。然后让lallure 去读日志文件

(偷懒)如下修改了一下,这样写会有什么问题吗?

1 个赞

没问题