How to use getMetaInfo method of com.consol.citrus.report.HtmlReporter class

Best Citrus code snippet using com.consol.citrus.report.HtmlReporter.getMetaInfo

Source:HtmlReporter.java Github

copy

Full Screen

...74 ResultDetail detail = Optional.ofNullable(details.get(result.getTestName())).orElse(new ResultDetail());75 Properties detailProps = new Properties();76 detailProps.put("test.style.class", result.getResult().toLowerCase());77 detailProps.put("test.case.name", result.getTestName());78 detailProps.put("test.author", !StringUtils.hasText(detail.getMetaInfo().getAuthor()) ? emptyString : detail.getMetaInfo().getAuthor());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(">", "&gt;").replaceAll("<", "&lt;") + "</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() {...

Full Screen

Full Screen

getMetaInfo

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.report.HtmlReporter2import com.consol.citrus.report.TestActionListeners3import com.consol.citrus.report.TestListeners4import com.consol.citrus.report.TestSuiteListeners5def report = new HtmlReporter()6report.setTestSuiteListeners(TestSuiteListeners.TESTSUITE_LISTENERS)7report.setTestListeners(TestListeners.TEST_LISTENERS)8report.setTestActionListeners(TestActionListeners.TESTACTION_LISTENERS)9def metaInfo = report.getMetaInfo("target/citrus-reports/report.html")10println metaInfo.get("author")11println metaInfo.get("description")

Full Screen

Full Screen

getMetaInfo

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;2import com.consol.citrus.report.HtmlReporter;3public class TestNameFromHtmlReport extends JUnit4CitrusTestDesigner {4 public void run() {5 String testName = HtmlReporter.getMetaInfo("testName");6 testName(testName);7 echo("Hello World!");8 }9}

Full Screen

Full Screen

getMetaInfo

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.report.HtmlReporter2import java.lang.System3def metaInfo = HtmlReporter.getMetaInfo(this)4def author = metaInfo.get("author")5System.out.println(author)6import com.consol.citrus.report.HtmlReporter7import java.lang.System8def metaInfo = HtmlReporter.getMetaInfo(this)9def author = metaInfo.get("author")10System.out.println(author)11import com.consol.citrus.report.HtmlReporter12import java.lang.System13val metaInfo = HtmlReporter.getMetaInfo(this)14val author = metaInfo.get("author")15System.out.println(author)

Full Screen

Full Screen

getMetaInfo

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.runner.TestRunner2import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner3import com.consol.citrus.report.HtmlReporter4import com.consol.citrus.report.TestActionListeners5import com.consol.citrus.report.TestActionListeners.TestActionListener6class GetMetaInfoTest extends TestNGCitrusTestRunner {7 void configure() {8 variable('metaInfo', getMetaInfo(htmlReporter(), 'testName'))9 echo('${metaInfo}')10 assertThat('${metaInfo}').contains('Test name: testName')11 }12 def htmlReporter() {13 return new HtmlReporter().testActionListeners(new TestActionListeners().listeners(new TestActionListener() {14 void onTestStart(String testName) {15 echo("Test started: ${testName}")16 }17 void onTestFinish(String testName) {18 echo("Test finished: ${testName}")19 }20 }))21 }22}

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