通过 TestNG 生成 PDF 报告

时间:2021-03-12 12:01:03

标签: java selenium testng

我正在尝试从 TestNG 生成 PDF 格式的报告。像在此页面上一样更改我的侦听器,但什么也没发生。我只收到 .html 和 .xml 报告

enter image description here

也许有人可以帮我解决这个问题。

https://www.guru99.com/pdf-emails-and-screenshot-of-test-reports-in-selenium.html

@Override
public void onTestStart(ITestResult result) {

}

@Override
public void onTestSuccess(ITestResult result) {
    PdfPTable failTest = new PdfPTable(new float[]{.3f, .3f, .1f, .3f});
    failTest.setTotalWidth(20f);
    Paragraph p = new Paragraph("Passed test", new Font(Font.FontFamily.UNDEFINED, Font.DEFAULTSIZE, Font.BOLD));
    p.setAlignment(Element.ALIGN_CENTER);
    PdfPCell cell = new PdfPCell(p);
    cell.setColspan(4);
}

@Override
public void onTestFailure(ITestResult result) {

    PdfPTable failTest = new PdfPTable(new float[]{.3f, .3f, .1f, .3f});
    failTest.setTotalWidth(20f);
    Paragraph p = new Paragraph("Faild test", new Font(Font.FontFamily.UNDEFINED, Font.DEFAULTSIZE, Font.BOLD));
    p.setAlignment(Element.ALIGN_CENTER);
    PdfPCell cell = new PdfPCell(p);
    cell.setColspan(4);
}

@Override
public void onTestSkipped(ITestResult result) {
   
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

}

@Override
public void onStart(ITestContext context) {
    String logMessage = String.format("\n%s STARTED (%s)", context.getName(), context.getSuite().getName());
    System.out.println(logMessage);
    writeLog(logMessage);
}

@Override
public void onFinish(ITestContext context) {
}

1 个答案:

答案 0 :(得分:0)

1. Add Below code in onTestSuccess() and onTestFailure() methods
       failTest.addCell(cell);
        try {
            this.document.add(failTest);
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
2. Add Below code in onStart() method:

        try {
         PdfWriter.getInstance(this.document, 
           new FileOutputStream(context.getName() + ".pdf"));
        } catch (Exception e) {
         e.printStackTrace();
        }
        this.document.open();

 3. Add below code in onFinish() method

        this.document.close();