Search in sources :

Example 1 with StepStatus

use of com.seleniumtests.reporter.logger.TestStep.StepStatus in project seleniumRobot by bhecquet.

the class SeleniumTestsReporter2 method generateExecutionReport.

public void generateExecutionReport(SeleniumTestsContext testContext, ITestResult testResult, String testStatus, boolean resourcesFromCdn) throws IOException {
    VelocityEngine ve = initVelocityEngine();
    Template t = ve.getTemplate("/reporter/templates/report.test.vm");
    VelocityContext context = new VelocityContext();
    context.put("staticPathPrefix", "../");
    boolean displaySnapshots = testContext.getSeleniumRobotServerCompareSnapshot() && TestNGResultUtils.getSnapshotTestCaseInSessionId(testResult) != null && TestNGResultUtils.getSnapshotComparisonResult(testResult) != null;
    context.put("snapshots", displaySnapshots);
    context.put("snapshotServer", testContext.getSeleniumRobotServerUrl());
    context.put("snapshotComparisonResult", TestNGResultUtils.getSnapshotComparisonResult(testResult));
    context.put("snapshotSessionId", TestNGResultUtils.getSnapshotTestCaseInSessionId(testResult));
    // optimize reports means that resources are get from internet
    context.put("localResources", !resourcesFromCdn);
    context.put(HEADER, testStatus);
    // test header
    Object[] testParameters = testResult.getParameters();
    StringBuilder testName = new StringBuilder(getTestName(testResult));
    // issue #163: add test parameter to test name
    if (testParameters.length > 0) {
        testName.append(" with params: (");
        int i = 0;
        for (Object param : testParameters) {
            testName.append(param.toString());
            if (i < testParameters.length - 1) {
                testName.append(",");
            }
            i++;
        }
        testName.append(")");
    }
    context.put("testName", testName);
    context.put("description", StringUtility.encodeString(TestNGResultUtils.getTestDescription(testResult), "html"));
    // error causes
    Map<String, String> testInfos = TestNGResultUtils.getTestInfoEncoded(testResult, "html");
    if (!TestNGResultUtils.getErrorCauses(testResult).isEmpty()) {
        String causes = String.join("<br/>", TestNGResultUtils.getErrorCauses(testResult).stream().map(e -> String.format("<li>%s</li>", e)).collect(Collectors.toList()));
        testInfos.put("Possible error causes", String.format("<ul>%s</ul>", causes));
    }
    context.put("testInfos", relocateTestInfos(testResult, testInfos));
    // Application information
    fillContextWithTestParams(context, testResult);
    // test steps
    List<TestStep> testSteps = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps();
    if (testSteps == null) {
        testSteps = new ArrayList<>();
    }
    boolean videoInReport = isVideoInReport(testResult);
    List<List<Object>> steps = new ArrayList<>();
    for (TestStep testStep : testSteps) {
        List<Object> stepInfo = new ArrayList<>();
        TestStep encodedTestStep = testStep.encode("html");
        stepInfo.add(encodedTestStep.getName());
        // step status
        StepStatus stepStatus = encodedTestStep.getStepStatus();
        if (stepStatus == StepStatus.FAILED) {
            stepInfo.add(FAILED_TEST);
        } else if (stepStatus == StepStatus.WARNING) {
            stepInfo.add(WARN_TEST);
        } else {
            stepInfo.add(PASSED_TEST);
        }
        stepInfo.add(encodedTestStep.getDuration() / (double) 1000);
        stepInfo.add(encodedTestStep);
        stepInfo.add(encodedTestStep.getRootCause());
        stepInfo.add(encodedTestStep.getRootCauseDetails());
        // do not display video timestamp if video is not taken, or removed
        if (encodedTestStep.getVideoTimeStamp() > 0 && videoInReport) {
            stepInfo.add(encodedTestStep.getVideoTimeStamp() / (double) 1000);
        } else {
            stepInfo.add(null);
        }
        steps.add(stepInfo);
    }
    context.put("steps", steps);
    // logs
    String logs = SeleniumRobotLogger.getTestLogs().get(getTestName(testResult));
    if (logs == null) {
        logs = "";
    }
    // exception handling
    String[] stack = null;
    if (testResult.getThrowable() != null) {
        StringBuilder stackString = new StringBuilder();
        ExceptionUtility.generateTheStackTrace(testResult.getThrowable(), testResult.getThrowable().getMessage(), stackString, "html");
        stack = stackString.toString().split("\n");
    }
    // encode logs
    List<String> logLines = new ArrayList<>();
    for (String line : logs.split("\n")) {
        logLines.add(StringEscapeUtils.escapeHtml4(line));
    }
    context.put(STATUS, getTestStatus(testResult));
    context.put("stacktrace", stack);
    context.put("logs", logLines);
    // previous execution logs
    if (SeleniumTestsContextManager.getGlobalContext().getKeepAllResults()) {
        List<String> executionResults = FileUtils.listFiles(new File(TestNGResultUtils.getSeleniumRobotTestContext(testResult).getOutputDirectory()), FileFilterUtils.suffixFileFilter(".zip"), null).stream().map(File::getName).collect(Collectors.toList());
        if (!executionResults.isEmpty()) {
            context.put("files", executionResults);
            context.put("title", "Previous execution results");
        }
    } else {
        context.put("title", "No previous execution results, you can enable it via parameter '-DkeepAllResults=true'");
    }
    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    generateReport(Paths.get(testContext.getOutputDirectory(), "TestReport.html").toFile(), writer.toString());
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) TestStep(com.seleniumtests.reporter.logger.TestStep) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) StepStatus(com.seleniumtests.reporter.logger.TestStep.StepStatus) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) GenericFile(com.seleniumtests.reporter.logger.GenericFile)

Aggregations

GenericFile (com.seleniumtests.reporter.logger.GenericFile)1 TestStep (com.seleniumtests.reporter.logger.TestStep)1 StepStatus (com.seleniumtests.reporter.logger.TestStep.StepStatus)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 VelocityEngine (org.apache.velocity.app.VelocityEngine)1