web_auto用例失敗截圖嵌入到Allure報(bào)表

Allure官網(wǎng):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實(shí)現(xiàn) IHookable接口 重寫IHookable接口提供的run方法public void run(IHookCallBack
 * callBack, ITestResult testResult); 監(jiān)聽用例執(zhí)行的的運(yùn)行狀態(tài)
 * 
 * @author TF
 *
 */
public class AllureReportListener implements IHookable {
    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        // callBack調(diào)用runTestMethod方法,將測試結(jié)果放到了testResult里面
        callBack.runTestMethod(testResult);
        // testResult調(diào)用getThrowable()方法獲取用例的異常執(zhí)行結(jié)果
        // 異常結(jié)果為空,說明沒有異常 ,不為空,說明有異常
        if (testResult.getThrowable() != null) {
            // 有異常,就截圖
            //3.將截圖嵌入到allure報(bào)表
            try {
                saveScreenShot();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * @throws IOException
     * @Attachment是allure提供的注解
     * 
     * 
     * 
     */
    @Attachment
    public byte[] saveScreenShot() throws IOException {
        // 1. 調(diào)用截圖的方法
        // 將異常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類型對象轉(zhuǎn)成byte[] 數(shù)組
        byte[] byteArray = Files.toByteArray(screenshot);

        return byteArray;

    }

}


(2)Step2:
BaseCase類getScreenshot()方法需要return File類型的返回值
(因?yàn)榻貓D嵌入到報(bào)表中,需要File類型的對象)


    public static File getScreenshot( String path) {
        // File sourceScreen = null 從if抽離出來
        // 是因?yàn)榉旁趇f{}里面,作用域只能是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 實(shí)例化File對象->通過自帶的構(gòu)造函數(shù),傳入需要生成的目標(biāo)文件(文件格式,png,jpg,自己定義)
        File targetFile = new File(path);
        // 4 將源文件sourceScreen拷貝到目標(biāo)對象targetFile中,即可完成login.png文件創(chuàng)建
        // FileUtils需要導(dǎo)入依賴 commons-io
        try {
            FileUtils.copyFile(sourceScreen, targetFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return targetFile ;
    }



(3)step3: 項(xiàng)目構(gòu)建

image.png

上圖可以看到截圖成功嵌入到報(bào)表,但是如果我要想知道截圖是來源哪個失敗的測試用例怎么做呢?-----優(yōu)化如下:
(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實(shí)現(xiàn) IHookable接口 重寫IHookable接口提供的run方法public void run(IHookCallBack
 * callBack, ITestResult testResult); 監(jiān)聽用例執(zhí)行的的運(yùn)行狀態(tài)
 * 
 * @author TF
 *
 */
public class AllureReportListener implements IHookable {
    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        // callBack調(diào)用runTestMethod方法,將測試結(jié)果放到了testResult里面
        callBack.runTestMethod(testResult);
        // testResult調(diào)用getThrowable()方法獲取用例的異常執(zhí)行結(jié)果
        // 異常結(jié)果為空,說明沒有異常 ,不為空,說明有異常
        if (testResult.getThrowable() != null) {
            // 有異常,就截圖
            // 3.將截圖嵌入到allure報(bào)表
            // 帶有@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. 調(diào)用截圖的方法
        // 將異常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類型對象轉(zhuǎn)成byte[] 數(shù)組
        byte[] byteArray = Files.toByteArray(screenshot);

        return byteArray;

    }

}


(2)項(xiàng)目重新構(gòu)建

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容