|
| 1 | +package com.anhtester.reports; |
| 2 | + |
| 3 | +import com.anhtester.constants.FrameworkConstants; |
| 4 | +import com.anhtester.driver.DriverManager; |
| 5 | +import com.anhtester.enums.AuthorType; |
| 6 | +import com.anhtester.enums.CategoryType; |
| 7 | +import com.anhtester.utils.*; |
| 8 | +import com.aventstack.extentreports.ExtentReports; |
| 9 | +import com.aventstack.extentreports.MediaEntityBuilder; |
| 10 | +import com.aventstack.extentreports.Status; |
| 11 | +import com.aventstack.extentreports.markuputils.Markup; |
| 12 | +import com.aventstack.extentreports.reporter.ExtentSparkReporter; |
| 13 | +import com.aventstack.extentreports.reporter.configuration.Theme; |
| 14 | +import org.openqa.selenium.OutputType; |
| 15 | +import org.openqa.selenium.TakesScreenshot; |
| 16 | +//import tech.grasshopper.reporter.ExtentPDFReporter; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +public class ExtentReportManager { |
| 22 | + |
| 23 | + private static ExtentReports extentReports; |
| 24 | + private static String link = ""; |
| 25 | + |
| 26 | + public static void initReports() { |
| 27 | + if (Objects.isNull(extentReports)) { |
| 28 | + extentReports = new ExtentReports(); |
| 29 | + |
| 30 | + if (FrameworkConstants.OVERRIDE_REPORTS.trim().equals(FrameworkConstants.NO)) { |
| 31 | + LogUtils.info("OVERRIDE EXTENT REPORTS = " + FrameworkConstants.OVERRIDE_REPORTS); |
| 32 | + link = FrameworkConstants.EXTENT_REPORT_FOLDER_PATH + File.separator + DateUtils.getCurrentDateTimeCustom("_") + "_" + FrameworkConstants.EXTENT_REPORT_FILE_NAME; |
| 33 | + LogUtils.info("Link Extent Report: " + link); |
| 34 | + } else { |
| 35 | + LogUtils.info("OVERRIDE EXTENT REPORTS = " + FrameworkConstants.OVERRIDE_REPORTS); |
| 36 | + link = FrameworkConstants.EXTENT_REPORT_FILE_PATH; |
| 37 | + LogUtils.info("Link Extent Report: " + link); |
| 38 | + } |
| 39 | + |
| 40 | +// ExtentPDFReporter pdf = new ExtentPDFReporter("reports/ExtentReports/PdfReport.pdf"); |
| 41 | +// try { |
| 42 | +// pdf.loadJSONConfig(new File("src/test/resources/pdf-config.json")); |
| 43 | +// } catch (IOException e) { |
| 44 | +// throw new RuntimeException(e); |
| 45 | +// } |
| 46 | +// extentReports.attachReporter(pdf); |
| 47 | + |
| 48 | + ExtentSparkReporter spark = new ExtentSparkReporter(link); |
| 49 | + extentReports.attachReporter(spark); |
| 50 | + spark.config().setTheme(Theme.STANDARD); |
| 51 | + spark.config().setDocumentTitle(FrameworkConstants.REPORT_TITLE); |
| 52 | + spark.config().setReportName(FrameworkConstants.REPORT_TITLE); |
| 53 | + extentReports.setSystemInfo("Framework Name", FrameworkConstants.REPORT_TITLE); |
| 54 | + extentReports.setSystemInfo("Author", FrameworkConstants.AUTHOR); |
| 55 | + |
| 56 | + LogUtils.info("Extent Reports is installed."); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static void flushReports() { |
| 61 | + if (Objects.nonNull(extentReports)) { |
| 62 | + extentReports.flush(); |
| 63 | + } |
| 64 | + ExtentTestManager.unload(); |
| 65 | + ReportUtils.openReports(link); |
| 66 | + } |
| 67 | + |
| 68 | + public static void createTest(String testCaseName) { |
| 69 | + ExtentTestManager.setExtentTest(extentReports.createTest(IconUtils.getBrowserIcon() + " " + testCaseName)); |
| 70 | + //ExtentTestManager.setExtentTest(extentReports.createTest(testCaseName)); |
| 71 | + } |
| 72 | + |
| 73 | + public static void createTest(String testCaseName, String description) { |
| 74 | + ExtentTestManager.setExtentTest(extentReports.createTest(testCaseName, description)); |
| 75 | + } |
| 76 | + |
| 77 | + public static void removeTest(String testCaseName) { |
| 78 | + extentReports.removeTest(testCaseName); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Adds the screenshot. |
| 83 | + * |
| 84 | + * @param message the message |
| 85 | + */ |
| 86 | + public static void addScreenShot(String message) { |
| 87 | + String base64Image = "data:image/png;base64," + ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.BASE64); |
| 88 | + |
| 89 | + //Base64 from Screenshot of Selenium |
| 90 | + ExtentTestManager.getExtentTest().log(Status.INFO, MediaEntityBuilder.createScreenCaptureFromBase64String(base64Image).build()); |
| 91 | + |
| 92 | + //File Path from Screenshot of Java |
| 93 | + //ExtentTestManager.getExtentTest().log(Status.INFO, MediaEntityBuilder.createScreenCaptureFromPath(String.valueOf(CaptureHelpers.getScreenshotFile(message))).build()); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Adds the screenshot. |
| 99 | + * |
| 100 | + * @param status the status |
| 101 | + * @param message the message |
| 102 | + */ |
| 103 | + public static void addScreenShot(Status status, String message) { |
| 104 | + String base64Image = "data:image/png;base64," + ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.BASE64); |
| 105 | + |
| 106 | + //Base64 from Screenshot of Selenium |
| 107 | + ExtentTestManager.getExtentTest().log(status, MediaEntityBuilder.createScreenCaptureFromBase64String(base64Image).build()); |
| 108 | + |
| 109 | + //File Path from Screenshot of Java |
| 110 | + //ExtentTestManager.getExtentTest().log(status, MediaEntityBuilder.createScreenCaptureFromPath(CaptureHelpers.getScreenshotAbsolutePath(message)).build()); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + synchronized public static void addAuthors(AuthorType[] authors) { |
| 115 | + if (authors == null) { |
| 116 | + ExtentTestManager.getExtentTest().assignAuthor("ANHTESTER"); |
| 117 | + } else { |
| 118 | + for (AuthorType author : authors) { |
| 119 | + ExtentTestManager.getExtentTest().assignAuthor(author.toString()); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // public static void addCategories(String[] categories) { |
| 125 | + synchronized public static void addCategories(CategoryType[] categories) { |
| 126 | + if (categories == null) { |
| 127 | + ExtentTestManager.getExtentTest().assignCategory("REGRESSION"); |
| 128 | + } else { |
| 129 | + // for (String category : categories) { |
| 130 | + for (CategoryType category : categories) { |
| 131 | + ExtentTestManager.getExtentTest().assignCategory(category.toString()); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + synchronized public static void addDevices() { |
| 137 | + ExtentTestManager.getExtentTest().assignDevice(BrowserInfoUtils.getBrowserInfo()); |
| 138 | +// ExtentReportManager.getExtentTest() |
| 139 | +// .assignDevice(BrowserIconUtils.getBrowserIcon() + " : " + BrowserInfoUtils.getBrowserInfo()); |
| 140 | + } |
| 141 | + |
| 142 | + public static void logMessage(String message) { |
| 143 | + ExtentTestManager.getExtentTest().log(Status.INFO, message); |
| 144 | + } |
| 145 | + |
| 146 | + public static void logMessage(Status status, String message) { |
| 147 | + ExtentTestManager.getExtentTest().log(status, message); |
| 148 | + } |
| 149 | + |
| 150 | + public static void logMessage(Status status, Object message) { |
| 151 | + ExtentTestManager.getExtentTest().log(status, (Throwable) message); |
| 152 | + } |
| 153 | + |
| 154 | + public static void pass(String message) { |
| 155 | + //LogUtils.info("ExtentReportManager class: " + ExtentTestManager.getExtentTest()); |
| 156 | + ExtentTestManager.getExtentTest().pass(message); |
| 157 | + } |
| 158 | + |
| 159 | + public static void pass(Markup message) { |
| 160 | + ExtentTestManager.getExtentTest().pass(message); |
| 161 | + } |
| 162 | + |
| 163 | + public static void fail(String message) { |
| 164 | + ExtentTestManager.getExtentTest().fail(message); |
| 165 | + } |
| 166 | + |
| 167 | + public static void fail(Object message) { |
| 168 | + ExtentTestManager.getExtentTest().fail((String) message); |
| 169 | + } |
| 170 | + |
| 171 | + public static void fail(Markup message) { |
| 172 | + ExtentTestManager.getExtentTest().fail(message); |
| 173 | + } |
| 174 | + |
| 175 | + public static void skip(String message) { |
| 176 | + ExtentTestManager.getExtentTest().skip(message); |
| 177 | + } |
| 178 | + |
| 179 | + public static void skip(Markup message) { |
| 180 | + ExtentTestManager.getExtentTest().skip(message); |
| 181 | + } |
| 182 | + |
| 183 | + public static void info(Markup message) { |
| 184 | + ExtentTestManager.getExtentTest().info(message); |
| 185 | + } |
| 186 | + |
| 187 | + public static void info(String message) { |
| 188 | + ExtentTestManager.getExtentTest().info(message); |
| 189 | + } |
| 190 | + |
| 191 | + public static void warning(String message) { |
| 192 | + ExtentTestManager.getExtentTest().log(Status.WARNING, message); |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments