Best Citrus code snippet using com.consol.citrus.report.HtmlReporter.getDescription
Source:HtmlReporter.java
...79 detailProps.put("test.status", detail.getMetaInfo().getStatus().toString());80 detailProps.put("test.creation.date", detail.getMetaInfo().getCreationDate() == null ? emptyString : dateFormat.format(detail.getMetaInfo().getCreationDate()));81 detailProps.put("test.updater", !StringUtils.hasText(detail.getMetaInfo().getLastUpdatedBy()) ? emptyString : detail.getMetaInfo().getLastUpdatedBy());82 detailProps.put("test.update.date", detail.getMetaInfo().getLastUpdatedOn() == null ? emptyString : dateFormat.format(detail.getMetaInfo().getLastUpdatedOn()));83 detailProps.put("test.description", !StringUtils.hasText(detail.getDescription()) ? emptyString : detail.getDescription());84 detailProps.put("test.result", result.getResult());85 reportDetails.append(PropertyUtils.replacePropertiesInString(testDetails, detailProps));86 if (result.isFailed() && result.getCause() != null) {87 reportDetails.append(getStackTraceHtml(result.getCause()));88 }89 });90 Properties reportProps = new Properties();91 reportProps.put("test.cnt", Integer.toString(getTestResults().getSize()));92 reportProps.put("skipped.test.cnt", Integer.toString(getTestResults().getSkipped()));93 reportProps.put("skipped.test.pct", getTestResults().getSkippedPercentage());94 reportProps.put("failed.test.cnt", Integer.toString(getTestResults().getFailed()));95 reportProps.put("failed.test.pct", getTestResults().getFailedPercentage());96 reportProps.put("success.test.cnt", Integer.toString(getTestResults().getSuccess()));97 reportProps.put("success.test.pct", getTestResults().getSuccessPercentage());98 reportProps.put("test.results", reportDetails.toString());99 reportProps.put("logo.data", getLogoImageData());100 return PropertyUtils.replacePropertiesInString(FileUtils.readToString(FileUtils.getFileResource(reportTemplate)), reportProps);101 } catch (IOException e) {102 throw new CitrusRuntimeException("Failed to generate HTML test report", e);103 }104 }105 /**106 * Reads citrus logo png image and converts to base64 encoded string for inline HTML image display.107 * @return108 * @throws IOException109 */110 private String getLogoImageData() {111 ByteArrayOutputStream os = new ByteArrayOutputStream();112 BufferedInputStream reader = null;113 114 try {115 reader = new BufferedInputStream(FileUtils.getFileResource(logo).getInputStream());116 117 byte[] contents = new byte[1024];118 while( reader.read(contents) != -1) {119 os.write(contents);120 }121 } catch(IOException e) {122 log.warn("Failed to add logo image data to HTML report", e);123 } finally {124 if (reader != null) {125 try {126 reader.close();127 } catch(IOException ex) {128 log.warn("Failed to close logo image resource for HTML report", ex);129 }130 }131 132 try {133 os.flush();134 } catch(IOException ex) {135 log.warn("Failed to flush logo image stream for HTML report", ex);136 }137 }138 139 return Base64.encodeBase64String(os.toByteArray());140 }141 /**142 * Gets the code section from test case XML which is responsible for the143 * error.144 * @param cause the error cause.145 * @return146 */147 private String getCodeSnippetHtml(Throwable cause) {148 StringBuilder codeSnippet = new StringBuilder();149 BufferedReader reader = null;150 151 try {152 if (cause instanceof CitrusRuntimeException) {153 CitrusRuntimeException ex = (CitrusRuntimeException) cause;154 if (!ex.getFailureStack().isEmpty()) {155 FailureStackElement stackElement = ex.getFailureStack().pop();156 if (stackElement.getLineNumberStart() > 0) {157 reader = new BufferedReader(new FileReader(158 new ClassPathResource(stackElement.getTestFilePath() + ".xml").getFile()));159 160 codeSnippet.append("<div class=\"code-snippet\">");161 codeSnippet.append("<h2 class=\"code-title\">" + stackElement.getTestFilePath() + ".xml</h2>");162 163 String line;164 String codeStyle;165 int lineIndex = 1;166 int snippetOffset = 5;167 while ((line = reader.readLine()) != null) {168 if (lineIndex >= stackElement.getLineNumberStart() - snippetOffset && 169 lineIndex < stackElement.getLineNumberStart() || 170 lineIndex > stackElement.getLineNumberEnd() && 171 lineIndex <= stackElement.getLineNumberEnd() + snippetOffset) {172 codeStyle = "code";173 } else if (lineIndex >= stackElement.getLineNumberStart() && 174 lineIndex <= stackElement.getLineNumberEnd()) {175 codeStyle = "code-failed";176 } else {177 codeStyle = "";178 }179 180 if (StringUtils.hasText(codeStyle)) {181 codeSnippet.append("<pre class=\"" + codeStyle +"\"><span class=\"line-number\">" + lineIndex + ":</span>" + 182 line.replaceAll(">", ">").replaceAll("<", "<") + "</pre>");183 }184 185 lineIndex++;186 187 }188 189 codeSnippet.append("</div>");190 }191 }192 }193 } catch (IOException e) {194 log.error("Failed to construct HTML code snippet", e);195 } finally {196 if (reader != null) {197 try {198 reader.close();199 } catch (IOException e) {200 log.warn("Failed to close test file", e);201 }202 }203 }204 205 return codeSnippet.toString();206 }207 /**208 * Construct HTML code snippet for stack trace information.209 * @param cause the causing error.210 * @return211 */212 private String getStackTraceHtml(Throwable cause) {213 StringBuilder stackTraceBuilder = new StringBuilder();214 stackTraceBuilder.append(cause.getClass().getName())215 .append(": ")216 .append(cause.getMessage())217 .append("\n ");218 for (int i = 0; i < cause.getStackTrace().length; i++) {219 stackTraceBuilder.append("\n\t at ");220 stackTraceBuilder.append(cause.getStackTrace()[i]);221 }222 223 return "<tr><td colspan=\"2\">" +224 "<div class=\"error-detail\"><pre>" + stackTraceBuilder.toString() + 225 "</pre>" + getCodeSnippetHtml(cause) + "</div></td></tr>";226 }227 @Override228 public void onTestSuccess(TestCase test) {229 details.put(test.getName(), ResultDetail.build(test));230 super.onTestSuccess(test);231 }232 233 @Override234 public void onTestFailure(TestCase test, Throwable cause) {235 details.put(test.getName(), ResultDetail.build(test));236 super.onTestFailure(test, cause);237 }238 239 @Override240 public void onTestSkipped(TestCase test) {241 details.put(test.getName(), ResultDetail.build(test));242 super.onTestSkipped(test);243 }244 /**245 * Sets the logo.246 * @param logo the logo to set247 */248 public void setLogo(String logo) {249 this.logo = logo;250 }251 /**252 * Sets the outputDirectory property.253 *254 * @param outputDirectory255 * @deprecated in favor of using {@link AbstractTestReporter#setReportDirectory}.256 */257 @Deprecated258 public void setOutputDirectory(String outputDirectory) {259 setReportDirectory(outputDirectory);260 }261 @Override262 public String getReportDirectory() {263 if (StringUtils.hasText(outputDirectory)) {264 return outputDirectory;265 }266 return super.getReportDirectory();267 }268 /**269 * Sets the reportFileName property.270 *271 * @param reportFileName272 */273 public void setReportFileName(String reportFileName) {274 this.reportFileName = reportFileName;275 }276 /**277 * Gets the reportFileName.278 *279 * @return280 */281 @Override282 public String getReportFileName() {283 return reportFileName;284 }285 /**286 * Sets the dateFormat property.287 *288 * @param dateFormat289 */290 public void setDateFormat(DateFormat dateFormat) {291 this.dateFormat = dateFormat;292 }293 /**294 * Sets the reportTemplate property.295 *296 * @param reportTemplate297 */298 public void setReportTemplate(String reportTemplate) {299 this.reportTemplate = reportTemplate;300 }301 /**302 * Sets the testDetailTemplate property.303 *304 * @param testDetailTemplate305 */306 public void setTestDetailTemplate(String testDetailTemplate) {307 this.testDetailTemplate = testDetailTemplate;308 }309 /**310 * Sets the enabled property.311 * @param enabled312 */313 public void setEnabled(boolean enabled) {314 this.enabled = String.valueOf(enabled);315 }316 @Override317 protected boolean isEnabled() {318 return StringUtils.hasText(enabled) && enabled.equalsIgnoreCase(Boolean.TRUE.toString());319 }320 /**321 * Value object holding test specific data for HTML report generation. 322 */323 private static class ResultDetail {324 /** The meta info of the underlying test */325 private TestCaseMetaInfo metaInfo = new TestCaseMetaInfo();326 327 /** Description of the test */328 private String description;329 330 /**331 * Builds a new result detail from test case.332 * @param test the test case.333 * @return the result detail.334 */335 public static ResultDetail build(TestCase test) {336 ResultDetail detail = new ResultDetail();337 detail.setDescription(test.getDescription());338 detail.setMetaInfo(test.getMetaInfo());339 340 return detail;341 }342 /**343 * Gets the test meta information.344 * @return the metaInfo345 */346 public TestCaseMetaInfo getMetaInfo() {347 return metaInfo;348 }349 /**350 * Sets the test meta information.351 * @param metaInfo the metaInfo to set352 */353 public void setMetaInfo(TestCaseMetaInfo metaInfo) {354 this.metaInfo = metaInfo;355 }356 /**357 * Gets the test description.358 * @return the description359 */360 public String getDescription() {361 return description;362 }363 /**364 * Sets the test description.365 * @param description the description to set366 */367 public void setDescription(String description) {368 this.description = description;369 }370 }371}...
getDescription
Using AI Code Generation
1reporter.setDescription(reporter.getDescription());2reporter.setDescription("This is the description of the HTML report");3reporter.getReportName();4reporter.setReportName("HTML report");5reporter.getReportDirectory();6reporter.setReportDirectory("C:\report");7reporter.getReportEncoding();8reporter.setReportEncoding("UTF-8");9reporter.isAutoCreateDirectory();10reporter.setAutoCreateDirectory(true);11reporter.getReportDateFormat();12reporter.setReportDateFormat("YYYY-MM-dd");13reporter.getReportTimeFormat();14reporter.setReportTimeFormat("HH:mm:ss");15reporter.getReportDateTimeFormat();16reporter.setReportDateTimeFormat("YYYY-MM-dd HH:mm:ss");17reporter.getReportStylesheet();18reporter.setReportStylesheet("C:\
getDescription
Using AI Code Generation
1reporter.getDescription()2reporter.setDescription("Hello World")3package com.consol.citrus.dsl.testng;4import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;5import com.consol.citrus.report.HtmlReporter;6import org.testng.annotations.Test;7public class HtmlReporterJavaITest extends TestNGCitrusTestDesigner {8 public void htmlReporterJavaITest() {9 description("HtmlReporter test");10 echo("Hello HtmlReporter!");11 variable("htmlReporter", new HtmlReporter());12 $(htmlReporter).description("Hello World");13 }14}15package com.consol.citrus.dsl.testng;16import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;17import com.consol.citrus.report.HtmlReporter;18import org.springframework.beans.factory.annotation.Autowired;19import org.testng.annotations.Test;20public class HtmlReporterJavaITest extends TestNGCitrusTestDesigner {21 private HtmlReporter htmlReporter;22 public void htmlReporterJavaITest() {23 description("HtmlReporter test");24 echo("Hello HtmlReporter!");25 $(htmlReporter).description("Hello World");26 }27}28package com.consol.citrus.dsl.testng;29import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;30import com.consol.citrus.report.HtmlReporter;31import org.springframework.beans.factory.annotation.Autowired;32import org.testng.annotations.Test;33public class HtmlReporterJavaITest extends TestNGCitrusTestDesigner {34 private HtmlReporter htmlReporter;35 public void htmlReporterJavaITest() {36 description("HtmlReporter test");37 echo("Hello HtmlReporter!");38 $(htmlReporter).description("Hello World");39 }40}41package com.consol.citrus.dsl.testng;42import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;
getDescription
Using AI Code Generation
1String description = new HtmlReporter().getDescription("testDescription", context);2System.out.println(description);3String description = new HtmlReporter().getDescription(context);4System.out.println(description);5String description = new HtmlReporter().getDescription();6System.out.println(description);
getDescription
Using AI Code Generation
1import com.consol.citrus.report.HtmlReporter;2import com.consol.citrus.report.TestActionListeners;3import com.consol.citrus.report.TestActionListenersFactory;4public class Test1 {5 public void test1() {6 description = HtmlReporter.getDescription();7 echo(description);8 }9 public void test2() {10 description = HtmlReporter.getDescription();11 echo(description);12 }13}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!