All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pega.uiframework.utils.HTMLReporter Maven / Gradle / Ivy

The newest version!
package com.pega.uiframework.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.testng.*;
import org.testng.internal.Utils;
import org.testng.reporters.util.StackTraceTools;
import org.testng.xml.XmlSuite;

import java.io.*;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;



public class HTMLReporter extends TestListenerAdapter implements IReporter
{
    private static PrintWriter f_out;
    private static String      outputDir;

    private static String[]    MODULES;
    private static String[]    TEST_GROUPS;

    public void generateReport(List arg0, List suites, String outdir)
    {
        try
        {
            outputDir = ProjectConfigurator.getProperty("test.reports.directory");
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }

        String modulesCommaSeparated = "";
        String testGroupsCommaSeparated = "";

        try
        {
            modulesCommaSeparated = ProjectConfigurator.getProperty("test.modules").replaceAll("\\s+", "");
            testGroupsCommaSeparated = ProjectConfigurator.getProperty("test.groups").replaceAll("\\s+", "");
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }

        if (modulesCommaSeparated == null || modulesCommaSeparated.trim().length() == 0)
        {
            Assert.fail("ERROR - Modules are not found in properties file");
        } else
        {
            MODULES = new String[modulesCommaSeparated.length()];
            MODULES = modulesCommaSeparated.split(",");
        }

        if (testGroupsCommaSeparated == null || testGroupsCommaSeparated.trim().length() == 0)
        {
            Assert.fail("ERROR - Test Groups are not found in properties file");
        } else
        {
            TEST_GROUPS = new String[testGroupsCommaSeparated.length()];
            TEST_GROUPS = testGroupsCommaSeparated.split(",");
        }

        try
        {
            f_out = createWriter(outputDir);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        startHtmlPage(f_out);

        try
        {
            generateAdditionalInfoReport(suites);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        generateTestExecutionStatus(suites);
        endHtmlPage(f_out);

        f_out.flush();
        f_out.close();

    }

    private void generateTestExecutionStatus(List suites)
    {
        String testName = "";

        int totalPassedMethods = 0;
        int totalFailedMethods = 0;
        int totalSkippedMethods = 0;
        int totalSkippedConfigurationMethods = 0;
        int totalFailedConfigurationMethods = 0;
        int totalMethods = 0;

        int suite_totalPassedMethods = 0;
        int suite_totalFailedMethods = 0;
        int suite_totalSkippedMethods = 0;

        String suite_passPercentage = "";
        String suiteName = "";

        ITestContext overview = null;
        HashMap dashboardReportMap = new HashMap();

        for (ISuite suite : suites)
        {
            suiteName = suite.getName();

            Map tests = suite.getResults();

            for (ISuiteResult r : tests.values())
            {
                overview = r.getTestContext();
                testName = overview.getName();

                totalPassedMethods = overview.getPassedTests().getAllMethods().size();
                totalFailedMethods = overview.getFailedTests().getAllMethods().size();
                totalSkippedMethods = overview.getSkippedTests().getAllMethods().size();

                totalMethods = overview.getAllTestMethods().length;

                NumberFormat nf = NumberFormat.getInstance();
                nf.setMaximumFractionDigits(2);
                nf.setGroupingUsed(true);

                String includedModule = "";
                String includedGroup = "";

                ITestNGMethod[] allTestMethods = overview.getAllTestMethods();
                for (ITestNGMethod testngMethod : allTestMethods)
                {
                    String[] modules = testngMethod.getGroups();
                    for (String module : modules)
                    {
                        for (String moduleName : MODULES)
                        {
                            if (module.equalsIgnoreCase(moduleName))
                            {
                                if (!(includedModule.contains(module)))
                                {
                                    includedModule = includedModule + " " + module;
                                }
                            }
                        }
                        for (String groupName : TEST_GROUPS)
                        {
                            if (module.equalsIgnoreCase(groupName))
                            {
                                if (!(includedGroup.contains(module)))
                                {
                                    includedGroup = includedGroup + " " + module;
                                }
                            }
                        }
                    }
                }

                String browser = overview.getCurrentXmlTest().getParameter("browser");
                String browser_version = overview.getCurrentXmlTest().getParameter("browser_version");
                String platform = overview.getCurrentXmlTest().getParameter("os");

                if (platform == null || platform.trim().length() == 0)
                {
                    platform = "N/A";
                }

                if (browser_version == null || browser_version.trim().length() == 0)
                {
                    browser_version = "N/A";
                }

                if (browser == null || browser.trim().length() == 0)
                {
                    browser = "N/A";
                }

                if (!(dashboardReportMap.containsKey(includedModule)))
                {
                    if (browser_version.equalsIgnoreCase("N/A"))
                    {
                        browser_version = "";
                    }
                    dashboardReportMap.put(includedModule, "os1~" + platform + "|browser1~" + browser + browser_version
                            + "|testcase_count_1~" + totalMethods + "|pass_count_1~" + totalPassedMethods
                            + "|fail_count_1~" + totalFailedMethods + "|skip_count_1~" + totalSkippedMethods
                            + "|skip_conf_count_1~" + totalSkippedConfigurationMethods + "|fail_conf_count_1~"
                            + totalFailedConfigurationMethods);

                } else
                {
                    for (String key : dashboardReportMap.keySet())
                    {

                        if (key.equalsIgnoreCase(includedModule))
                        {
                            if (browser_version.equalsIgnoreCase("N/A"))
                            {
                                browser_version = "";
                            }
                            String value = dashboardReportMap.get(key);
                            int index = StringUtils.countMatches(value, "#") + 1;

                            index += 1;

                            value = value + "#" + "os" + index + "~" + platform + "|browser" + index + "~" + browser
                                    + browser_version + "|testcase_count_" + index + "~" + totalMethods
                                    + "|pass_count_" + index + "~" + totalPassedMethods + "|fail_count_" + index + "~"
                                    + totalFailedMethods + "|skip_count_" + index + "~" + totalSkippedMethods
                                    + "|skip_conf_count_" + index + "~" + totalSkippedConfigurationMethods
                                    + "|fail_conf_count_" + index + "~" + totalFailedConfigurationMethods;
                            dashboardReportMap.put(key, value);
                        }
                    }
                }

                suite_totalPassedMethods += totalPassedMethods;
                suite_totalFailedMethods += totalFailedMethods;
                suite_totalSkippedMethods += totalSkippedMethods;

                try
                {
                    suite_passPercentage = nf
                            .format(((float) suite_totalPassedMethods / (float) (suite_totalPassedMethods
                                    + suite_totalFailedMethods + suite_totalSkippedMethods)) * 100);
                } catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }
            }
        }

        StringBuilder dashboardResults = new StringBuilder();

        dashboardResults.append("");
        dashboardResults
                .append("");

        int total_browser_combinations = 0;
        int total_unique_testcases = 0;

        for (String key : dashboardReportMap.keySet())
        {

            String fileName = key.trim() + "-Overall" + "-customized-report.html";
            try
            {
                generateModuleOverallTestReport(testName, key, suites, fileName);
            } catch (Exception e)
            {
                e.printStackTrace();
            }

            String value = dashboardReportMap.get(key);
            String[] values = value.split("#");

            int testcase_count = 0;
            int pass_count = 0;
            int fail_count = 0;
            int skip_count = 0;
            int skip_conf_count = 0;
            int fail_conf_count = 0;

            String dashboardModule = key;

            for (String val : values)
            {

                String[] tokens = val.split("\\|");
                for (String token : tokens)
                {
                    if (token.contains("testcase_count"))
                    {
                        testcase_count = testcase_count + Integer.parseInt(token.split("~")[1]);
                    }
                    if (token.contains("pass_count"))
                    {
                        pass_count = pass_count + Integer.parseInt(token.split("~")[1]);
                    }
                    if (token.contains("fail_count"))
                    {
                        fail_count = fail_count + Integer.parseInt(token.split("~")[1]);
                    }
                    if (token.contains("skip_count"))
                    {
                        skip_count = skip_count + Integer.parseInt(token.split("~")[1]);
                    }
                    if (token.contains("skip_conf_count"))
                    {
                        skip_conf_count = skip_conf_count + Integer.parseInt(token.split("~")[1]);
                    }
                    if (token.contains("fail_conf_count"))
                    {
                        fail_conf_count = fail_conf_count + Integer.parseInt(token.split("~")[1]);
                    }
                }
            }

            String[] sub = value.split("#");
            String temp = "";
            for (String s : sub)
            {
                s = s.substring(0, s.indexOf("fail_count"));
                temp = temp + s;
            }

            temp = temp.substring(0, temp.lastIndexOf("|"));
            temp = temp.replace(" ", "%20");

            NumberFormat nformat = NumberFormat.getInstance();
            nformat.setMaximumFractionDigits(2);
            nformat.setGroupingUsed(true);
            String passPercent = nformat
                    .format(((float) pass_count / (float) (pass_count + fail_count + skip_count)) * 100);

            String finalStr = "[";
            String[] val = dashboardReportMap.get(key).split("#");

            int unique_testcase = 0;

            int limit = val.length - 1;
            for (int i = 0; i < val.length; i++)
            {
                String testCaseCount = (val[i].split("\\|")[2]).split("~")[1];
                int next = Integer.parseInt(testCaseCount);
                if (next > unique_testcase)
                {
                    unique_testcase = next;
                }
                finalStr = finalStr + testCaseCount + " T * 1 B]";
                if (i != limit)
                {
                    finalStr += " + [";
                }
            }

            String finalString = "";
            if ((unique_testcase * values.length) != (pass_count + fail_count + skip_count))
            {
                finalString = "" + (pass_count + fail_count + skip_count)
                        + "";
            } else
            {
                finalString = String.valueOf((pass_count + fail_count + skip_count));
            }

            String passCount = "";
            String failCount = "";
            String skipCount = "";

            if (pass_count > 0)
            {
                passCount = "";
            } else
            {
                passCount = "";
            }

            if (fail_count > 0)
            {
                failCount = "";
            } else
            {
                failCount = "";
            }

            if (skip_count > 0)
            {
                skipCount = "";
            } else
            {
                skipCount = "";
            }

            dashboardResults
                    .append(""
                            + passCount
                            + failCount
                            + skipCount
                            + "");

            if (total_browser_combinations < values.length)
            {
                total_browser_combinations = values.length;
            }

            total_unique_testcases += unique_testcase;
        }

        dashboardResults.append("
Module Name # Unique TestCases # Browser Combinations # Passed # Failed # Skipped# TotalSuccess Rate
" + pass_count + "" + pass_count + "" + fail_count + "" + fail_count + "" + skip_count + "" + skip_count + "
" + dashboardModule + "" + unique_testcase + "" + values.length + "" + finalString + "" + passPercent + " %" + "
"); String suite_pass = ""; String suite_fail = ""; String suite_skip = ""; if (suite_totalPassedMethods > 0) { suite_pass = "" + suite_totalPassedMethods + ""; } else { suite_pass = "" + suite_totalPassedMethods + ""; } if (suite_totalFailedMethods > 0) { suite_fail = "" + suite_totalFailedMethods + ""; } else { suite_fail = "" + suite_totalFailedMethods + ""; } if (suite_totalSkippedMethods > 0) { suite_skip = "" + suite_totalSkippedMethods + ""; } else { suite_skip = "" + suite_totalSkippedMethods + ""; } NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); nf.setGroupingUsed(true); try { suite_passPercentage = nf .format(((float) suite_totalPassedMethods / (float) (total_unique_testcases) * 100)); } catch (NumberFormatException e) { e.printStackTrace(); } // Summary Table f_out.println("

Overall Execution Summary

"); f_out.println("" + suite_pass + suite_fail + suite_skip + "
Test Suite Name# Unique TestCases# Browser Combinations # Passed # Failed # Skipped# Total Success Rate
" + suiteName + "" + total_unique_testcases + "" + total_browser_combinations + "" + (suite_totalPassedMethods + suite_totalFailedMethods + suite_totalSkippedMethods) + "" + suite_passPercentage + " %" + "
"); f_out.flush(); f_out.println("
"); f_out.println("

Modulewise Execution Summary

"); f_out.println(dashboardResults); f_out.flush(); } private void generateModuleOverallTestReport(String testName, String moduleVar, List suites, String newFileName) throws Exception { StringBuilder moduleResults = new StringBuilder(); final PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDir, newFileName)))); startHtmlPage(pw); pw.println(""); pw.println("


"); pw.println("

Testwise Overall Execution Details

"); pw.println("
Module Name: " + moduleVar + "


"); moduleResults.append(""); moduleResults .append(""); int totalPassedMethods = 0; int totalFailedMethods = 0; int totalSkippedMethods = 0; String passPercentage = ""; ITestContext overview = null; for (ISuite suite : suites) { Map tests = suite.getResults(); for (ISuiteResult r : tests.values()) { overview = r.getTestContext(); testName = overview.getName(); totalPassedMethods = overview.getPassedTests().getAllMethods().size(); totalFailedMethods = overview.getFailedTests().getAllMethods().size(); totalSkippedMethods = overview.getSkippedTests().getAllMethods().size(); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); nf.setGroupingUsed(true); try { passPercentage = nf.format(((float) totalPassedMethods / (float) (totalPassedMethods + totalFailedMethods + totalSkippedMethods)) * 100); } catch (NumberFormatException e) { e.printStackTrace(); } String includedModule = ""; String includedGroup = ""; ITestNGMethod[] allTestMethods = overview.getAllTestMethods(); for (ITestNGMethod testngMethod : allTestMethods) { String[] modules = testngMethod.getGroups(); for (String module : modules) { for (String moduleName : MODULES) { if (module.equalsIgnoreCase(moduleName)) { if (!(includedModule.contains(module))) { includedModule = includedModule + " " + module; } } } for (String groupName : TEST_GROUPS) { if (module.equalsIgnoreCase(groupName)) { if (!(includedGroup.contains(module))) { includedGroup = includedGroup + " " + module; } } } } } String browser = overview.getCurrentXmlTest().getParameter("browser"); String browser_version = overview.getCurrentXmlTest().getParameter("browser_version"); String platform = overview.getCurrentXmlTest().getParameter("os"); String platformVersion = overview.getCurrentXmlTest().getParameter("os_version"); if (platform == null || platform.trim().length() == 0) { platform = "N/A"; } if (browser_version == null || browser_version.trim().length() == 0) { browser_version = "N/A"; } if (browser == null || browser.trim().length() == 0) { browser = "N/A"; } if (browser.equalsIgnoreCase("firefox")) { browser = "Firefox"; } else if (browser.equalsIgnoreCase("chrome")) { browser = "Chrome"; } else if (browser.equalsIgnoreCase("internet explorer")) { browser = "IE"; } if (platform.equalsIgnoreCase("windows") && platformVersion.equalsIgnoreCase("xp")) { platform = "XP"; } else if (platform.equalsIgnoreCase("windows") && platformVersion.equalsIgnoreCase("7")) { platform = "Win 7"; } else if (platform.equalsIgnoreCase("windows") && platformVersion.equalsIgnoreCase("8")) { platform = "Win 8"; } else if (platform.equalsIgnoreCase("mac")) { platform = "Mac"; } else { } if (includedModule.equalsIgnoreCase(moduleVar)) { String fileName = testName + "-customized-report.html"; try { generateModuleWiseTestReport(testName, suites, fileName, moduleVar, "?"); } catch (IOException e) { e.printStackTrace(); } String passCount = ""; String failCount = ""; String skipCount = ""; if (totalPassedMethods > 0) { passCount = ""; } else { passCount = ""; } if (totalFailedMethods > 0) { failCount = ""; } else { failCount = ""; } if (totalSkippedMethods > 0) { skipCount = ""; } else { skipCount = ""; } moduleResults .append("" + "" + passCount + failCount + skipCount + ""); } } } moduleResults.append("
Test Name Module Group Browser Version OSNode IP# Passed # Failed # Skipped# Total Success Rate
" + totalPassedMethods + "" + totalPassedMethods + "" + totalFailedMethods + "" + totalFailedMethods + "" + totalSkippedMethods + "" + totalSkippedMethods + "
" + testName + "" + includedModule + "" + includedGroup + "" + browser + "" + browser_version + "" + platform + "" + "?" + "" + (totalPassedMethods + totalFailedMethods + totalSkippedMethods) + "" + passPercentage + " %" + "
"); pw.println(moduleResults); endHtmlPage(pw); pw.flush(); pw.close(); } private void generateModuleWiseTestReport(String testName, List suites, String newFileName, String passedModule, String nodeIp) throws IOException { final PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDir, newFileName)))); startHtmlPage(pw); pw.println(""); pw.println("


"); pw.println("

Modulewise Execution Summary

"); pw.println("
Test Name: " + testName + "

"); pw.println(""); pw.println(""); HashMap> moduleMap = new HashMap>(); ITestContext overview = null; for (ISuite suite : suites) { Map tests = suite.getResults(); for (ISuiteResult r : tests.values()) { overview = r.getTestContext(); if ((overview.getName()).equalsIgnoreCase(testName)) { ITestNGMethod[] testngMethods = overview.getAllTestMethods(); ArrayList> moduleMethods = new ArrayList>(); for (ITestNGMethod testngMethod : testngMethods) { String[] groups = testngMethod.getGroups(); for (String group : groups) { for (String module : MODULES) { if (group.equalsIgnoreCase(module)) { HashMap tempMap = new HashMap(); tempMap.put(module, testngMethod); moduleMethods.add(tempMap); } } } } for (String module : MODULES) { ArrayList moduleTestMethods = new ArrayList(); Iterator> it = moduleMethods.iterator(); while (it.hasNext()) { String moduleName = ""; ITestNGMethod testMethod = null; HashMap moduleWithTestMethod = it.next(); if (moduleWithTestMethod.containsKey(module)) { moduleName = module; testMethod = moduleWithTestMethod.get(module); } if (module.equalsIgnoreCase(moduleName)) { moduleTestMethods.add(testMethod); } } moduleMap.put(module, moduleTestMethods); } } } } Set keySet = moduleMap.keySet(); Iterator it = keySet.iterator(); for (ISuite suite : suites) { Map tests = suite.getResults(); for (ISuiteResult r : tests.values()) { overview = r.getTestContext(); if ((overview.getName()).equalsIgnoreCase(testName)) { while (it.hasNext()) { String moduleName = (String) it.next(); int totalPassedMethods = 0; int totalFailedMethods = 0; int totalSkippedMethods = 0; int totalSkippedConfigurations = 0; int totalFailedConfigurations = 0; ArrayList values = moduleMap.get(moduleName); ListIterator it2 = values.listIterator(); while (it2.hasNext()) { ITestNGMethod method = it2.next(); int failedMethods = overview.getFailedTests().getResults(method).size(); int passedMethods = overview.getPassedTests().getResults(method).size(); int skippedMethods = overview.getSkippedTests().getResults(method).size(); totalPassedMethods += passedMethods; totalFailedMethods += failedMethods; totalSkippedMethods += skippedMethods; } if (values.size() > 0) { String fileName = testName + "-" + moduleName + "-customized-report.html"; try { generateModuleTestMethodSummary(testName, moduleName, suites, fileName, values, nodeIp); } catch (IOException e) { e.printStackTrace(); } int totalMethods = totalPassedMethods + totalFailedMethods + totalSkippedMethods; NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); nf.setGroupingUsed(false); String passPercentage = nf .format(((float) totalPassedMethods / (float) totalMethods) * 100); generateModulesRow(pw, fileName, moduleName, totalPassedMethods, totalFailedMethods, totalSkippedMethods, totalSkippedConfigurations, totalFailedConfigurations, totalMethods, passPercentage); } } break; } } } pw.println("
Module Name# Passed# Failed# Skipped# TotalSuccess Rate
"); endHtmlPage(pw); pw.flush(); pw.close(); } private void generateModuleTestMethodSummary(String testName, String modulename, List suites, String fileName, ArrayList testngMethods, String nodeIp) throws IOException { final PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDir, fileName)))); startHtmlPage(pw); String htmlFile = testName + "-customized-report.html"; String modulewiseTestFileName = testName + "-" + modulename + "-customized-report.html"; pw.println(""); pw.println("


"); pw.println("

Details

"); pw.println(""); pw.println("
Test Name: " + testName + "
Module Name: " + modulename + "

"); pw.println(""); pw.println(""); for (ISuite suite : suites) { Map tests = suite.getResults(); for (ISuiteResult re : tests.values()) { ITestContext overview = re.getTestContext(); if ((overview.getName()).equalsIgnoreCase(testName)) { Iterator it = testngMethods.iterator(); while (it.hasNext()) { ITestNGMethod method = it.next(); String[] allGroups = method.getGroups(); String methodName = ""; String className = ""; for (String grp : allGroups) { if (grp.equalsIgnoreCase(modulename)) { methodName = method.getMethodName(); className = method.getTestClass().getName(); ArrayList> statusResult = new ArrayList>(); Set failedTestStatus = overview.getFailedTests().getResults(method); if (!(failedTestStatus.isEmpty())) { statusResult.add(failedTestStatus); } Set passedTestStatus = overview.getPassedTests().getResults(method); if (!(passedTestStatus.isEmpty())) { statusResult.add(passedTestStatus); } Set skippedTestStatus = overview.getSkippedTests().getResults(method); if (!(skippedTestStatus.isEmpty())) { statusResult.add(skippedTestStatus); } pw.println(""); Iterator> statusIterator = statusResult.iterator(); while (statusIterator.hasNext()) { Set status = statusIterator.next(); StringBuilder stackTrace; StringBuilder failedConf; Iterator it2 = status.iterator(); List msgs = new ArrayList(); String executionStatus = ""; long time_start = Long.MAX_VALUE; long time_end = Long.MIN_VALUE; Throwable exception = null; String screenshotFileLink = ""; ITestResult result = null; while (it2.hasNext()) { stackTrace = new StringBuilder(); failedConf = new StringBuilder(); result = it2.next(); time_start = result.getStartMillis(); time_end = result.getEndMillis(); int execStatus = result.getStatus(); if (execStatus == ITestResult.SUCCESS) { executionStatus = "PASS"; } else if (execStatus == ITestResult.FAILURE) { executionStatus = "FAIL"; } else if (execStatus == ITestResult.SKIP) { executionStatus = "SKIP"; } if (execStatus == ITestResult.SKIP) { status = overview.getFailedConfigurations().getAllResults(); it2 = status.iterator(); failedConf.append("
"); while (it2.hasNext()) { result = it2.next(); failedConf.append("Failed Configuration - " + result.getMethod().getMethodName()); failedConf.append("
"); } exception = result.getThrowable(); } else { exception = result.getThrowable(); } try { msgs = Reporter.getOutput(result); /* * if (msgs.size() == 0) { msgs = * Reporter.getOutput(); } */ } catch (Exception ex) { // Log error message } /* * If enable logs is false, then only * take the screenshot. */ try { if ((ProjectConfigurator.getProperty("enable.logs.in.report") .equalsIgnoreCase("false")) && (msgs != null)) { for (String line : msgs) { if (line.contains("[Console Log] Screenshot saved in")) { screenshotFileLink = line.substring(line.indexOf("in") + 3, line.length()); break; } } } } catch (Exception ex) { ex.printStackTrace(); } /* * If enable logs is true, take the * whole log along with screenshot. */ try { if ((ProjectConfigurator.getProperty("enable.logs.in.report") .equalsIgnoreCase("true")) && (msgs != null)) { for (String line : msgs) { if (line.contains("[Console Log] Screenshot saved in")) { screenshotFileLink = line.substring(line.indexOf("in") + 3, line.length()); break; } } if (screenshotFileLink.trim().length() != 0) { stackTrace .append("
View Screenshot in New Window/Tab

"); } for (String line : msgs) { if (!(line.contains("[Console Log] Screenshot saved in"))) { stackTrace.append("
" + line); } } } } catch (Exception ex) { ex.printStackTrace(); } if (msgs != null) { msgs.clear(); } Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(100000); String stackTraceFile = testName + "-" + modulename + "-" + methodName + "-" + randomInt + "-" + "custom-report.html"; stackTrace.append("
" + failedConf.toString()); generateStackTraceReport(modulewiseTestFileName, stackTraceFile, stackTrace, exception, method, nodeIp, result); String link = ""; if (executionStatus.equalsIgnoreCase("pass")) { executionStatus = "
"; } else if (executionStatus.equalsIgnoreCase("fail")) { executionStatus = ""; } else if (executionStatus.equalsIgnoreCase("skip")) { executionStatus = ""; } else { executionStatus = ""; } pw.println("" + executionStatus + ""); pw.flush(); } } } } } } } } pw.println("
Method NameTotal Time (ms)StatusStack Trace | Test Parameters
" + method.getMethodName() + "
" + executionStatus + "" + executionStatus + "" + executionStatus + "" + executionStatus + "
" + "[Class Name] " + className + "" + (time_end - time_start) + "" + link + "
"); endHtmlPage(pw); pw.flush(); pw.close(); } private void generateStackTraceReport(String fileName, String stackTraceFile, StringBuilder stackTrace, Throwable exception, ITestNGMethod method, String nodeIp, ITestResult result) throws IOException { final PrintWriter fw = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDir, stackTraceFile)))); startHtmlPage(fw); fw.println(""); fw.println("


"); if (result != null) { fw.println("
Test Parameters"); fw.println(""); Object[] params = result.getParameters(); if (params != null) { for (int i = 0; i < params.length; i++) { fw.println(""); } } fw.println("
[Parameter " + i + "]" + params[i] + "
"); fw.println("
"); fw.println("
"); } fw.println("
Screenshot / Exception Log"); try { if (ProjectConfigurator.getProperty("enable.logs.in.report").equalsIgnoreCase("false")) { fw.println("

[Debug] - Console logs in custom report is disabled. To view the Console logs please set the enable.logs.in.report as true in properties file!

"); } } catch (Exception ex) { ex.printStackTrace(); } // fw.println("

[Node IP] - " + nodeIp + "

"); fw.println(stackTrace + "
"); fw.println("
"); fw.println("
"); if (exception != null) { fw.println("
Stack Trace"); generateExceptionReport(exception, method, fw); fw.println("
"); } endHtmlPage(fw); fw.flush(); fw.close(); } protected void generateExceptionReport(Throwable exception, ITestNGMethod method, PrintWriter pw) { pw.flush(); generateExceptionReport(exception, method, exception.getLocalizedMessage(), pw); } private void generateExceptionReport(Throwable exception, ITestNGMethod method, String title, PrintWriter m_out) { m_out.println("

" + title + "

"); StackTraceElement[] s1 = exception.getStackTrace(); Throwable t2 = exception.getCause(); if (t2 == exception) { t2 = null; } int maxlines = Math.min(100, StackTraceTools.getTestRoot(s1, method)); for (int x = 0; x <= maxlines; x++) { m_out.println((x > 0 ? "
at " : "") + Utils.escapeHtml(s1[x].toString())); } if (maxlines < s1.length) { m_out.println("
" + (s1.length - maxlines) + " lines not shown"); } if (t2 != null) { generateExceptionReport(t2, method, "Caused by " + t2.getLocalizedMessage(), m_out); } m_out.println("

"); m_out.flush(); } private void generateModulesRow(PrintWriter pw, String fileName, String moduleName, int passedMethods, int failedMethods, int skippedMethods, int skippedConfiguration, int failedConfiguration, int totalMethods, String passPercentage) { String passCount = ""; String failCount = ""; String skipCount = ""; if (passedMethods > 0) { passCount = "" + passedMethods + ""; } else { passCount = "" + passedMethods + ""; } if (failedMethods > 0) { failCount = "" + failedMethods + ""; } else { failCount = "" + failedMethods + ""; } if (skippedMethods > 0) { skipCount = "" + skippedMethods + ""; } else { skipCount = "" + skippedMethods + ""; } pw.println("" + moduleName + "" + passCount + failCount + skipCount + "" + totalMethods + "" + passPercentage + " %" + ""); pw.flush(); } private void generateAdditionalInfoReport(List suites) throws Exception { String url = System.getProperty("url"); if (url == null) { url = ProjectConfigurator.getProperty("url"); } f_out.println("Test Environment Details

"); f_out.println(""); // f_out.println(""); f_out.println(""); for (ISuite suite : suites) { f_out.println(""); break; } f_out.println("
Configuration Details
Application URL: " + "" + url + "
Parallel Run: " + suite.getParallel() + "
"); f_out.println("
"); f_out.println("OS/Browser Details

"); f_out.println(""); // f_out.println(""); f_out.println(""); f_out.println(""); f_out.println(""); f_out.println(""); f_out.println("
Configuration Details
OS Name: " + "" + WordUtils.capitalize(System.getProperty("os")) + "
OS Version: " + "" + System.getProperty("os_version") + "
Browser Name: " + "" + WordUtils.capitalize(System.getProperty("browser")) + "
Browser Version: " + "" + System.getProperty("browser_version") + "
"); f_out.println("
"); f_out.flush(); } private PrintWriter createWriter(String outdir) throws IOException { new File(outdir).mkdirs(); return new PrintWriter(new BufferedWriter( new FileWriter(new File(outputDir, "customized-test-run-report.html")))); } /** Starts HTML Stream */ private void startHtmlPage(PrintWriter out) { out.println(""); out.println(""); out.println(""); out.println("Pega QA Automation Test Results Summary"); out.println(""); out.println("
" + "
" + "

My Company QA Automation Report

"); Calendar currentdate = Calendar.getInstance(); String strdate = null; DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm a z"); strdate = formatter.format(currentdate.getTime()); TimeZone obj = TimeZone.getTimeZone("IST"); formatter.setTimeZone(obj); strdate = formatter.format(currentdate.getTime()); out.println("
Report generated on: " + strdate + "


"); out.flush(); } /** Finishes HTML Stream */ private void endHtmlPage(PrintWriter out) { out.println("

"); out.println(""); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy