How to use createWriter method of com.paypal.selion.internal.reports.html.HtmlReporterListener class

Best SeLion code snippet using com.paypal.selion.internal.reports.html.HtmlReporterListener.createWriter

Source:HtmlReporterListener.java Github

copy

Full Screen

...100 return;101 }102 this.outputDir = outputDir;103 ReportDataGenerator.initReportData(suites);104 out = createWriter(outputDir);105 startHtml(out);106 List<Line> lines = createSummary(suites);107 createDetail(lines);108 createMethodContent(suites, outputDir);109 endHtml(out);110 out.flush();111 out.close();112 logger.exiting();113 }114 115 private void createDetail(List<Line> lines) {116 logger.entering(lines);117 for (Line line : lines) {118 createContent(line);119 }120 logger.exiting();121 }122 private void createContent(Line line) {123 logger.entering(line);124 try {125 File f = new File(outputDir + "/html/", line.getId() + ".html");126 logger.fine("generating method " + f.getAbsolutePath());127 Writer fileSystemWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(128 new FileOutputStream(f), "UTF8")));129 Map<ITestNGMethod, List<ITestResult>> resultByMethod = new HashMap<ITestNGMethod, List<ITestResult>>();130 // find all methods131 for (ITestResult result : line.getAssociatedResults()) {132 List<ITestResult> list = resultByMethod.get(result.getMethod());133 if (list == null) {134 list = new ArrayList<ITestResult>();135 resultByMethod.put(result.getMethod(), list);136 }137 list.add(result);138 }139 // for each method, find all the status140 for (Entry<ITestNGMethod, List<ITestResult>> method : resultByMethod.entrySet()) {141 List<ITestResult> passed = new ArrayList<ITestResult>();142 List<ITestResult> failed = new ArrayList<ITestResult>();143 List<ITestResult> skipped = new ArrayList<ITestResult>();144 List<ITestResult> results = method.getValue();145 for (ITestResult result : results) {146 switch (result.getStatus()) {147 case ITestResult.SUCCESS:148 passed.add(result);149 break;150 case ITestResult.FAILURE:151 failed.add(result);152 break;153 case ITestResult.SKIP:154 skipped.add(result);155 break;156 default:157 throw new ReporterException(158 "Implementation exists only for tests with status as : Success, Failure and Skipped");159 }160 }161 // for each status // method, create the html162 if (passed.size() > 0) {163 Template t = ve.getTemplate("/templates/method.part.html");164 VelocityContext context = new VelocityContext();165 context.put("status", "passed");166 context.put("method", passed.get(0).getMethod());167 StringBuilder buff = new StringBuilder();168 for (ITestResult result : passed) {169 buff.append(getContent(result));170 }171 context.put("content", buff.toString());172 StringWriter writer = new StringWriter();173 t.merge(context, writer);174 fileSystemWriter.write(writer.toString());175 }176 if (failed.size() > 0) {177 Template t = ve.getTemplate("/templates/method.part.html");178 VelocityContext context = new VelocityContext();179 context.put("status", "failed");180 context.put("method", failed.get(0).getMethod());181 StringBuilder buff = new StringBuilder();182 for (ITestResult result : failed) {183 buff.append(getContent(result));184 }185 context.put("content", buff.toString());186 StringWriter writer = new StringWriter();187 t.merge(context, writer);188 fileSystemWriter.write(writer.toString());189 }190 if (skipped.size() > 0) {191 Template t = ve.getTemplate("/templates/method.part.html");192 VelocityContext context = new VelocityContext();193 context.put("status", "skipped");194 context.put("method", skipped.get(0).getMethod());195 StringBuilder buff = new StringBuilder();196 for (ITestResult result : skipped) {197 buff.append(getContent(result));198 }199 context.put("content", buff.toString());200 StringWriter writer = new StringWriter();201 t.merge(context, writer);202 fileSystemWriter.write(writer.toString());203 }204 }205 fileSystemWriter.flush();206 fileSystemWriter.close();207 } catch (Exception e) {208 ReporterException re = new ReporterException(e);209 throw re;210 }211 logger.exiting();212 }213 private void createMethodContent(List<ISuite> suites, String outdir) {214 logger.entering(new Object[] { suites, outdir });215 for (ISuite suite : suites) {216 Map<String, ISuiteResult> r = suite.getResults();217 for (ISuiteResult r2 : r.values()) {218 ITestContext ctx = r2.getTestContext();219 ITestNGMethod[] methods = ctx.getAllTestMethods();220 for (int i = 0; i < methods.length; i++) {221 createMethod(ctx, methods[i], outdir);222 }223 }224 }225 logger.exiting();226 }227 private String getContent(ITestResult result) {228 logger.entering(result);229 StringBuilder contentBuffer = new StringBuilder();230 contentBuffer.append(String.format("Total duration of this instance run : %02d sec. ",231 (result.getEndMillis() - result.getStartMillis()) / 1000));232 Object[] parameters = result.getParameters();233 boolean hasParameters = parameters != null && parameters.length > 0;234 List<String> msgs = Reporter.getOutput(result);235 boolean hasReporterOutput = msgs.size() > 0;236 Throwable exception = result.getThrowable();237 boolean hasThrowable = exception != null;238 List<String> imgForFilmStrip = new ArrayList<String>();239 if (hasReporterOutput || hasThrowable) {240 if (hasParameters) {241 contentBuffer.append("<h2 class='yuk_grey_midpnl_ltitle'>");242 for (int i = 0; i < parameters.length; i++) {243 Object p = parameters[i];244 String paramAsString = "null";245 if (p != null) {246 paramAsString = p.toString() + "<i>(" + p.getClass().getSimpleName() + ")</i> , ";247 }248 contentBuffer.append(paramAsString);249 }250 contentBuffer.append("</h2>");251 }252 if (hasReporterOutput || hasThrowable) {253 contentBuffer.append("<div class='leftContent' style='float: left; width: 70%;'>");254 contentBuffer.append("<h3>Test Log</h3>");255 for (String line : msgs) {256 BaseLog logLine = new BaseLog(line);257 if (logLine.getScreen() != null) {258 imgForFilmStrip.add(logLine.getScreenURL());259 }260 String htmllog = logLine.getMsg();261 // Attaching ralogId to each of the page title.262 if ((logLine.getHref() != null) && (logLine.getHref().length() > 1)) {263 htmllog = "<a href='../" + logLine.getHref() + "' title='" + logLine.getLocation() + "' >"264 + (StringUtils.isNotEmpty(htmllog) ? htmllog : "Page Source") + "</a>";265 266 }267 // Don't output blank message w/o any Href.268 if ((logLine.getHref() != null) || logLine.getMsg() != null && !logLine.getMsg().isEmpty()) {269 contentBuffer.append(htmllog);270 contentBuffer.append("<br/>");271 }272 }273 if (hasThrowable) {274 generateExceptionReport(exception, result.getMethod(), contentBuffer);275 }276 }277 contentBuffer.append("</div>"); // end of278 // leftContent279 contentBuffer.append("<div class='filmStripContainer' style='float: right; width: 100%;'>");280 contentBuffer.append("<b>Preview</b>");281 contentBuffer.append("<div class=\"filmStrip\">");282 contentBuffer.append("<ul>");283 for (String imgPath : imgForFilmStrip) {284 contentBuffer.append("<li>");285 contentBuffer.append("<a href=\"../" + imgPath + "\" > <img src=\"../" + imgPath286 + "\" width=\"200\" height=\"200\" /> </a>");287 contentBuffer.append("</li>");288 }289 contentBuffer.append("</ul>");290 contentBuffer.append("</div>");291 contentBuffer.append("</div>");292 }293 contentBuffer.append("<div class='clear_both'></div>");294 // Not logging the return value, because it will clog the logs295 logger.exiting();296 return contentBuffer.toString();297 }298 protected void generateExceptionReport(Throwable exception, ITestNGMethod method, StringBuilder contentBuffer) {299 logger.entering(new Object[] { exception, method, contentBuffer });300 Throwable fortile = exception;301 String title = fortile.getMessage();302 if (title == null) {303 title = "Encountered problems when attempting to extract a meaningful Root cause.";304 if (fortile.getCause() != null && !fortile.getCause().getMessage().trim().isEmpty()) {305 title = fortile.getCause().getMessage();306 }307 }308 generateExceptionReport(exception, method, title, contentBuffer);309 logger.exiting();310 }311 private void generateExceptionReport(Throwable exception, ITestNGMethod method, String title,312 StringBuilder contentBuffer) {313 generateTheStackTrace(exception, method, title, contentBuffer);314 }315 private void generateTheStackTrace(Throwable exception, ITestNGMethod method, String title,316 StringBuilder contentBuffer) {317 logger.entering(new Object[] { exception, method, title, contentBuffer });318 contentBuffer.append(" <div class='stContainer' >" + exception.getClass() + ":" + title// escape(title)319 + "<a class='exceptionlnk' href='#'>(+)</a>");320 contentBuffer.append("<div class='exception' style='display:none'>");321 StackTraceElement[] s1 = exception.getStackTrace();322 Throwable t2 = exception.getCause();323 if ((t2 != null) && (t2.equals(exception))) {324 t2 = null;325 }326 for (int x = 0; x < s1.length; x++) {327 contentBuffer.append((x > 0 ? "<br/>at " : "") + escape(s1[x].toString()));328 }329 if (t2 != null) {330 generateExceptionReport(t2, method, "Caused by " + t2.getLocalizedMessage(), contentBuffer);331 }332 contentBuffer.append("</div></div>");333 logger.exiting();334 }335 private static String escape(String string) {336 if (null == string) {337 return string;338 }339 return string.replaceAll("<", "&lt;").replaceAll(">", "&gt;");340 }341 private void createMethod(ITestContext ctx, ITestNGMethod method, String outdir) {342 logger.entering(new Object[] { ctx, method, outdir });343 try {344 File f = new File(outdir + "/html/", method.getId() + ".html");345 logger.fine("generating method " + f.getAbsolutePath());346 Writer fileSystemWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(347 new FileOutputStream(f), "UTF8")));348 Template t = ve.getTemplate("/templates/method.part.html");349 Set<ITestResult> passed = ctx.getPassedTests().getResults(method);350 for (ITestResult result : passed) {351 VelocityContext context = new VelocityContext();352 context.put("method", method);353 context.put("status", "passed");354 context.put("result", result);355 context.put("content", getContent(result));356 StringWriter writer = new StringWriter();357 t.merge(context, writer);358 fileSystemWriter.write(writer.toString());359 }360 Set<ITestResult> failed = ctx.getFailedTests().getResults(method);361 for (ITestResult result : failed) {362 VelocityContext context = new VelocityContext();363 context.put("method", method);364 context.put("status", "failed");365 context.put("result", result);366 context.put("content", getContent(result));367 StringWriter writer = new StringWriter();368 t.merge(context, writer);369 fileSystemWriter.write(writer.toString());370 }371 Set<ITestResult> skipped = ctx.getSkippedTests().getResults(method);372 for (ITestResult result : skipped) {373 VelocityContext context = new VelocityContext();374 context.put("method", method);375 context.put("status", "skipped");376 context.put("result", result);377 context.put("content", getContent(result));378 StringWriter writer = new StringWriter();379 t.merge(context, writer);380 fileSystemWriter.write(writer.toString());381 }382 fileSystemWriter.flush();383 fileSystemWriter.close();384 } catch (Exception e) { // catching exception because velocity throws385 // that and we can't change it386 ReporterException re = new ReporterException(e);387 throw re;388 }389 logger.exiting();390 }391 private List<Line> createSummary(List<ISuite> suites) {392 logger.entering(suites);393 try {394 Template t = ve.getTemplate("/templates/summaryTabs.part.html");395 VelocityContext context = new VelocityContext();396 List<GroupingView> views = new ArrayList<GroupingView>();397 GroupingView view = new GroupingView("managerView", "per class", "Overview organized per class", ve,398 suites, new ByClassSplitter());399 views.add(view);400 GroupingView view2 = new GroupingView("managerView2", "per package", "Overview organized per package", ve,401 suites, new ByPackageSplitter());402 views.add(view2);403 GroupingView view3 = new GroupingView("managerView3", "per method", "Overview organized per method", ve,404 suites, new ByMethodSplitter());405 views.add(view3);406 GroupingView view9 = new GroupingView("managerView9", "per testName", "Overview organized per testName",407 ve, suites, new ByTestNameSplitter());408 views.add(view9);409 /*********************************/410 Filter f2 = new StateFilter(ITestResult.FAILURE);411 GroupingView view6 = new GroupingView("managerView6", "failed methods only",412 "Overview organized per failed methods", ve, suites, new ByMethodSplitter(), f2);413 views.add(view6);414 GroupingView view7 = new GroupingView("managerView7", "per group", "Overview organized per group", ve,415 suites, new ByGroupSplitter());416 views.add(view7);417 context.put("views", views);418 StringWriter writer = new StringWriter();419 t.merge(context, writer);420 out.write(writer.toString());421 List<Line> lines = new ArrayList<Line>();422 for (GroupingView v : views) {423 for (Line line : v.getSplitter().getLines().values()) {424 lines.add(line);425 }426 }427 logger.exiting(lines);428 return lines;429 } catch (Exception e) {430 ReporterException re = new ReporterException("Error occurred while generating report summary", e);431 throw re;432 }433 }434 /** Starts HTML stream */435 protected void startHtml(PrintWriter out) {436 logger.entering(out);437 try {438 Template t = ve.getTemplate("/templates/header.part.html");439 VelocityContext context = new VelocityContext();440 StringBuilder output = new StringBuilder();441 for (Entry<String, String> temp : ConfigSummaryData.getConfigSummary().entrySet()) {442 Entry<String, String> formattedTemp = ReporterDateFormatter.formatReportDataForBrowsableReports(temp);443 output.append(formattedTemp.getKey()).append(" : <b>").append(formattedTemp.getValue()).append("</b><br>");444 }445 context.put("configSummary", output.toString());446 StringWriter writer = new StringWriter();447 t.merge(context, writer);448 out.write(writer.toString());449 } catch (Exception e) {450 logger.log(Level.SEVERE, e.getMessage(), e);451 }452 logger.exiting();453 }454 private void endHtml(PrintWriter out) {455 logger.entering(out);456 try {457 Template t = ve.getTemplate("/templates/footer.part.html");458 VelocityContext context = new VelocityContext();459 StringWriter writer = new StringWriter();460 t.merge(context, writer);461 out.write(writer.toString());462 } catch (Exception e) {463 ReporterException re = new ReporterException(e);464 throw re;465 }466 logger.exiting();467 }468 protected PrintWriter createWriter(String outdir) {469 logger.entering(outdir);470 File f = new File(outdir + "/html/", "report.html");471 if (f.exists()) {472 Format formatter = new SimpleDateFormat("MM-dd-yyyy-HH-mm");473 String currentDate = formatter.format(new Date());474 f.renameTo(new File(outdir + "/html/", "report-" + currentDate + ".html")); // NOSONAR475 }476 logger.info("generating report " + f.getAbsolutePath());477 try {478 PrintWriter pw = new PrintWriter(479 new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF8")));480 logger.exiting(pw);481 return pw;482 } catch (Exception e) {...

