先附上参考链接,Allure官网:https://docs.qameta.io/allure/
其实很简单,但是我被坑了两天,简书,CSDN上的相关资料都查遍了,也试遍了,就是不行。通过查看Allure的源码,发现Allure是实现testNG的监听器接口,ISuiteListener, ITestListener, IInvokedMethodListener2,使用动态代理的方式切入到testNG的整个生命周期,并以反射的方式获取到套件,测试类,测试方法,以及测试参数信息作为报告显示,同时新增特性,丰富了测试报告。其实我们可以参考Allure的实现来自己开发一套更加契合自己所在公司的测试报告框架,源码自行参考如下,就不贴代码了
网上找大多都是下面这种方法,因为低耦合,无侵入性,事实上,我代码中却没有用。我用的第二种。第二种方法绝对管用。
以下内容来自官网
第一种:
import io.qameta.allure.Attachment;
...
@Attachment
public String performedActions(ActionSequence actionSequence) {
return actionSequence.toString();
}
@Attachment(value = "Page screenshot", type = "image/png")
public byte[] saveScreenshot(byte[] screenShot) {
return screenShot;
}
第二种:
import io.qameta.allure.Allure;
...
Allure.addAttachment("My attachment", "My attachment content");
Path content = Paths.get("path-to-my-attachment-contnet");
try (InputStream is = Files.newInputStream(content)) {
Allure.addAttachemnt("My attachment", is);
}
实际代码应用:
截图展示
// 截图
public void takePhotoByAllure(WebDriver driver){
byte[] screenshotAs = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
Allure.addAttachment("截图", new ByteArrayInputStream(screenshotAs));
}
@Test
public void test1() {
try {
String url = "www.baidu.com";
String header = "accept: application/json";
String body = "{t: 1585926656064}";
String response = "{\"status\":200,\"message\":\"success\",\"data\":{\"session\":\"e6530619f00b6e27f58d5c251a9c7b07\",\"id\":\"bc5a637f976db9779cc8ec56dbc5f418\",\"challenge\":\"e6530619f00b6e27f58d5c251a9c7b07\",\"provider\":\"gt\",\"failback\":false},\"timestamp\":\"1585926656203\",\"version\":4}";
caseStep(url,header,body,response);
} catch (Exception e) {
e.printStackTrace();
}
}
//测试步骤
public void caseStep(String url, String header, String body, String response)
{
Allure.addAttachment("请求地址", url);
Allure.addAttachment("请求头", header);
Allure.addAttachment("请求体", body);
Allure.addAttachment("请求响应", response);
}
最终效果图