美文网首页
web_auto用例失败截图嵌入到Allure报表

web_auto用例失败截图嵌入到Allure报表

作者: testerPM | 来源:发表于2020-02-16 15:05 被阅读0次

Allure官网:https://docs.qameta.io/allure/

(1)step1:
AllureReportListener这个类需要再加一个saveScreenShot()方法

package com.lemon.listener;

import java.io.File;
import java.io.IOException;

import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;

import com.google.common.io.Files;
import com.lemon.common.BaseCases;

import io.qameta.allure.Attachment;

/**
 * implements实现 IHookable接口 重写IHookable接口提供的run方法public void run(IHookCallBack
 * callBack, ITestResult testResult); 监听用例执行的的运行状态
 * 
 * @author TF
 *
 */
public class AllureReportListener implements IHookable {
    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        // callBack调用runTestMethod方法,将测试结果放到了testResult里面
        callBack.runTestMethod(testResult);
        // testResult调用getThrowable()方法获取用例的异常执行结果
        // 异常结果为空,说明没有异常 ,不为空,说明有异常
        if (testResult.getThrowable() != null) {
            // 有异常,就截图
            //3.将截图嵌入到allure报表
            try {
                saveScreenShot();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * @throws IOException
     * @Attachment是allure提供的注解
     * 
     * 
     * 
     */
    @Attachment
    public byte[] saveScreenShot() throws IOException {
        // 1. 调用截图的方法
        // 将异常case的截图存在这个路径下System.getProperty("user.dir")+"\\target\\screenShot\\
        // 图片名字:System.currentTimeMillis().png
        File screenshot = BaseCases.getScreenshot(
                System.getProperty("user.dir") + "\\target\\screenShot\\" + System.currentTimeMillis() + ".png");
        // 2.Files.toByteArray(screenshot)将 File类型对象转成byte[] 数组
        byte[] byteArray = Files.toByteArray(screenshot);

        return byteArray;

    }

}


(2)Step2:
BaseCase类getScreenshot()方法需要return File类型的返回值
(因为截图嵌入到报表中,需要File类型的对象)


    public static File getScreenshot( String path) {
        // File sourceScreen = null 从if抽离出来
        // 是因为放在if{}里面,作用域只能是if方法体,if{}外面 找不到
        // FileUtils.copyFile(sourceScreen, targetFile)会找不到sourceScreen
        File sourceScreen = null;
        if (browserName.equals("chrome")) {
            ChromeDriver chromeDriver = (ChromeDriver) driver;
            sourceScreen = chromeDriver.getScreenshotAs(OutputType.FILE);

        } else if (browserName.equals("firefox")) {
            FirefoxDriver FirefoxDriver = (FirefoxDriver) driver;
            sourceScreen = FirefoxDriver.getScreenshotAs(OutputType.FILE);
        } else if (browserName.equals("ie")) {
            InternetExplorerDriver ieDriver = (InternetExplorerDriver) driver;
            sourceScreen = ieDriver.getScreenshotAs(OutputType.FILE);
        }
        // 3 实例化File对象->通过自带的构造函数,传入需要生成的目标文件(文件格式,png,jpg,自己定义)
        File targetFile = new File(path);
        // 4 将源文件sourceScreen拷贝到目标对象targetFile中,即可完成login.png文件创建
        // FileUtils需要导入依赖 commons-io
        try {
            FileUtils.copyFile(sourceScreen, targetFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return targetFile ;
    }



(3)step3: 项目构建

image.png

上图可以看到截图成功嵌入到报表,但是如果我要想知道截图是来源哪个失败的测试用例怎么做呢?-----优化如下:
(1)AllureReportListener类
1)注解@Attachment(value = "Failure on method{0}", type = "image/png")添加value和type熟悉
2)saveScreenShot(testResult.getMethod().getMethodName());
testResult.getMethod().getMethodName()获取异常case的测试方法名methodName


package com.lemon.listener;

import java.io.File;
import java.io.IOException;

import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;

import com.google.common.io.Files;
import com.lemon.common.BaseCases;

import io.qameta.allure.Attachment;

/**
 * implements实现 IHookable接口 重写IHookable接口提供的run方法public void run(IHookCallBack
 * callBack, ITestResult testResult); 监听用例执行的的运行状态
 * 
 * @author TF
 *
 */
public class AllureReportListener implements IHookable {
    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        // callBack调用runTestMethod方法,将测试结果放到了testResult里面
        callBack.runTestMethod(testResult);
        // testResult调用getThrowable()方法获取用例的异常执行结果
        // 异常结果为空,说明没有异常 ,不为空,说明有异常
        if (testResult.getThrowable() != null) {
            // 有异常,就截图
            // 3.将截图嵌入到allure报表
            // 带有@Attachment注解的方法的返回值即为要上传的附件
            try {
                // testResult.getMethod().getMethodName()获取异常case的测试方法名methodName
                saveScreenShot(testResult.getMethod().getMethodName());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * @throws IOException
     * @Attachment是allure提供的注解 
     * value="Failure on method{0}"
     * Failure on method这个可以自己定义名字
     * {0}-->
     * type="image/png要上传的文件是图片类型且格式为png
     * 
     * 
     */
    @Attachment(value = "Failure on method{0}", type = "image/png")
    public byte[] saveScreenShot(String methodName) throws IOException {
        // 1. 调用截图的方法
        // 将异常case的截图存在这个路径下System.getProperty("user.dir")+"\\target\\screenShot\\
        // 图片名字:System.currentTimeMillis().png
        File screenshot = BaseCases.getScreenshot(
                System.getProperty("user.dir") + "\\target\\screenShot\\" + System.currentTimeMillis() + ".png");
        // 2.Files.toByteArray(screenshot)将 File类型对象转成byte[] 数组
        byte[] byteArray = Files.toByteArray(screenshot);

        return byteArray;

    }

}


(2)项目重新构建

image.png

相关文章

网友评论

      本文标题:web_auto用例失败截图嵌入到Allure报表

      本文链接:https://www.haomeiwen.com/subject/amwpfhtx.html