Full Screen

Full Screen

createWriter

Using AI Code Generation

copy

Full Screen

1HtmlReporterListener listener = new HtmlReporterListener();2listener.createWriter();3HtmlReporterListener listener = new HtmlReporterListener();4listener.createWriter("path/to/directory");5HtmlReporterListener listener = new HtmlReporterListener();6listener.createWriter("path/to/directory", "filename");7HtmlReporterListener listener = new HtmlReporterListener();8listener.createWriter("path/to/directory", "filename", "html");9HtmlReporterListener listener = new HtmlReporterListener();10listener.createWriter("path/to/directory", "filename", "html", "markdown");11HtmlReporterListener listener = new HtmlReporterListener();12listener.createWriter("path/to/directory", "filename", "html", "markdown", "UTF-8");13HtmlReporterListener listener = new HtmlReporterListener();14listener.createWriter("path/to/directory", "filename", "html", "markdown", "UTF-8", true);15HtmlReporterListener listener = new HtmlReporterListener();16listener.createWriter("path/to/directory", "filename", "html", "markdown", "UTF-8", true, true);17HtmlReporterListener listener = new HtmlReporterListener();18listener.createWriter("path/to/directory", "filename", "html", "markdown", "UTF-8", true, true, true);19HtmlReporterListener listener = new HtmlReporterListener();20listener.createWriter("path/to/directory", "filename", "html", "markdown", "UTF-8", true, true, true, true);

