【问题标题】:Generate reporting by TestNG in PDF通过 TestNG 生成 PDF 报告
【发布时间】:2021-03-12 12:01:03
【问题描述】:

我正在尝试从 TestNG 生成 PDF 格式的报告。在此页面上更改我的听众,但没有发生任何事情。我只收到 .html 和 .xml 报告

也许有人可以帮助我。

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) {
}

【问题讨论】:

    标签: java selenium testng


    【解决方案1】:
    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多