1、ReportNGUtils增加了如下方法
①截图方法
注:
1、该方法会在test-output目录下创建一个img文件夹,用来存储截图
2、方法中会根据被@Test标注的测试方法的Method.name来判断是否有存在的截图,如果有会删除,所以在方法命名时,各个方法名之间不要存在包含与被包含的关系,以免截图引用错误
3、该方法会将生成的图片格式化命名
public String shotScreen(ITestResult result) {
File file1 = null;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
File file2[] = file.listFiles();
for (int i = 0; i < file2.length; i++) {
if (file2[i].getName().startsWith(result.getMethod().getMethodName())) {
file2[i].delete();
}
}
if(result.getStatus()==2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.CHINESE);
String name = result.getMethod().getMethodName() + simpleDateFormat.format(new Date()) + ".png";
file1 = new File(path + "\\" + name);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//获得屏幕高度和宽度
Rectangle screenRectangle = new Rectangle(screenSize);
//指定一个坐标区域
try {
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", file1);
image.flush();
} catch (Exception e) {
e.getCause();
}
}
return file1.getPath();
}
②获取图片方法
注:该方法是获取截图的方法,根据method.name判断
public String getPathImage( ITestNGMethod testNGMethod){
String temp = null;
File file = new File(path);
if(file.isDirectory()) {
File file1[] = file.listFiles();
for (int i = 0; i < file1.length; i++) {
if (file1[i].getName().startsWith(testNGMethod.getMethodName())) {
temp = "img/" + file1[i].getName();
break;
}
}
}
return temp;
}
③增加全局变量
private static final String path = "./test-output/html/img";
④增加格式化时间方法
public String dateFormate(long endMillis){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
date.setTime(endMillis);
return simpleDateFormat.format(date);
}
本文介绍了一个增强版的ReportNGUtils工具类,新增了截图和获取图片的方法。截图方法会在test-output目录下的img文件夹存储截图,并按测试方法名和时间格式化命名。获取图片方法则根据method.name判断,返回图片路径。

被折叠的 条评论
为什么被折叠?