Full Screen

Full Screen

createWriter

Using AI Code Generation

copy

Full Screen

1package com.paypal.selion.reports.reporter;2import java.io.File;3import java.io.IOException;4import java.util.HashMap;5import java.util.Map;6import org.testng.ITestContext;7import org.testng.ITestListener;8import org.testng.ITestResult;9import com.paypal.selion.internal.reports.html.HtmlReporterListener;10public class CustomHtmlReporterListener implements ITestListener {11 private Map<String, HtmlReporterListener> writers = new HashMap<String, HtmlReporterListener>();12 public void onTestStart(ITestResult result) {13 String className = result.getTestClass().getName();14 if (!writers.containsKey(className)) {15 try {16 HtmlReporterListener htmlReporterListener = new HtmlReporterListener(17 new File("target/surefire-reports/" + className + ".html"));18 writers.put(className, htmlReporterListener);19 } catch (IOException e) {20 e.printStackTrace();21 }22 }23 writers.get(className).onTestStart(result);24 }25 public void onTestSuccess(ITestResult result) {26 writers.get(result.getTestClass().getName()).onTestSuccess(result);27 }28 public void onTestFailure(ITestResult result) {29 writers.get(result.getTestClass().getName()).onTestFailure(result);30 }31 public void onTestSkipped(ITestResult result) {32 writers.get(result.getTestClass().getName()).onTestSkipped(result);33 }34 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {35 writers.get(result.getTestClass().getName()).onTestFailedButWithinSuccessPercentage(result);36 }37 public void onStart(ITestContext context) {38 writers.clear();39 }40 public void onFinish(ITestContext context) {41 for (HtmlReporterListener htmlReporterListener : writers.values()) {42 htmlReporterListener.onFinish(context);43 }44 }45}46package com.paypal.selion.reports.reporter;47import java.io.File;48import java.io.IOException;49import java.util.HashMap;50import java.util.Map;51import org.testng.ITestContext;52import org.testng.ITestListener;53import org.testng.ITestResult;54import com.paypal.selion.internal.reports

Full Screen

Full Screen

createWriter

Using AI Code Generation

copy

Full Screen

1public class HtmlReporterListener implements IReporterListener {2 private static final Logger LOGGER = Logger.getLogger(HtmlReporterListener.class.getName());3 private static final String REPORT_FOLDER = "reports";4 private static final String REPORT_FILE_NAME = "report.html";5 private static final String REPORT_CSS_FILE_NAME = "report.css";6 private static final String REPORT_JAVASCRIPT_FILE_NAME = "report.js";7 private static final String REPORT_JQUERY_FILE_NAME = "jquery-1.11.1.min.js";8 private static final String REPORT_JQUERY_UI_FILE_NAME = "jquery-ui.min.js";9 private static final String REPORT_JQUERY_UI_CSS_FILE_NAME = "jquery-ui.min.css";10 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_FILE_NAME = "jquery-ui.min.css";11 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";12 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_2X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";13 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_3X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";14 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_4X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";15 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_5X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";16 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_6X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";17 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_7X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";18 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_8X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";19 private static final String REPORT_JQUERY_UI_CSS_SMOOTHNESS_IMAGES_9X_FILE_NAME = "images/ui-bg_flat_0_aaaaaa_40x100.png";

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful