如何在Allure中添加测试步骤和截图美化测试报告@Attachment

1 篇文章 0 订阅
1 篇文章 0 订阅

先附上参考链接,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);
 	}

最终效果图
在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值