automation.library.reporting.PDFReport Maven / Gradle / Ivy
Show all versions of library-reporting-extent Show documentation
package automation.library.reporting;
import automation.library.common.Property;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.property.AreaBreakType;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import org.apache.logging.log4j.LogManager;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.*;
import java.util.List;
import org.apache.logging.log4j.Logger;
/**
* This is helper class to generate the PDF report so that it can be used to upload as in evidence in the
* jira or any other project/test management tool.
* This only supports ExtentReport 4.
* Parser checks for the following tags in defined in the Feature file:
* \@CucumberScenario:[some unique ID to identify the scenario and this use in naming the PDF file] e.g. @CucumberScenario:ID1
* \@TMSIssue:[Just for reference if this scenario is testing and specific user story or functionality] e.g. @TMSIssue:ICI-969
* Generated Scenario report - ExtentHtml_[YYYYMMDD]_[HHMMSS]_ICI-969_ID1
*
* if these ar tags or not found then report will be generated by appending the number
* Generated Scenario report - ExtentHtml_[YYYYMMDD]_[HHMMSS]_Sce1
* Generated Scenario report - ExtentHtml_[YYYYMMDD]_[HHMMSS]_Sce2 etc.
*
* PDF report prefix - E_Pass, E_Skip or E_Fail
* PDF report Suffix - depends on
* adding the pdf report name in the Extent Report as div tag with id=pdfReport
* or cucumberScenario tag in the feature file
* or generic _SceN, where is N is incremental
*/
public class PDFReport {
private static final String serviceUserName = "Pipeline Execution";
protected static Logger log = LogManager.getLogger(PDFReport.class);
@Test
public void temp() throws IOException {
String report = "C:\\Users\\anshul.j.jain\\Documents\\git\\Intient\\test-automation-libs\\library-reporting\\RunReports\\";
String rep = "ExtentHtmlIOS.html"; //Scenario Outline
bddScenarioPDFReport(report + rep);
bddscenarioOutLinePDFReport2(report + rep);
}
public static void scenarioPDFReport(String reportname) throws IOException {
bddScenarioPDFReport(reportname);
bddscenarioOutLinePDFReport2(reportname);
}
/**
* @param reportname -path for Extent Report. Only extent 4 supported. If is expected that feature file will have
* \@CucumberScenario:[some unique ID for the scenario]
* @throws IOException file handling exceptions
*/
public static void bddScenarioPDFReport(String reportname) throws IOException {
//getting the Extent HTML Report -
org.jsoup.nodes.Document htmlFile = Jsoup.parse(new File(reportname), "ISO-8859-1");
//get the Feature name for the scenarios
Elements eF = htmlFile.getElementsByClass("linked");
Map testID_FeatureName = new LinkedHashMap();
for (Element e : eF) {
String test_id = e.attr("test-id");
String featureName = e.text().trim().split("\\.")[0];
testID_FeatureName.put(test_id, featureName);
}
//get the list if all the Gherkin Scenarios
Elements eScenarios = htmlFile.getElementsByClass("scenario node");
int seq = 1;
for (Element eScenario : eScenarios) {
//get the 'TMSIssueId:<> and CucumberScenario:<> tags. These will be used to update the generate the pdf reports.
Elements eTags = eScenario.getElementsByClass("category label");
Map tags = new HashMap<>();
ArrayList tagList = new ArrayList();
for (Element eTag : eTags) {
String tag = eTag.text().trim();
tagList.add(tag);
String[] t = tag.split(":");
if (t.length > 1) {
tags.put(t[0].substring(1), t[1]);
}
}
String sceStatus = eScenario.attr("status");
String pdfReportPrefix = getReportPrefix(sceStatus);
String pdfReport;
if (tags.containsKey("CucumberScenario")) { //PDF report based of Feaure Scenario Tag value
pdfReport = reportname
.replace(".html", "_" + tags.get("CucumberScenario") + ".pdf")
.replace("ExtentHtml", pdfReportPrefix);
} else { // i.e. @CucumberScenario tag not in the Feature file either check if div element with id=pdfReport
String pdfReportSuffix = getReportSuffix(eScenario);
if (pdfReportSuffix.equalsIgnoreCase("")) {
pdfReport = reportname
.replace(".html", "_Sce" + seq + ".pdf")
.replace("ExtentHtml", pdfReportPrefix);
seq++;
} else {
pdfReport = reportname
.replace(".html", pdfReportSuffix + ".pdf")
.replace("ExtentHtml", pdfReportPrefix);
}
}
log.info("Generating PDF report: " + pdfReport);
PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
PdfWriter writer = new PdfWriter(pdfReport);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.A4.rotate());
/*
Setting the Header Page -
Report Name - Application, Executed By, Test Envrionment, Start, End, Time takes
Header Page Table 1
*/
document.add(new Paragraph("Test Execution Report").setFont(font).setFontSize(60).setBold().setTextAlignment(TextAlignment.CENTER));
String zephyrId = Property.getVariable("TMSIssueId");
if (zephyrId != null) {
document.add(new Paragraph("TMS Issue ID: " + zephyrId)
.setFont(font).setFontSize(40).setBold().setTextAlignment(TextAlignment.CENTER));
}
if (tags.containsKey("CucumberScenario")) {
document.add(new Paragraph("Cucumber Scenario : " + tags.get("CucumberScenario"))
.setFont(font).setFontSize(25).setBold().setTextAlignment(TextAlignment.CENTER));
}
document.add(getHeaderPageTable(eScenario, reportname, tagList, font));
document.add(new Paragraph("\n"));
document.add(getTechStackTable(eScenario, font));
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
document.add(getScenarioDetailTable(eScenario, testID_FeatureName, sceStatus, font));
//get the scenario steps
Elements eSteps = eScenario.getElementsByTag("ul").get(0).getElementsByTag("li");
//Table 2 - Scenario with scenario steps
Table table2 = new Table(new float[]{50, 1});
table2.setWidth(UnitValue.createPercentValue(100));
table2.setFixedLayout();
for (Element eStep : eSteps) {
String stepStatus = eStep.attr("status");
if (!stepStatus.equalsIgnoreCase("")) {
String stepText = replaceText(eStep.getElementsByClass("step-name").get(0).text().trim(), stepStatus); //Extent 4
Paragraph para = new Paragraph().setFont(font);
para.add(stepText);
//look for steplog //Extent 4
Elements stepLogs = eStep.getElementsByClass("node-step");
for (Element stepLog : stepLogs) {
Elements tbody = stepLog.getElementsByTag("tbody");
String str = stepLog.text();
if (stepText.contains("And a request body") && str.startsWith("{") && !str.contains("Step skipped")) {
//str is json body
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic();
} else if (stepText.contains("And trace out request response") && str.startsWith("response: {")) {
para.add("\nresponse:").setItalic();
str = str.replace("response: ", "");
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic().setFontColor(ColorConstants.DARK_GRAY);
} else {
if (tbody.size() > 0) {
Table stepLogTable = htmlTableToPDFTable(tbody.get(0));
para.add("\n");
para.add(stepLogTable).setItalic().setFontColor(ColorConstants.DARK_GRAY);
} else {
while (str.length() > 95) {
String subStr = str.substring(0, 95);
para.add("\n" + subStr).setItalic().setFontColor(ColorConstants.DARK_GRAY);
str = str.substring(95);
}
para.add("\n" + str.trim()).setItalic().setFontColor(ColorConstants.DARK_GRAY);
}
}
}
//Extent 4
if (stepStatus.equalsIgnoreCase("fail")) {
String errorMsg = eStep.getElementsByTag("textarea").text();
para.add("\n\n" + errorMsg).setItalic().setFontColor(ColorConstants.RED).setFontSize(8);
}
//step
table2.addCell(new Cell().add(para).setFont(font));
if (stepStatus.equalsIgnoreCase("pass"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (stepStatus.equalsIgnoreCase("fail"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (stepStatus.equalsIgnoreCase("skip"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
//add image
try {
Elements eImgs = eStep.getElementsByTag("a");
addImage(table2, eImgs);
} catch (Exception e) {
}
}
}
//adding data to PDF :
document.add(table2);
document.flush();
document.close();
}
}
public static Table addImage(Table table2, Elements eImgs) throws MalformedURLException {
for (Element eImg : eImgs) {
String imgFile = eImg.attr("href");
imgFile = "RunReports" + imgFile.substring(1);
ImageData data = ImageDataFactory.create(imgFile);
Image image = new Image(data);
image.setAutoScale(true);
// image.setAutoScaleHeight(true);
// image.scaleToFit(PageSize.A4.rotate().getHeight(), PageSize.A4.rotate().getWidth());
Cell c = new Cell();
c.setMinHeight(400f);
// c.setMaxHeight(100f);
c.add(image);
table2.addCell(c);
table2.addCell(new Cell().add(new Paragraph().add("")));
}
return table2;
}
private static Table getScenarioDetailTable(Element eScenario, Map testID_FeatureName, String sceStatus, PdfFont font) {
Table table = new Table(new float[]{1, 8});
table.setWidth(UnitValue.createPercentValue(100));
table.setFixedLayout();
String sceTestId = eScenario.attr("test-id");
String scenarioName = replaceText(eScenario.getElementsByClass("scenario-name").get(0).text().trim(), sceStatus); //Extent 4
//row for feature name
table.addCell(new Cell().add(new Paragraph("Feature : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
table.addCell(new Cell().add(new Paragraph(testID_FeatureName.get(sceTestId))).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
System.out.println(testID_FeatureName.get(sceTestId));
System.out.println(scenarioName);
//row for scenario name
table.addCell(new Cell().add(new Paragraph("Scenario : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table.addCell(new Cell().add(new Paragraph(scenarioName)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario Start DateTime
String startTime = eScenario.getElementById("sceStartTime").text();
table.addCell(new Cell().add(new Paragraph("Start : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table.addCell(new Cell().add(new Paragraph(startTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario End DateTime
String endTime = eScenario.getElementById("sceEndTime").text();
table.addCell(new Cell().add(new Paragraph("End : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table.addCell(new Cell().add(new Paragraph(endTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//Scenario status row
table.addCell(new Cell().add(new Paragraph("status : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table.addCell(colouredStatusCell(sceStatus));
return table;
}
public static void bddscenarioOutLinePDFReport2(String reportname) throws IOException {
//getting the Extent HTML Report -
org.jsoup.nodes.Document htmlFile = Jsoup.parse(new File(reportname), "ISO-8859-1");
//get the Feature name for the scenarios
Elements eF = htmlFile.getElementsByClass("linked");
Map testID_FeatureName = new LinkedHashMap();
for (Element e : eF) {
String test_id = e.attr("test-id");
String featureName = e.text().trim().split("\\.")[0];
testID_FeatureName.put(test_id, featureName);
}
//get the Gherkin Scenario Outline
Elements eScenarioOutlines = htmlFile.getElementsByClass("scenariooutline node");
for (Element eScenarioOutline : eScenarioOutlines) {
Elements eTags = eScenarioOutline.getElementsByClass("category label");
Map tags = new HashMap<>();
ArrayList tagList = new ArrayList();
for (Element eTag : eTags) {
String tag = eTag.text().trim();
tagList.add(tag);
String[] t = tag.split(":");
if (t.length > 1) {
tags.put(t[0].substring(1), t[1]);
}
}
//Scenario outline
String sceOutTestId = eScenarioOutline.attr("test-id");
String sceOutLineStatus = eScenarioOutline.attr("status");
String scenarioOutlineName = replaceText(
eScenarioOutline
.getElementsByClass("scenario-name")
.get(0).text().trim(),
sceOutLineStatus
);
//scenarios in scenarioOutLine //li tags list
Elements eScenarioList = eScenarioOutline.getElementsByClass("steps")
.get(0)
.getElementsByAttributeValueContaining("class", "node scenario");
int seq = 1;
ArrayList pdfReportList = new ArrayList<>();
for (Element scenario : eScenarioList) {
String sceStatus = scenario.attr("status");
String pdfReportPrefix = getReportPrefix(sceStatus);
String pdfReport;
int ex = 1;
if (tags.containsKey("CucumberScenario")) { //PDF report based of Feaure Scenario Tag value
pdfReport = reportname
.replace(".html", "_" + tags.get("CucumberScenario") + "_Ex" + (ex) + ".pdf")
.replace("ExtentHtml", pdfReportPrefix);
} else { // i.e. @CucumberScenario tag not in the Feature file either check if div element with id=pdfReport
String pdfReportSuffix = getReportSuffix(scenario);
if (pdfReportSuffix.equalsIgnoreCase("")) {
pdfReport = reportname
.replace(".html", "_Sce" + seq + "_Ex" + (ex) + ".pdf")
.replace("ExtentHtml", pdfReportPrefix);
seq++;
} else {
pdfReport = reportname
.replace(".html", pdfReportSuffix + "_Ex" + (ex) + ".pdf")
.replace("ExtentHtml", pdfReportPrefix);
}
}
while (pdfReportList.contains(pdfReport)) {
ex++;
pdfReport = pdfReport.replace("_Ex" + (ex - 1) + ".pdf", "_Ex" + ex + ".pdf");
}
pdfReportList.add(pdfReport);
log.info("Generating PDF report: " + pdfReport);
PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
PdfWriter writer = new PdfWriter(pdfReport);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.A4.rotate());
//Setting the Header Page - Report Name - Appilcation, Executed By, test Envrionment, start, end, time takes, feature, scenario, step
document.add(new Paragraph("Test Execution Report").setFont(font).setFontSize(60).setBold().setTextAlignment(TextAlignment.CENTER));
String zephyrId = Property.getVariable("TMSIssueId");
if (zephyrId != null) {
document.add(new Paragraph("TMS Issue ID: " + zephyrId)
.setFont(font).setFontSize(40).setBold().setTextAlignment(TextAlignment.CENTER));
}
if (tags.containsKey("CucumberScenario")) {
document.add(new Paragraph("Cucumber Scenario : " + tags.get("CucumberScenario"))
.setFont(font).setFontSize(25).setBold().setTextAlignment(TextAlignment.CENTER));
}
document.add(getHeaderPageTable(scenario, reportname, tagList, font));
document.add(new Paragraph("\n"));
document.add(getTechStackTable(scenario, font));
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//Table 1 - Scenario Outline with scenario outline details
Table tableFeatureDetails = new Table(new float[]{1, 8});
tableFeatureDetails.setWidth(UnitValue.createPercentValue(100));
tableFeatureDetails.setFixedLayout();
//row for feature name
tableFeatureDetails.addCell(new Cell().add(new Paragraph("Feature : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
tableFeatureDetails.addCell(new Cell().add(new Paragraph(testID_FeatureName.get(sceOutTestId))).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario Outline name
tableFeatureDetails.addCell(new Cell().add(new Paragraph("Scenario OutLine: ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
tableFeatureDetails.addCell(new Cell().add(new Paragraph(scenarioOutlineName)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
document.add(tableFeatureDetails);
document.add(new Paragraph("\n"));
//Table 1 - Scenario with scenario details
Table tableScenarioDetails = new Table(new float[]{1, 8});
tableScenarioDetails.setWidth(UnitValue.createPercentValue(100));
tableScenarioDetails.setFixedLayout();
String scenarioName = replaceText(scenario.getElementsByClass("step-name").get(0).text().trim(), sceStatus); //Extent 4
//row for scenario name
tableScenarioDetails.addCell(new Cell().add(new Paragraph("Scenario : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
tableScenarioDetails.addCell(new Cell().add(new Paragraph(scenarioName)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario Start DateTime
try {
String startTime = scenario.getElementById("sceStartTime").text();
tableScenarioDetails.addCell(new Cell().add(new Paragraph("Start : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
tableScenarioDetails.addCell(new Cell().add(new Paragraph(startTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
} catch (Exception ignore) {
}
//row for scenario End DateTime
try {
String endTime = scenario.getElementById("sceEndTime").text();
tableScenarioDetails.addCell(new Cell().add(new Paragraph("End : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
tableScenarioDetails.addCell(new Cell().add(new Paragraph(endTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
} catch (Exception ignore) {
}
//Scenario status row
tableScenarioDetails.addCell(new Cell().add(new Paragraph("status : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
tableScenarioDetails.addCell(colouredStatusCell(sceStatus));
document.add(tableScenarioDetails);
document.add(new Paragraph("\n"));
//steps in the scenario //steps of scenario - gc steps
Table tableSteps = new Table(new float[]{50, 1});
tableSteps.setWidth(UnitValue.createPercentValue(100));
tableSteps.setFixedLayout();
Elements eSteps = scenario.getElementsByTag("ul").get(0).getElementsByTag("li");
for (Element eStep : eSteps) {
String stepStatus = eStep.attr("status");
if (!stepStatus.equalsIgnoreCase("")) {
String stepText = replaceText(eStep.getElementsByClass("step-name").get(0).text().trim(), stepStatus); //Extent 4
Paragraph para = new Paragraph().setFont(font);
para.add(stepText);
//look for steplog //Extent 4
Elements stepLogs = eStep.getElementsByClass("node-step");
for (Element stepLog : stepLogs) {
String str = stepLog.text();
if (stepText.contains("And a request body") && str.startsWith("{") && !str.contains("Step skipped")) {
//str is json body
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic();
} else if (stepText.contains("And trace out request response") && str.startsWith("response: {")) {
para.add("\nresponse:").setItalic();
str = str.replace("response: ", "");
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic();
} else {
try {
Elements tbody = stepLog.getElementsByTag("tbody");
if (tbody.size() > 0) {
Table stepLogTable = htmlTableToPDFTable(tbody.get(0));
para.add("\n");
para.add(stepLogTable).setItalic().setFontColor(ColorConstants.DARK_GRAY);
} else {
while (str.length() > 95) {
String subStr = str.substring(0, 95);
para.add("\n" + subStr).setItalic().setFontColor(ColorConstants.DARK_GRAY);
str = str.substring(95);
}
para.add("\n" + str.trim()).setItalic().setFontColor(ColorConstants.DARK_GRAY);
}
} catch (Exception e) {
while (str.length() > 95) {
String subStr = str.substring(0, 95);
para.add("\n" + subStr).setItalic().setFontColor(ColorConstants.DARK_GRAY);
str = str.substring(95);
}
para.add("\n" + str.trim()).setItalic().setFontColor(ColorConstants.DARK_GRAY);
}
}
}
//Extent 4
if (stepStatus.equalsIgnoreCase("fail")) {
String errorMsg = eStep.getElementsByTag("textarea").text();
para.add("\n\n" + errorMsg).setItalic().setFontColor(ColorConstants.RED).setFontSize(8);
}
//step
tableSteps.addCell(new Cell().add(para).setFont(font));
if (stepStatus.equalsIgnoreCase("pass"))
tableSteps.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (stepStatus.equalsIgnoreCase("fail"))
tableSteps.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (stepStatus.equalsIgnoreCase("skip"))
tableSteps.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
//add image
try {
Elements eImgs = eStep.getElementsByTag("a");
addImage(tableSteps, eImgs);
} catch (Exception e) {
}
}
} //step loop end
//adding data to PDF :
document.add(tableSteps);
document.close();
}//scenario loop end
}//scenario outline loop end
}
private static String getReportSuffix(Element scenario) {
try {
return scenario.getElementById("pdfReport").text().replace("PDF Name : ", "");
} catch (Exception e1) {
System.out.println(e1.getMessage());
return "";
}
}
private static String getReportPrefix(String sceStatus) {
if (sceStatus.equalsIgnoreCase("pass"))
return "E_Pass";
if (sceStatus.equalsIgnoreCase("skip"))
return "E_Skip";
return "E_Fail";
}
public static void bddscenarioOutLinePDFReport(String reportname) throws IOException {
//getting the Extent HTML Report -
org.jsoup.nodes.Document htmlFile = Jsoup.parse(new File(reportname), "ISO-8859-1");
//get the Feature name for the scenarios
Elements eF = htmlFile.getElementsByClass("linked");
Map testID_FeatureName = new LinkedHashMap();
for (Element e : eF) {
String test_id = e.attr("test-id");
String featureName = e.text().trim().split("\\.")[0];
testID_FeatureName.put(test_id, featureName);
}
//get the Gherkin Scenario Outline
//Extent 4
Elements eScenarioOutlines = htmlFile.getElementsByClass("scenariooutline node");
/*int seq = 1;*/
System.out.println("====================================================");
System.out.println("number of scenarios outline: " + eScenarioOutlines.size());
System.out.println("====================================================");
for (Element eScenarioOutline : eScenarioOutlines) {
Map pdfMap = new HashMap<>();
Map scenarioOutlineStatusMap = new HashMap<>();
//get the 'TMSIssueId:<> and CucumberScenario:<> tags. These will be used to update the Jira.
Elements eTags = eScenarioOutline.getElementsByClass("category label");
Map tags = new HashMap<>();
ArrayList tagList = new ArrayList();
for (Element eTag : eTags) {
String tag = eTag.text().trim();
tagList.add(tag);
String[] t = tag.split(":");
if (t.length > 1) {
tags.put(t[0].substring(1), t[1]);
}
}
//Scenario outline
String sceOutTestId = eScenarioOutline.attr("test-id");
String sceOutLineStatus = eScenarioOutline.attr("status");
String prefix = sceOutLineStatus.equalsIgnoreCase("pass") ? "E_Pass" : "E_Fail";
String scenarioOutlineName = replaceText(eScenarioOutline.getElementsByClass("scenario-name")
.get(0).text().trim(), sceOutLineStatus);
//add the example table
//bdd-test
Elements tableRows = eScenarioOutline
.getElementsByClass("bdd-test").get(0).
getElementsByTag("table").get(0)
.getElementsByTag("tr");
Table exTable = new Table(tableRows.get(0).getElementsByTag("td").size());
exTable.setFixedLayout();
for (Element row : tableRows) {
Elements columns = row.getElementsByTag("td");
for (Element column : columns) {
exTable.addCell((new Cell().add(new Paragraph(column.text()))));
}
}
//scenarios in scenarioOutLine //li tags list
Elements eScenarioList = eScenarioOutline.getElementsByClass("steps").get(0).getElementsByAttributeValueContaining("class", "node scenario");
int seq = 1;
for (Element scenario : eScenarioList) {
//+++++
String pdfReport;
String pdfReportSuffix;
try {
pdfReportSuffix = scenario.getElementById("pdfReport").text().replace("PDF Name : ", "");
} catch (Exception e1) {
pdfReportSuffix = "Anshul1";
System.out.println(e1.getMessage());
}
if (tags.containsKey("CucumberScenario")) {
pdfReport = reportname
.replace(".html", "_" + pdfReportSuffix + ".pdf")
.replace("ExtentHtml", prefix);
} else {
pdfReport = reportname
.replace(".html", "_Sce" + seq + ".pdf")
.replace("ExtentHtml", prefix);
seq++;
}
Document document = null;
PdfFont font = null;
//if part of same scenario outline folder
if (!pdfMap.containsKey(pdfReport)) {
System.out.println("Generating PDF report: " + pdfReport);
font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
PdfWriter writer = new PdfWriter(pdfReport);
PdfDocument pdf = new PdfDocument(writer);
document = new Document(pdf, PageSize.A4.rotate());
pdfMap.put(pdfReport, document);
//Setting the Header Page - Report Name - Appilcation, Executed By, test Envrionment, start, end, time takes, feature, scenario, step
//Header Page Table 1
Table headerTable1 = new Table(new float[]{1, 8});
headerTable1.setWidth(UnitValue.createPercentValue(50));
headerTable1.setHorizontalAlignment(HorizontalAlignment.CENTER);
// headerTable1.setFixedLayout();
document.add(new Paragraph("Test Execution Report").setFont(font).setFontSize(60).setBold().setTextAlignment(TextAlignment.CENTER));
if (tags.containsKey("CucumberScenario")) {
String zephyrId = Property.getVariable("TMSIssueId"); //IP-9983
// document.add(new Paragraph("Zephyr Id: " + tags.get("TMSIssueId")).setFont(font).setFontSize(40).setBold().setTextAlignment(TextAlignment.CENTER));
document.add(new Paragraph("Zephyr Id: " + zephyrId).setFont(font).setFontSize(40).setBold().setTextAlignment(TextAlignment.CENTER));
document.add(new Paragraph("Cucumber Scenario : " + tags.get("CucumberScenario")).setFont(font).setFontSize(25).setBold().setTextAlignment(TextAlignment.CENTER));
}
String envPropFile = Property.getVariable("cukes.env").toUpperCase();
// String duration = eScenarioOutline.getElementsByClass("duration right label").get(0).text();
headerTable1.addCell(new Cell().add(new Paragraph("Environment")).setFont(font).setBold());
headerTable1.addCell(new Cell().add(new Paragraph(envPropFile)).setFont(font));
try {
String buildTag = Property.getVariable("GitTag").toUpperCase();
headerTable1.addCell(new Cell().add(new Paragraph("Build Tag")).setFont(font).setBold());
headerTable1.addCell(new Cell().add(new Paragraph(buildTag)).setFont(font));
} catch (Exception ignore) {
}
headerTable1.addCell(new Cell().add(new Paragraph("Executor")).setFont(font).setBold());
String executor = System.getProperty("user.name").equalsIgnoreCase("root") ? serviceUserName : System.getProperty("user.name");
headerTable1.addCell(new Cell().add(new Paragraph(executor)).setFont(font));
headerTable1.addCell(new Cell().add(new Paragraph("Date")).setFont(font).setBold());
String strDate = reportname.substring(22, 37);
headerTable1.addCell(new Cell().add(new Paragraph(strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6))).setFont(font));
// headerTable1.addCell(new Cell().add(new Paragraph("Duration")).setFont(font).setBold());
// headerTable1.addCell(new Cell().add(new Paragraph(duration)).setFont(font));
headerTable1.addCell(new Cell().add(new Paragraph("Tags")).setFont(font).setBold());
headerTable1.addCell(new Cell().add(new Paragraph(tagList.toString())).setFont(font));
document.add(headerTable1);
document.add(new Paragraph("\n"));
Table techTable = new Table(new float[]{1});
techTable.setWidth(UnitValue.createPercentValue(30));
techTable.setHorizontalAlignment(HorizontalAlignment.CENTER);
techTable.setFixedLayout();
try {
String platform = scenario.getElementById("platform").text();
techTable.addCell(new Cell().add(new Paragraph(platform)).setFont(font));
String platformVersion = scenario.getElementById("platFormVersion").text();
techTable.addCell(new Cell().add(new Paragraph(platformVersion)).setFont(font));
} catch (Exception ignored) {
}
try {
String browser = scenario.getElementById("browser").text();
String version = scenario.getElementById("Version").text();
techTable.addCell(new Cell().add(new Paragraph(browser)).setFont(font));
techTable.addCell(new Cell().add(new Paragraph(version)).setFont(font));
} catch (Exception ignored) {
}
document.add(techTable);
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//Table 1 - Scenario Outline with scenario outline details
Table table1 = new Table(new float[]{1, 8});
table1.setWidth(UnitValue.createPercentValue(100));
table1.setFixedLayout();
//row for feature name
table1.addCell(new Cell().add(new Paragraph("Feature : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(testID_FeatureName.get(sceOutTestId))).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario Outline name
table1.addCell(new Cell().add(new Paragraph("Scenario OutLine: ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(scenarioOutlineName)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
document.add(table1);
document.add(new Paragraph("\n"));
} else {
document = (Document) pdfMap.get(pdfReport);
font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
}
//Table 1 - Scenario with scenario details
Table table2 = new Table(new float[]{1, 8});
table2.setWidth(UnitValue.createPercentValue(100));
table2.setBackgroundColor(ColorConstants.CYAN);
table2.setFixedLayout();
// String sceTestId = eScenarioOutline.attr("test-id");
String sceStatus = scenario.attr("status");
String scenarioName = replaceText(scenario.getElementsByClass("step-name").get(0).text().trim(), sceStatus); //Extent 4
//row for scenario name
table2.addCell(new Cell().add(new Paragraph("Scenario : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table2.addCell(new Cell().add(new Paragraph(scenarioName)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario Start DateTime
String startTime = scenario.getElementById("sceStartTime").text();
table2.addCell(new Cell().add(new Paragraph("Start : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table2.addCell(new Cell().add(new Paragraph(startTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario End DateTime
String endTime = scenario.getElementById("sceEndTime").text();
table2.addCell(new Cell().add(new Paragraph("End : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table2.addCell(new Cell().add(new Paragraph(endTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//Scenario status row
table2.addCell(new Cell().add(new Paragraph("status : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
if (sceStatus.equalsIgnoreCase("pass")) {
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
if (scenarioOutlineStatusMap.containsKey(pdfReport)) {
boolean status = scenarioOutlineStatusMap.get(pdfReport);
if (status) scenarioOutlineStatusMap.put(pdfReport, true);
} else {
scenarioOutlineStatusMap.put(pdfReport, true);
}
} else {
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
scenarioOutlineStatusMap.put(pdfReport, false);
}
document.add(table2);
document.add(new Paragraph("\n"));
//steps in the scenario //steps of scenario - gc steps
Table table3 = new Table(new float[]{50, 1});
table3.setWidth(UnitValue.createPercentValue(100));
table3.setFixedLayout();
Elements eSteps = scenario.getElementsByTag("ul").get(0).getElementsByTag("li");
for (Element eStep : eSteps) {
String stepStatus = eStep.attr("status");
if (!stepStatus.equalsIgnoreCase("")) {
String stepText = replaceText(eStep.getElementsByClass("step-name").get(0).text().trim(), stepStatus); //Extent 4
Paragraph para = new Paragraph().setFont(font);
para.add(stepText);
//look for steplog //Extent 4
Elements stepLogs = eStep.getElementsByClass("node-step");
for (Element stepLog : stepLogs) {
Elements tbody = stepLog.getElementsByTag("tbody");
String str = stepLog.text();
if (stepText.contains("And a request body") && str.startsWith("{") && !str.contains("Step skipped")) {
//str is json body
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic();
} else if (stepText.contains("And trace out request response") && str.startsWith("response: {")) {
para.add("\nresponse:").setItalic();
str = str.replace("response: ", "");
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic();
} else {
if (tbody.size() > 0) {
Table stepLogTable = htmlTableToPDFTable(tbody.get(0));
para.add("\n");
para.add(stepLogTable).setItalic().setFontColor(ColorConstants.DARK_GRAY);
} else {
while (str.length() > 95) {
String subStr = str.substring(0, 95);
para.add("\n" + subStr).setItalic().setFontColor(ColorConstants.DARK_GRAY);
str = str.substring(95);
}
para.add("\n" + str.trim()).setItalic().setFontColor(ColorConstants.DARK_GRAY);
}
}
}
//Extent 4
if (stepStatus.equalsIgnoreCase("fail")) {
String errorMsg = eStep.getElementsByTag("textarea").text();
para.add("\n\n" + errorMsg).setItalic().setFontColor(ColorConstants.RED).setFontSize(8);
}
//step
table3.addCell(new Cell().add(para).setFont(font));
if (stepStatus.equalsIgnoreCase("pass"))
table3.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (stepStatus.equalsIgnoreCase("fail"))
table3.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (stepStatus.equalsIgnoreCase("skip"))
table3.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
//add image
try {
Elements eImgs = eStep.getElementsByTag("a");
for (Element eImg : eImgs) {
String imgFile = eImg.attr("href");
imgFile = "RunReports" + imgFile.substring(1);
ImageData data = ImageDataFactory.create(imgFile);
Image image = new Image(data);
image.scaleToFit(PageSize.A4.rotate().getHeight(), PageSize.A4.rotate().getWidth());
table3.addCell(new Cell().add(image));
table3.addCell(new Cell().add(new Paragraph().add("")));
}
} catch (Exception e) {
}
}
}
//adding data to PDF :
document.add(table3);
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
// document.close();
}
Set x = pdfMap.keySet();
for (String s : x) {
Document doc = (Document) pdfMap.get(s);
PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
doc.add(new Paragraph("Scenario Outline Status").setFont(font).setFontSize(30).setBold().setTextAlignment(TextAlignment.CENTER));
doc.add(new Paragraph(scenarioOutlineStatusMap.get(s) ? "Pass" : "Fail").setFont(font)
.setFontSize(20)
.setBold()
.setFontColor(scenarioOutlineStatusMap.get(s) ? ColorConstants.GREEN : ColorConstants.RED)
.setTextAlignment(TextAlignment.CENTER));
scenarioOutlineStatusMap.get(s);
doc.close();
}
}
}
public static void generatePDfReport(String reportname) throws IOException {
try {
//creating pdf
PdfWriter writer = new PdfWriter(reportname.replace(".html", ".pdf"));
PdfDocument pdf = new PdfDocument(writer);
com.itextpdf.layout.Document document = new com.itextpdf.layout.Document(pdf, PageSize.A4.rotate());
document.setMargins(20, 20, 20, 20);
PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
//getting the Extent HTML Report -
org.jsoup.nodes.Document htmlFile = Jsoup.parse(new File(reportname), "ISO-8859-1");
//Setting the Header Page - Report Name - Appilcation, Executed By, test Envrionment, start, end, time takes, feature, scenario, step
//Header Page Table 1
Table tableP1 = new Table(new float[]{1, 5});
tableP1.setWidth(UnitValue.createPercentValue(30));
tableP1.setHorizontalAlignment(HorizontalAlignment.CENTER);
Elements cardElements = htmlFile.getElementsByClass("card-panel r");
document.add(new Paragraph("Test Execution Report").setFont(font).setFontSize(60).setBold().setTextAlignment(TextAlignment.CENTER));
String zephyrId = Property.getVariable("TMSIssueId"); //IP-9983
document.add(new Paragraph("Zephyr ID: " + zephyrId).setFont(font).setFontSize(40).setBold().setTextAlignment(TextAlignment.CENTER));
String envPropFile = Property.getVariable("cukes.env").toUpperCase();
tableP1.addCell(new Cell().add(new Paragraph("Environment")).setFont(font).setBold());
tableP1.addCell(new Cell().add(new Paragraph(envPropFile)).setFont(font));
try {
String buildTag = Property.getVariable("GitTag").toUpperCase();
tableP1.addCell(new Cell().add(new Paragraph("Build Tag")).setFont(font).setBold());
tableP1.addCell(new Cell().add(new Paragraph(buildTag)).setFont(font));
} catch (Exception ignore) {
}
tableP1.addCell(new Cell().add(new Paragraph("Executor")).setFont(font).setBold());
tableP1.addCell(new Cell().add(new Paragraph(System.getProperty("user.name"))).setFont(font));
for (Element cardElement : cardElements) {
String value = cardElement.getElementsByClass("panel-lead").get(0).text();
String cardHeading = cardElement.text().replace(value, "").trim();
tableP1.addCell(new Cell().add(new Paragraph(cardHeading)).setFont(font).setBold());
tableP1.addCell(new Cell().add(new Paragraph(value)).setFont(font));
}
document.add(tableP1);
document.add(new Paragraph("\n"));
//Header Page Table 2
Table tableP2 = new Table(new float[]{1, 1, 1});
tableP2.setWidth(UnitValue.createPercentValue(70));
tableP2.setHorizontalAlignment(HorizontalAlignment.CENTER);
tableP2.addCell(new Cell().setMargin(10).add(new Paragraph("Features")).setFont(font).setBold());
tableP2.addCell(new Cell().setMargin(10).add(new Paragraph("Scenarios")).setFont(font).setBold());
tableP2.addCell(new Cell().setMargin(10).add(new Paragraph("Steps")).setFont(font).setBold());
Elements chartElements = htmlFile.getElementsByClass("card-panel nm-v");
for (Element chartElement : chartElements) {
Elements es = chartElement.getElementsByClass("block text-small");
tableP2.addCell(new Cell().setMargin(10).add(new Paragraph(es.get(0).text() + "\n" + es.get(1).text())).setFont(font).setBold());
}
document.add(tableP2);
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//get the Feature name for teh scenarios
Elements eF = htmlFile.getElementsByClass("linked");
Map testID_FeatureName = new LinkedHashMap();
for (Element e : eF) {
String test_id = e.attr("test-id");
String featureName = e.text().trim().split("\\.")[0];
testID_FeatureName.put(test_id, featureName);
}
//get the Gherkin Scenario
Elements eScenarios = htmlFile.getElementsByClass("scenario node");
for (Element eScenario : eScenarios) {
//Table 1 - Scenario with scenario details
Table table1 = new Table(new float[]{1, 8});
table1.setWidth(UnitValue.createPercentValue(100));
table1.setFixedLayout();
String sceTestId = eScenario.attr("test-id");
String sceStatus = eScenario.attr("status");
String scenarioName = replaceText(eScenario.getElementsByClass("scenario-name").get(0).text().trim(), sceStatus); //Extent 4
// String scenarioName = eScenario.getElementsByClass("desc-text").get(0).text().trim(); //Extent 1
//row for feature name
table1.addCell(new Cell().add(new Paragraph("Feature : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(testID_FeatureName.get(sceTestId))).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario name
table1.addCell(new Cell().add(new Paragraph("Scenario : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(scenarioName)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
try {
//row for scenario Start DateTime
String startTime = eScenario.getElementById("sceStartTime").text();
table1.addCell(new Cell().add(new Paragraph("Start : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(startTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
//row for scenario End DateTime
String endTime = eScenario.getElementById("sceEndTime").text();
table1.addCell(new Cell().add(new Paragraph("End : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(endTime)).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
} catch (Exception ignore) {
}
//Scenario status row
table1.addCell(new Cell().add(new Paragraph("status : ")).setBold().setFont(font).setFontColor(ColorConstants.BLUE));
if (sceStatus.equalsIgnoreCase("pass"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (sceStatus.equalsIgnoreCase("fail"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (sceStatus.equalsIgnoreCase("skip"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
document.add(table1);
Elements eSteps = eScenario.getElementsByTag("ul").get(0).getElementsByTag("li");
// Elements eSteps2 = eScenario.getElementsByClass("steps").get(0).getElementsByTag("li");
//Table 2 - Scenario with scenario steps
Table table2 = new Table(new float[]{50, 1});
table2.setWidth(UnitValue.createPercentValue(100));
table2.setFixedLayout();
for (Element eStep : eSteps) {
String stepStatus = eStep.attr("status");
if (!stepStatus.equalsIgnoreCase("")) {
String stepText = replaceText(eStep.getElementsByClass("step-name").get(0).text().trim(), stepStatus); //Extent 4
Paragraph para = new Paragraph().setFont(font);
para.add(stepText);
//look for steplog //Extent 4
Elements stepLogs = eStep.getElementsByClass("node-step");
for (Element stepLog : stepLogs) {
String str = stepLog.text();
if (stepText.contains("And a request body") && str.startsWith("{") && !str.contains("Step skipped")) {
//str is json body
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic();
} else if (stepText.contains("And trace out request response") && str.startsWith("response: {")) {
para.add("\nresponse:").setItalic();
str = str.replace("response: ", "");
JSONObject obj = new JSONObject(str);
para.add("\n" + obj.toString(4)).setItalic();
} else {
while (str.length() > 108) {
String subStr = str.substring(0, 95);
para.add("\n" + subStr).setItalic();
str = str.substring(108);
}
para.add("\n" + str.trim()).setItalic();
}
}
if (stepStatus.equalsIgnoreCase("fail")) {
String errorMsg = eStep.getElementsByTag("textarea").text();
para.add("\n\n" + errorMsg).setItalic().setFontColor(ColorConstants.RED).setFontSize(8);
}
table2.addCell(new Cell().add(para).setFont(font));
if (stepStatus.equalsIgnoreCase("pass"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (stepStatus.equalsIgnoreCase("fail"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (stepStatus.equalsIgnoreCase("skip"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
//add image
try {
Elements eImgs = eStep.getElementsByTag("a");
for (Element eImg : eImgs) {
String imgFile = eImg.attr("href");
imgFile = "RunReports" + imgFile.substring(1);
ImageData data = ImageDataFactory.create(imgFile);
Image image = new Image(data);
image.scaleToFit(PageSize.A4.rotate().getHeight(), PageSize.A4.rotate().getWidth());
table2.addCell(new Cell().add(image));
table2.addCell(new Cell().add(new Paragraph().add("")));
}
} catch (Exception e) {
}
}
}
//adding data to PDF :
document.add(table2);
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
//get the Gherkin Scenario Outline
//Extent 4
Elements eScenarioOutlines = htmlFile.getElementsByClass("scenariooutline node");
for (Element eScenarioOutline : eScenarioOutlines) {
String sceOutTestId = eScenarioOutline.attr("test-id");
String sceStatus = eScenarioOutline.attr("status");
String scenarioOutlineName = replaceText(eScenarioOutline.getElementsByClass("scenario-name").get(0).text().trim(), sceStatus);
//scenario in scenarioOutLine
Elements eScenarioList = eScenarioOutline.getElementsByClass("steps").get(0).getElementsByAttributeValueContaining("class", "node scenario");
for (Element sce : eScenarioList) {
//Table 1 - Scenario with scenario details
Table table1 = new Table(new float[]{1, 8});
table1.setWidth(UnitValue.createPercentValue(100));
List imageList = new ArrayList();
//Scenario details
String scenarioStatus = sce.attr("status");
String scenarioName = replaceText(sce.getElementsByClass("step-name").get(0).text().trim(), scenarioStatus);
String sceId = sce.attr("test-id");
//row for feature name
table1.addCell(new Cell().add(new Paragraph("Feature : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(testID_FeatureName.get(sceId))).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
//row for scenario outline name
table1.addCell(new Cell().add(new Paragraph("Scenario Outline: ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(scenarioOutlineName)).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
//Scenario outline status row
table1.addCell(new Cell().add(new Paragraph("Outline status : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
if (sceStatus.equalsIgnoreCase("pass"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (sceStatus.equalsIgnoreCase("fail"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (sceStatus.equalsIgnoreCase("skip"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
//row for scenario name
table1.addCell(new Cell().add(new Paragraph("Scenario : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
table1.addCell(new Cell().add(new Paragraph(scenarioName)).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
//Scenario status row
table1.addCell(new Cell().add(new Paragraph("scenario status : ")).setFont(font).setBold().setFontColor(ColorConstants.BLUE));
if (scenarioStatus.equalsIgnoreCase("pass"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (scenarioStatus.equalsIgnoreCase("fail"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (scenarioStatus.equalsIgnoreCase("skip"))
table1.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
document.add(table1);
Elements eSteps = sce.getElementsByTag("ul").get(0).getElementsByTag("li"); //steps of scenario - gc steps
//Table 2 - Scenario with scenario steps
Table table2 = new Table(new float[]{50, 1});
table2.setWidth(UnitValue.createPercentValue(100));
table2.setFixedLayout();
for (Element eStep : eSteps) {
String stepStatus = eStep.attr("status");
if (!stepStatus.equalsIgnoreCase("")) {
String stepText = replaceText(eStep.getElementsByClass("step-name").get(0).text().trim(), stepStatus);
Paragraph para = new Paragraph().setFont(font);
para.add(stepText);
//look for steplog //Extent 4
Elements stepLogs = eStep.getElementsByClass("node-step");
for (Element stepLog : stepLogs) {
para.add("\n" + stepLog.text().trim()).setItalic();
}
//Extent 4
if (stepStatus.equalsIgnoreCase("fail")) {
String errorMsg = eStep.getElementsByTag("textarea").text();
para.add("\n" + errorMsg).setItalic();
}
table2.addCell(new Cell().add(para).setFont(font));
if (stepStatus.equalsIgnoreCase("pass"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN));
else if (stepStatus.equalsIgnoreCase("fail"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED));
else if (stepStatus.equalsIgnoreCase("skip"))
table2.addCell(new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE));
//add image
try {
Elements eImgs = eStep.getElementsByTag("a");
for (Element eImg : eImgs) {
String imgFile = eImg.attr("href");
imgFile = "RunReports" + imgFile.substring(1);
ImageData data = ImageDataFactory.create(imgFile);
Image image = new Image(data);
image.scaleToFit(PageSize.A4.rotate().getHeight(), PageSize.A4.rotate().getWidth());
table2.addCell(new Cell().add(image));
table2.addCell(new Cell().add(new Paragraph().add("")));
}
} catch (Exception e) {
}
}
}
//adding data to PDF :
document.add(table2);
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String replaceText(String str, String status) {
if (status.equalsIgnoreCase("pass")) {
return str.replace("check_circle", "");
} else if (status.equalsIgnoreCase("fail")) {
return str.replace("cancel", "");
} else if (status.equalsIgnoreCase("skip")) {
return str.replace("redo", "");
} else return str;
}
private static Table htmlTableToPDFTable(Element tbody) {
Elements rows = tbody.getElementsByTag("tr");
int columnSize = rows.get(0).getElementsByTag("td").size();
Table table = new Table(columnSize);
table.setWidth(UnitValue.createPercentValue(70));
table.setFixedLayout();
for (Element row : rows) {
Elements columns = row.getElementsByTag("td");
for (Element column : columns) {
table.addCell(new Cell().add(new Paragraph(column.text())));
}
}
return table;
}
private static Cell colouredStatusCell(String sceStatus) {
if (sceStatus.equalsIgnoreCase("pass"))
return new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.GREEN);
if (sceStatus.equalsIgnoreCase("skip"))
return new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.ORANGE);
return new Cell().add(new Paragraph("")).setBackgroundColor(ColorConstants.RED);
}
private static Table getHeaderPageTable(Element eScenario, String reportname, ArrayList tagList, PdfFont font) {
Table table = new Table(new float[]{1, 8});
table.setWidth(UnitValue.createPercentValue(50));
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
String envPropFile = Property.getVariable("cukes.env").toUpperCase();
String executor = System.getProperty("user.name").equalsIgnoreCase("root") ? serviceUserName : System.getProperty("user.name");
String dateTime = reportname.substring(reportname.indexOf("ExtentHtml_") + 11, reportname.indexOf(".html"));
table.addCell(new Cell().add(new Paragraph("Environment")).setFont(font).setBold());
table.addCell(new Cell().add(new Paragraph(envPropFile)).setFont(font));
try {
String buildTag = Property.getVariable("GitTag").toUpperCase();
table.addCell(new Cell().add(new Paragraph("Build Tag")).setFont(font).setBold());
table.addCell(new Cell().add(new Paragraph(buildTag)).setFont(font));
} catch (Exception ignore) {
}
table.addCell(new Cell().add(new Paragraph("Executor")).setFont(font).setBold());
table.addCell(new Cell().add(new Paragraph(executor)).setFont(font));
table.addCell(new Cell().add(new Paragraph("Date")).setFont(font).setBold());
table.addCell(new Cell().add(new Paragraph(dateTime.substring(0, 4) + "-" + dateTime.substring(4, 6) + "-" + dateTime.substring(6))).setFont(font));
try {
String duration = eScenario.getElementsByClass("duration right label").get(0).text();
table.addCell(new Cell().add(new Paragraph("Duration")).setFont(font).setBold());
table.addCell(new Cell().add(new Paragraph(duration)).setFont(font));
} catch (Exception ignore) {
}
table.addCell(new Cell().add(new Paragraph("Tags")).setFont(font).setBold());
table.addCell(new Cell().add(new Paragraph(tagList.toString())).setFont(font));
return table;
}
private static Table getTechStackTable(Element eScenario, PdfFont font) {
Table techTable = new Table(new float[]{1});
techTable.setWidth(UnitValue.createPercentValue(30));
techTable.setHorizontalAlignment(HorizontalAlignment.CENTER);
techTable.setFixedLayout();
try {
String platform = eScenario.getElementById("platform").text();
techTable.addCell(new Cell().add(new Paragraph(platform)).setFont(font));
String platformVersion = eScenario.getElementById("platFormVersion").text();
techTable.addCell(new Cell().add(new Paragraph(platformVersion)).setFont(font));
} catch (Exception ignored) {
}
try {
String browser = eScenario.getElementById("browser").text();
String version = eScenario.getElementById("Version").text();
techTable.addCell(new Cell().add(new Paragraph(browser)).setFont(font));
techTable.addCell(new Cell().add(new Paragraph(version)).setFont(font));
} catch (Exception ignored) {
}
return techTable;
}
}