在java中将屏幕截图添加到自定义HTML报告中

时间:2017-04-14 07:31:21

标签: java html selenium

我正在为我的selenium框架创建自定义HTML报告。现在,当我尝试将屏幕截图链接添加到我的HTML页面时,我得到了“指针异常”#39;有人能指出我哪里出错了。

这是我写入HTML文件的代码。

public static void writeResults(String SummaryHTMLFile) {
        FileOutputStream out; // declare a file o/p object
        PrintStream print = null;   // declare a print stream object

        try {

            out = new FileOutputStream(SummaryHTMLFile);
            print = new PrintStream(out);

            String reportIn = "";
            for (int i = 0; i < details.size();i++) {

                reportIn = "<html><head><title>Automation Execution Results</title>";

                reportIn +="</head><Body>"+
                        "<p align = center><table border=2 bordercolor=#000000 id=table1 width=900 height=31 bordercolorlight=#000000>"+
                        "<tr><td COLSPAN = 6 bgcolor = "+h1color+">";
                reportIn+= "<p align=center><font color="+fontColor+" size=4 face= Copperplate Gothic Bold>"+"Framwork"+" Automation Execution Results </font><font face= Copperplate Gothic Bold></font> </p>";
                reportIn +="</td></tr>"+
                        "<tr>"+
                        "<td COLSPAN = 6 bgcolor = "+h1color+">"+
                        "<p align=justify><b><font color="+fontColor+" size=2 face= Verdana>DATE:"+ currentDate+
                        "</td></tr>";
                reportIn+="<tr bgcolor="+h2color+">"+
                        "<td><b>Step No</b></td>"+
                        "<td><b>Step Name</b></td>"+
                        "<td><b>Description </b></td>"+
                        "<td><b>Status</b>  </td>"+
                        "<td><b>Screen Shot </b></td>"+
                        "</tr>";

                reportIn+="<tr><td>" + Integer.toString(i+1) +"</td>"+
                        "<td>" + details.get(i).getStepName() + "</td>"+
                        "<td>" + details.get(i).getDesc() + "</td>" +
                        "<td>" + details.get(i).getStrStatus()+ "</td>" +
                        "<td><a href=" + new File(details.get(i).getResultScreenshot()).toURI().toURL() + ">screenshot</a></td></tr>" +resultPlaceholder;

            print.println(reportIn);
                print.close();
            }


        } catch (Exception e) {
            System.out.println("Error when writing report file:\n" + e.toString());
        }
    }

这是我获得空指针异常的行。如果删除此行,则会成功生成文件。

"<td><a href=" + new File(details.get(i).getResultScreenshot()).toURI().toURL() + ">screenshot</a></td></tr>" +resultPlaceholder;

编辑:有一个结果类有getter&amp; setters检索结果。

package com.reporting;

public class Result {

    private String result;
    private String resultText;
    private String stepName;
    private String Desc;
    private String strStatus;
    private String resultScreenshot;

    public Result(String resultText,String result) {
        this.result = result;
        this.resultText = resultText;
    }

    public Result(String strStepName,String strDescription,String strStatus, String resultScreenshot) {
        this.setStepName(strStepName);
        this.setDesc(strDescription);
        this.setStrStatus(strStatus);
        this.setResultScreenshot(resultScreenshot);
        //this.resultScreenshot = resultScreenshot;
    }

    public Result(String strStepName,String strDescription,String strStatus) {
        this.setStepName(strStepName);
        this.setDesc(strDescription);
        this.setStrStatus(strStatus);
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getResult() {
        return this.result;
    }

    public void setResultText(String resultText) {
        this.resultText = resultText;
    }

    public String getResultText() {
        return this.resultText;
    }

    public String getStepName() {
        return stepName;
    }

    public void setStepName(String stepName) {
        this.stepName = stepName;
    }

    public String getDesc() {
        return Desc;
    }

    public void setDesc(String desc) {
        Desc = desc;
    }

    public String getStrStatus() {
        return strStatus;
    }

    public void setStrStatus(String strStatus) {
        this.strStatus = strStatus;
    }   
    public void setResultScreenshot(String resultScreenshot) {
        this.resultScreenshot = resultScreenshot;
    }

    public String getResultScreenshot() {
        return this.resultScreenshot;
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的代码,我认为问题出在getResultScreenshot()方法中。它返回null,当你尝试调用toURI()方法时,它会抛出NPE。

您应该检查它返回null的原因,并为这种情况添加处理程序。 Somethind喜欢在getResultScreenshot中放置if以返回空/默认图像url。或者你可以把if检查只在有值时构建截图的链接

例如

if(details.get(i).getResultScreenshot() != null){


     reportIn+="<td><a href=" + new File(details.get(i).getResultScreenshot()).toURI().toURL() + ">screenshot</a></td></tr>" +resultPlaceholder;

}