Best Carina code snippet using com.qaprosoft.carina.core.foundation.report.email.EmailReportGenerator.isCucumberReportFolderExists
Source:EmailReportGenerator.java
...14 * limitations under the License.15 */16package com.qaprosoft.carina.core.foundation.report.email;17import static com.qaprosoft.carina.core.foundation.cucumber.CucumberRunner.getCucumberReportResultLink;18import static com.qaprosoft.carina.core.foundation.cucumber.CucumberRunner.isCucumberReportFolderExists;19import static com.qaprosoft.carina.core.foundation.cucumber.CucumberRunner.isCucumberTest;20import static com.qaprosoft.carina.core.foundation.cucumber.CucumberRunner.useJSinCucumberReport;21import static com.qaprosoft.carina.core.foundation.report.ReportContext.isArtifactsFolderExists;22import java.util.Collections;23import java.util.List;24import org.apache.commons.collections.CollectionUtils;25import org.apache.commons.lang3.StringEscapeUtils;26import org.apache.commons.lang3.StringUtils;27import org.apache.log4j.Logger;28import com.qaprosoft.carina.core.foundation.report.TestResultItem;29import com.qaprosoft.carina.core.foundation.report.TestResultType;30import com.qaprosoft.carina.core.foundation.utils.Configuration;31import com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter;32import com.qaprosoft.carina.core.foundation.utils.R;33import com.qaprosoft.carina.core.foundation.utils.SpecialKeywords;34/**35 * EmailReportGenerator generates emailable report using data from test suite log.36 * 37 * @author Alex Khursevich38 */39public class EmailReportGenerator40{41 protected static final Logger LOGGER = Logger.getLogger(EmailReportGenerator.class);42 43 private static String CONTAINER = R.EMAIL.get("container");44 private static String PACKAGE_TR = R.EMAIL.get("package_tr");45 private static String PASS_TEST_LOG_DEMO_TR = R.EMAIL.get("pass_test_log_demo_tr");46 private static String FAIL_TEST_LOG_DEMO_TR = R.EMAIL.get("fail_test_log_demo_tr");47 private static String BUG_TEST_LOG_DEMO_TR = R.EMAIL.get("bug_test_log_demo_tr");48 private static String SKIP_TEST_LOG_DEMO_TR = R.EMAIL.get("skip_test_log_demo_tr");49 private static String FAIL_CONFIG_LOG_DEMO_TR = R.EMAIL.get("fail_config_log_demo_tr");50 private static String PASS_TEST_LOG_TR = R.EMAIL.get("pass_test_log_tr");51 private static String FAIL_TEST_LOG_TR = R.EMAIL.get("fail_test_log_tr");52 private static String BUG_TEST_LOG_TR = R.EMAIL.get("bug_test_log_tr");53 private static String SKIP_TEST_LOG_TR = R.EMAIL.get("skip_test_log_tr");54 private static String FAIL_CONFIG_LOG_TR = R.EMAIL.get("fail_config_log_tr"); 55 private static String CREATED_ITEMS_LIST = R.EMAIL.get("created_items_list");56 private static String CREATED_ITEM = R.EMAIL.get("created_item");57 private static final String TITLE_PLACEHOLDER = "${title}";58 private static final String ENV_PLACEHOLDER = "${env}";59 private static final String DEVICE_PLACEHOLDER = "${device}"; 60 private static final String BROWSER_PLACEHOLDER = "${browser}";61 private static final String VERSION_PLACEHOLDER = "${version}";62 private static final String FINISH_DATE_PLACEHOLDER = "${finish_date}";63 private static final String ELAPSED_TIME_PLACEHOLDER = "${elapsed_time}";64 private static final String CI_TEST_JOB = "${ci_test_job}";65 private static final String PASS_COUNT_PLACEHOLDER = "${pass_count}";66 private static final String FAIL_COUNT_PLACEHOLDER = "${fail_count}";67 private static final String SKIP_COUNT_PLACEHOLDER = "${skip_count}";68 private static final String PASS_RATE_PLACEHOLDER = "${pass_rate}";69 private static final String RESULTS_PLACEHOLDER = "${result_rows}";70 private static final String PACKAGE_NAME_PLACEHOLDER = "${package_name}";71 private static final String TEST_NAME_PLACEHOLDER = "${test_name}";72 private static final String FAIL_REASON_PLACEHOLDER = "${fail_reason}";73 private static final String SKIP_REASON_PLACEHOLDER = "${skip_reason}";74 private static final String FAIL_CONFIG_REASON_PLACEHOLDER = "${fail_config_reason}"; 75 private static final String SCREENSHOTS_URL_PLACEHOLDER = "${screenshots_url}";76 private static final String LOG_URL_PLACEHOLDER = "${log_url}";77 private static final String CREATED_ITEMS_LIST_PLACEHOLDER = "${created_items_list}";78 private static final String CREATED_ITEM_PLACEHOLDER = "${created_item}";79 private static final String BUG_URL_PLACEHOLDER = "${bug_url}";80 private static final String BUG_ID_PLACEHOLDER = "${bug_id}";81 private static final int MESSAGE_LIMIT = R.EMAIL.getInt("fail_description_limit");82 //Cucumber section83 private static final String CUCUMBER_RESULTS_PLACEHOLDER = "${cucumber_results}";84 private static final String CUCUMBER_JS_PLACEHOLDER ="${js_placeholder}";85 private static boolean INCLUDE_PASS = R.EMAIL.getBoolean("include_pass");86 private static boolean INCLUDE_FAIL = R.EMAIL.getBoolean("include_fail");87 private static boolean INCLUDE_SKIP = R.EMAIL.getBoolean("include_skip");88 private String emailBody = CONTAINER;89 private StringBuilder testResults = null;90 private int passCount = 0;91 private int failCount = 0;92 private int skipCount = 0;93 94 public EmailReportGenerator(String title, String url, String version, String device, String browser, String finishDate, String elapsedTime, String ciTestJob,95 List<TestResultItem> testResultItems, List<String> createdItems)96 {97 emailBody = emailBody.replace(TITLE_PLACEHOLDER, title);98 emailBody = emailBody.replace(ENV_PLACEHOLDER, url);99 emailBody = emailBody.replace(DEVICE_PLACEHOLDER, device);100 emailBody = emailBody.replace(VERSION_PLACEHOLDER, version);101 emailBody = emailBody.replace(BROWSER_PLACEHOLDER, browser);102 emailBody = emailBody.replace(FINISH_DATE_PLACEHOLDER, finishDate);103 emailBody = emailBody.replace(ELAPSED_TIME_PLACEHOLDER, elapsedTime);104 emailBody = emailBody.replace(CI_TEST_JOB, !StringUtils.isEmpty(ciTestJob) ? ciTestJob : "");105 emailBody = emailBody.replace(RESULTS_PLACEHOLDER, getTestResultsList(testResultItems));106 emailBody = emailBody.replace(PASS_COUNT_PLACEHOLDER, String.valueOf(passCount));107 emailBody = emailBody.replace(FAIL_COUNT_PLACEHOLDER, String.valueOf(failCount));108 emailBody = emailBody.replace(SKIP_COUNT_PLACEHOLDER, String.valueOf(skipCount));109 emailBody = emailBody.replace(PASS_RATE_PLACEHOLDER, String.valueOf(getSuccessRate()));110 emailBody = emailBody.replace(CREATED_ITEMS_LIST_PLACEHOLDER, getCreatedItemsList(createdItems));111 //Cucumber section112 emailBody = emailBody.replace(CUCUMBER_RESULTS_PLACEHOLDER, getCucumberResults());113 emailBody = emailBody.replace(CUCUMBER_JS_PLACEHOLDER, getCucumberJavaScript());114 }115 public String getEmailBody()116 {117 return emailBody;118 }119 private String getTestResultsList(List<TestResultItem> testResultItems)120 {121 if (testResultItems.size() > 0)122 {123 if (Configuration.getBoolean(Parameter.RESULT_SORTING)) {124 125 //TODO: identify way to synch config failure with testNG method126 Collections.sort(testResultItems, new EmailReportItemComparator());127 }128 129 String packageName = "";130 testResults = new StringBuilder();131 for (TestResultItem testResultItem : testResultItems)132 {133 if (!testResultItem.isConfig() && !packageName.equals(testResultItem.getPack()))134 {135 packageName = testResultItem.getPack();136 testResults.append(PACKAGE_TR.replace(PACKAGE_NAME_PLACEHOLDER, packageName));137 }138 testResults.append(getTestRow(testResultItem));139 }140 }141 return testResults != null ? testResults.toString() : "";142 }143 private String getTestRow(TestResultItem testResultItem)144 {145 String result = "";146 String failReason = "";147 if (testResultItem.getResult().name().equalsIgnoreCase("FAIL")) {148 if(INCLUDE_FAIL){149 if (testResultItem.isConfig()) {150 result = testResultItem.getLinkToScreenshots() != null ? FAIL_CONFIG_LOG_DEMO_TR : FAIL_CONFIG_LOG_TR;151 result = result.replace(TEST_NAME_PLACEHOLDER, testResultItem.getTest());152 153 failReason = testResultItem.getFailReason();154 if (!StringUtils.isEmpty(failReason))155 {156 // Make description more compact for email report 157 failReason = failReason.length() > MESSAGE_LIMIT ? (failReason.substring(0, MESSAGE_LIMIT) + "...") : failReason;158 result = result.replace(FAIL_CONFIG_REASON_PLACEHOLDER, formatFailReasonAsHtml(failReason));159 }160 else161 {162 result = result.replace(FAIL_CONFIG_REASON_PLACEHOLDER, "Undefined failure: contact qa engineer!");163 }164 } else {165 if (Configuration.getBoolean(Parameter.TRACK_KNOWN_ISSUES) && !testResultItem.getJiraTickets().isEmpty())166 {167 result = testResultItem.getLinkToScreenshots() != null ? BUG_TEST_LOG_DEMO_TR : BUG_TEST_LOG_TR;168 }169 else170 {171 result = testResultItem.getLinkToScreenshots() != null ? FAIL_TEST_LOG_DEMO_TR : FAIL_TEST_LOG_TR;172 }173 174 result = result.replace(TEST_NAME_PLACEHOLDER, testResultItem.getTest());175 176 failReason = testResultItem.getFailReason();177 if (!StringUtils.isEmpty(failReason))178 {179 // Make description more compact for email report 180 failReason = failReason.length() > MESSAGE_LIMIT ? (failReason.substring(0, MESSAGE_LIMIT) + "...") : failReason;181 result = result.replace(FAIL_REASON_PLACEHOLDER, formatFailReasonAsHtml(failReason));182 }183 else184 {185 result = result.replace(FAIL_REASON_PLACEHOLDER, "Undefined failure: contact qa engineer!");186 }187 } 188 189 190 result = result.replace(LOG_URL_PLACEHOLDER, testResultItem.getLinkToLog());191 192 if(testResultItem.getLinkToScreenshots() != null)193 {194 result = result.replace(SCREENSHOTS_URL_PLACEHOLDER, testResultItem.getLinkToScreenshots());195 }196 }197 198 if (Configuration.getBoolean(Parameter.TRACK_KNOWN_ISSUES) && !testResultItem.getJiraTickets().isEmpty())199 {200 // do nothing201 } else202 failCount++;203 }204 if (testResultItem.getResult().name().equalsIgnoreCase("SKIP")) {205 failReason = testResultItem.getFailReason();206 if (!testResultItem.isConfig() && !failReason.contains(SpecialKeywords.ALREADY_PASSED)207 && !failReason.contains(SpecialKeywords.SKIP_EXECUTION)) {208 if (INCLUDE_SKIP) {209 result = testResultItem.getLinkToScreenshots() != null ? SKIP_TEST_LOG_DEMO_TR : SKIP_TEST_LOG_TR;210 result = result.replace(TEST_NAME_PLACEHOLDER, testResultItem.getTest());211 if (!StringUtils.isEmpty(failReason)) {212 // Make description more compact for email report213 failReason = failReason.length() > MESSAGE_LIMIT214 ? (failReason.substring(0, MESSAGE_LIMIT) + "...") : failReason;215 result = result.replace(SKIP_REASON_PLACEHOLDER, formatFailReasonAsHtml(failReason));216 } else {217 result = result.replace(SKIP_REASON_PLACEHOLDER,218 "Analyze SYSTEM ISSUE log for details or check dependency settings for the test.");219 }220 result = result.replace(LOG_URL_PLACEHOLDER, testResultItem.getLinkToLog());221 if (testResultItem.getLinkToScreenshots() != null) {222 result = result.replace(SCREENSHOTS_URL_PLACEHOLDER, testResultItem.getLinkToScreenshots());223 }224 }225 skipCount++;226 }227 }228 if (testResultItem.getResult().name().equalsIgnoreCase("PASS")) {229 if (!testResultItem.isConfig()) {230 passCount++;231 if(INCLUDE_PASS){232 result = testResultItem.getLinkToScreenshots() != null ? PASS_TEST_LOG_DEMO_TR : PASS_TEST_LOG_TR;233 result = result.replace(TEST_NAME_PLACEHOLDER, testResultItem.getTest());234 result = result.replace(LOG_URL_PLACEHOLDER, testResultItem.getLinkToLog());235 236 if(testResultItem.getLinkToScreenshots() != null)237 {238 result = result.replace(SCREENSHOTS_URL_PLACEHOLDER, testResultItem.getLinkToScreenshots());239 }240 }241 }242 }243 244 List<String> jiraTickets = testResultItem.getJiraTickets();245 246 String bugId = null;247 String bugUrl = null;248 249 if (jiraTickets.size() > 0)250 {251 bugId = jiraTickets.get(0);252 253 if (!Configuration.get(Parameter.JIRA_URL).isEmpty()) {254 bugUrl = Configuration.get(Parameter.JIRA_URL) + "/browse/" + jiraTickets.get(0);255 }256 257 if (jiraTickets.size() > 1) {258 LOGGER.error("Current implementation doesn't support email report generation with several Jira Tickets fo single test!");259 }260 }261 if (bugId == null) {262 bugId = "N/A";263 }264 265 if (bugUrl == null) {266 bugUrl = "#";267 }268 result = result.replace(BUG_ID_PLACEHOLDER, bugId);269 result = result.replace(BUG_URL_PLACEHOLDER, bugUrl);270 return result;271 }272 273 private int getSuccessRate()274 {275 return passCount > 0 ? (int) (((double) passCount) / ((double) passCount + (double) failCount + (double) skipCount) * 100) : 0;276 }277 public static TestResultType getSuiteResult(List<TestResultItem> ris)278 {279 int passed = 0;280 int failed = 0;281 int failedKnownIssue = 0;282 int skipped = 0;283 int skipped_already_passed = 0;284 285 for(TestResultItem ri : ris)286 {287 if (ri.isConfig()) {288 continue;289 }290 291 switch (ri.getResult()) {292 case PASS:293 passed++;294 break;295 case FAIL:296 if (Configuration.getBoolean(Parameter.TRACK_KNOWN_ISSUES)) {297 if (ri.getJiraTickets().size() > 0) {298 // increment known issue counter299 failedKnownIssue++;300 } else {301 failed++;302 }303 } else {304 failed++;305 }306 break;307 case SKIP:308 if (ri.getFailReason().startsWith(SpecialKeywords.ALREADY_PASSED)) {309 skipped_already_passed++;310 } else if (ri.getFailReason().startsWith(SpecialKeywords.SKIP_EXECUTION)) { 311 // don't calculate such message at all as it shouldn't be312 // included into the report at all313 }314 else {315 skipped++;316 }317 break;318 case SKIP_ALL:319 //do nothing320 break;321 default:322 //do nothing323 break;324 }325 }326 TestResultType result;327 if (passed == 0 && failed == 0 && skipped == 0 && skipped_already_passed > 0){328 result = TestResultType.SKIP_ALL_ALREADY_PASSED; //it was re-run of the suite where all tests passed during previous run329 } else if (passed > 0 && failedKnownIssue == 0 && failed == 0 && skipped == 0) {330 result = TestResultType.PASS;331 } else if (passed >= 0 && failedKnownIssue > 0 && failed == 0 && skipped == 0) {332 result = TestResultType.PASS_WITH_KNOWN_ISSUES;333 } else if (passed == 0 && failed == 0 && skipped > 0){334 result = TestResultType.SKIP_ALL;335 } else if (passed >= 0 && failed == 0 && skipped > 0){336 result = TestResultType.SKIP;337 } else {338 result = TestResultType.FAIL;339 }340 result.setPassed(passed);341 result.setFailed(failed + failedKnownIssue);342 result.setSkipped(skipped);343 344 return result;345 }346 public String getCreatedItemsList(List<String> createdItems)347 {348 if (!CollectionUtils.isEmpty(createdItems))349 {350 StringBuilder result = new StringBuilder();351 for (String createdItem : createdItems)352 {353 result.append(CREATED_ITEM.replace(CREATED_ITEM_PLACEHOLDER, createdItem));354 }355 return CREATED_ITEMS_LIST.replace(CREATED_ITEMS_LIST_PLACEHOLDER, result.toString());356 }357 else358 {359 return "";360 }361 }362 public String formatFailReasonAsHtml(String reasonText)363 {364 if (!StringUtils.isEmpty(reasonText))365 {366 reasonText = StringEscapeUtils.escapeHtml4(reasonText);367 reasonText = reasonText.replace("\n", "<br/>");368 }369 return reasonText;370 }371 public String getCucumberResults() {372 String result="";373 if ((isArtifactsFolderExists()) && (isCucumberTest()) && (isCucumberReportFolderExists())) {374 String link = getCucumberReportResultLink();375 String feature1 = "";376 if (useJSinCucumberReport()) {377 feature1 = String.format("%s %n %s %n %s %n %s %n %s %n %s"378 , "<input class='hide' id='hd-1' type='checkbox'>"379 , "<label for='hd-1'>Show Cucumber Results</label> "380 , "<div>"381 , "<iframe name='frm' id='mainframe' src='" + link + "' scrolling='yes' width='90%'height='100%' align='center' frameborder='0' allowtransparency='no' target='_self' >"382 , "</iframe>"383 , "</div><br/>"384 );385 }386 result = String.format("<br/><b><a href='%s' style='color: green;' target='_blank'> Open Cucumber Report in new Window </a></b><br/>%s",link,feature1);387 LOGGER.info("Cucumber result: "+result);388 }389 return result;390 }391 private String getCucumberJavaScript() {392 String result="";393 if ((isArtifactsFolderExists()) && (isCucumberTest()) && (isCucumberReportFolderExists()) && (useJSinCucumberReport())) {394 result="<link rel=\"stylesheet\" type=\"text/css\" href=\"gallery-lib/cucumber.css\">";395 }396 return result;397 }398 399}...
isCucumberReportFolderExists
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.report.email.EmailReportGenerator;2public class EmailReportGeneratorTest {3 public static void main(String[] args) {4 System.out.println(EmailReportGenerator.isCucumberReportFolderExists("cucumber-report"));5 }6}
isCucumberReportFolderExists
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.report.email.EmailReportGenerator;2import com.qaprosoft.carina.core.foundation.report.email.EmailTemplate;3import java.io.File;4public class Test {5 public static void main(String[] args) {6 EmailReportGenerator reportGenerator = new EmailReportGenerator();7 EmailTemplate emailTemplate = new EmailTemplate();
isCucumberReportFolderExists
Using AI Code Generation
1public static boolean isCucumberReportFolderExists(String reportPath) {2 File folder = new File(reportPath);3 if (folder.exists() && folder.isDirectory()) {4 for (File file : folder.listFiles()) {5 if (file.getName().contains("cucumber-report")) {6 return true;7 }8 }9 }10 return false;11}12public static boolean isCucumberReportFolderExists(String reportPath) {13 File folder = new File(reportPath);14 if (folder.exists() && folder.isDirectory()) {15 for (File file : folder.listFiles()) {16 if (file.getName().contains("cucumber-report")) {17 return true;18 }19 }20 }21 return false;22}23public static boolean isCucumberReportFolderExists(String reportPath) {24 File folder = new File(reportPath);25 if (folder.exists() && folder.isDirectory()) {26 for (File file : folder.listFiles()) {27 if (file.getName().contains("cucumber-report")) {28 return true;29 }30 }31 }32 return false;33}34public static boolean isCucumberReportFolderExists(String reportPath) {35 File folder = new File(reportPath);36 if (folder.exists() && folder.isDirectory()) {37 for (File file : folder.listFiles()) {38 if (file.getName().contains("cucumber-report")) {39 return true;40 }41 }42 }43 return false;44}
isCucumberReportFolderExists
Using AI Code Generation
1if (isCucumberReportFolderExists()) {2 log.info("Cucumber report exists");3} else {4 log.info("Cucumber report doesn't exist");5}6if (isCucumberReportFolderExists()) {7 log.info("Cucumber report exists");8} else {9 log.info("Cucumber report doesn't exist");10}11if (isCucumberReportFolderExists()) {12 log.info("Cucumber report exists");13} else {14 log.info("Cucumber report doesn't exist");15}16if (isCucumberReportFolderExists()) {17 log.info("Cucumber report exists");18} else {19 log.info("Cucumber report doesn't exist");20}21if (isCucumberReportFolderExists()) {22 log.info("Cucumber report exists");23} else {24 log.info("Cucumber report doesn't exist");25}26if (isCucumberReportFolderExists()) {27 log.info("Cucumber report exists");28} else {29 log.info("Cucumber report doesn't exist");30}31if (isCucumberReportFolderExists()) {32 log.info("Cucumber report exists");33} else {34 log.info("Cucumber report doesn't exist");35}36if (isCucumberReportFolderExists()) {37 log.info("Cucumber report exists");38} else {39 log.info("Cucumber report doesn't exist");40}41if (isCucumberReportFolderExists()) {
isCucumberReportFolderExists
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.report.email.EmailReportGenerator;2import com.qaprosoft.carina.core.foundation.report.email.EmailReportGenerator.isCucumberReportFolderExists;3public class isCucumberReportFolderExists {4 public static void main(String[] args) {5 EmailReportGenerator emailReportGenerator = new EmailReportGenerator();6 System.out.println(emailReportGenerator.isCucumberReportFolderExists());7 }8}
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!!