首页 > 解决方案 > 将base64屏幕截图添加到extendreport 3.1.5不成功

问题描述

我想用 testNG 对所有通过和失败的屏幕截图进行 base64 屏幕截图,下面是我的代码。

私有静态 ExtentReports 范围;私有静态平台平台;private static String reportFileName = "ExtentReports-Version3-Test-Automaton-Report.html"; 私有静态字符串 macPath = System.getProperty("user.dir")+ "/TestReport"; 私有静态字符串 windowsPath = System.getProperty("user.dir")+ "\TestReport"; 私有静态字符串 macReportFileLoc = macPath + "/" + reportFileName; 私有静态字符串 winReportFileLoc = windowsPath + "\" + reportFileName;

public static ExtentReports getInstance() {
    if (extent == null)
        createInstance();
    return extent;
}

//Create an extent report instance
public static ExtentReports createInstance() {
    platform = getCurrentPlatform();
    String fileName = getReportFileLocation(platform);
    ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
    htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
    htmlReporter.config().setChartVisibilityOnOpen(true);
    htmlReporter.config().setTheme(Theme.STANDARD);
    htmlReporter.config().setDocumentTitle(fileName);
    htmlReporter.config().setEncoding("utf-8");
    htmlReporter.config().setReportName(fileName);
    extent = new ExtentReports();
    extent.setSystemInfo("Author", "Gladson Antony");
    extent.setSystemInfo("Browser", Browser);
    extent.setSystemInfo("OS", OSName);     
    extent.setSystemInfo("OS Version", OSVersion);
    extent.setSystemInfo("OS Architecture", OSArchitecture);
    extent.setSystemInfo("OS Bit", OSBit);
    extent.setSystemInfo("JAVA Version",System.getProperty("java.version"));        
    extent.attachReporter(htmlReporter);
    return extent;
}

//Select the extent report file location based on platform
private static String getReportFileLocation (Platform platform) {
    String reportFileLocation = null;
    switch (platform) {
        case MAC:
            reportFileLocation = macReportFileLoc;
            createReportPath(macPath);
            System.out.println("ExtentReport Path for MAC: " + macPath + "\n");
            break;
        case WINDOWS:
            reportFileLocation = winReportFileLoc;
            createReportPath(windowsPath);
            System.out.println("ExtentReport Path for WINDOWS: " + windowsPath + "\n");
            break;
        default:
            System.out.println("ExtentReport path has not been set! There is a problem!\n");
            break;
    }
    return reportFileLocation;
}

//Create the report path if it does not exist
private static void createReportPath (String path) {
    File testDirectory = new File(path);
    if (!testDirectory.exists()) {
        if (testDirectory.mkdir()) {
            System.out.println("Directory: " + path + " is created!" );
        } else {
            System.out.println("Failed to create directory: " + path);
        }
    } else {
        System.out.println("Directory already exists: " + path);
    }
}

//Get current platform
private static Platform getCurrentPlatform () {
    if (platform == null) {
        String operSys = System.getProperty("os.name").toLowerCase();
        if (operSys.contains("win")) {
            platform = Platform.WINDOWS;
        } else if (operSys.contains("nix") || operSys.contains("nux")
                || operSys.contains("aix")) {
            platform = Platform.LINUX;
        } else if (operSys.contains("mac")) {
            platform = Platform.MAC;
        }
    }
    return platform;
}

下面是我的侦听器类,我使用 Base64 格式为每个通过的测试用例截屏。

私有静态 ExtentReports 范围 = ExtentManager.createInstance(); 私有静态 ThreadLocal 测试 = new ThreadLocal();

public synchronized void onStart(ITestContext context) {
    System.out.println("Test Suite started!");
}

public synchronized void onFinish(ITestContext context) {
    System.out.println(("Test Suite is ending!"));
    extent.flush();
}

public synchronized void onTestStart(ITestResult result) {
    System.out.println((result.getMethod().getMethodName() + " started!"));
    ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName(),
            result.getMethod().getDescription());
    test.set(extentTest);
}

public synchronized void onTestSuccess(ITestResult result) {
    System.out.println((result.getMethod().getMethodName() + " passed!"));
    test.get().pass("Test passed");
    try {
        test.get().pass(result.getTestClass().getName() + "." + result.getMethod().getMethodName(),
                MediaEntityBuilder.createScreenCaptureFromBase64String(TestBase.addScreenshot()).build());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public synchronized void onTestFailure(ITestResult result) {
    System.out.println((result.getMethod().getMethodName() + " failed!"));
    test.get().fail(result.getThrowable());
    try {
        test.get().fail(result.getTestClass().getName() + "." + result.getMethod().getMethodName(),
                MediaEntityBuilder.createScreenCaptureFromBase64String(TestBase.addScreenshot()).build());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public synchronized void onTestSkipped(ITestResult result) {
    System.out.println((result.getMethod().getMethodName() + " skipped!"));
    test.get().skip(result.getThrowable());
}

public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    System.out.println(("onTestFailedButWithinSuccessPercentage for " + result.getMethod().getMethodName()));
}

提前感谢您的青睐!!

标签: javacollections

解决方案


推荐阅读