1.5 reportNg之ReportNGUtils修改

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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值