Search in sources :

Example 36 with TestStep

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

the class SeleniumRobotServerTestRecorder method recordSteps.

/**
 * Record test steps to server
 * @param serverConnector
 * @param sessionId
 * @param testCaseInSessionId
 * @param testSteps
 */
private void recordSteps(SeleniumRobotSnapshotServerConnector serverConnector, Integer sessionId, Integer testCaseInSessionId, List<TestStep> testSteps, ITestResult testResult) {
    for (TestStep testStep : testSteps) {
        logger.info(String.format("Recording step %s on server", testStep.getName()));
        // record test step
        Integer testStepId = serverConnector.createTestStep(testStep.getName(), testCaseInSessionId);
        String stepLogs = testStep.toJson().toString();
        Integer stepResultId = serverConnector.recordStepResult(!testStep.getFailed(), stepLogs, testStep.getDuration(), sessionId, testCaseInSessionId, testStepId);
        testStep.setStepResultId(stepResultId);
        // sends all snapshots that are flagged as comparable
        for (Snapshot snapshot : new ArrayList<>(testStep.getSnapshots())) {
            if (snapshot.getCheckSnapshot().recordSnapshotOnServerForComparison() && SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshot()) {
                if (snapshot.getName() == null || snapshot.getName().isEmpty()) {
                    logger.warn("Snapshot hasn't any name, it won't be sent to server");
                    continue;
                }
                try {
                    Integer snapshotId = serverConnector.createSnapshot(snapshot, sessionId, testCaseInSessionId, stepResultId);
                    for (Rectangle excludeZone : snapshot.getCheckSnapshot().getExcludeElementsRect()) {
                        serverConnector.createExcludeZones(excludeZone, snapshotId);
                    }
                    logger.info("Check snapshot created");
                } catch (SeleniumRobotServerException e) {
                    logger.error("Could not create snapshot on server", e);
                }
            // record reference image on server if step is successful
            } else if (snapshot.getCheckSnapshot().recordSnapshotOnServerForReference() && SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerRecordResults()) {
                if (Boolean.FALSE.equals(testStep.getFailed())) {
                    try {
                        serverConnector.createStepReferenceSnapshot(snapshot, stepResultId);
                        logger.info("Step OK: reference created");
                    } catch (SeleniumRobotServerException e) {
                        logger.error("Could not create reference snapshot on server", e);
                    }
                    // remove this snapshot, extracted from video as it won't be used anymore
                    testStep.getSnapshots().remove(snapshot);
                } else {
                    try {
                        // move snapshot to "screenshots" directory as "video" directory will be removed at the end of the test
                        snapshot.relocate(TestNGResultUtils.getSeleniumRobotTestContext(testResult).getOutputDirectory(), ScreenshotUtil.SCREENSHOT_DIR + "/" + snapshot.getScreenshot().getImageName());
                        File referenceSnapshot = serverConnector.getReferenceSnapshot(stepResultId);
                        if (referenceSnapshot != null) {
                            logger.info("Step KO: reference snapshot got from server");
                            Path newPath = Paths.get(TestNGResultUtils.getSeleniumRobotTestContext(testResult).getScreenshotOutputDirectory(), referenceSnapshot.getName()).toAbsolutePath();
                            FileUtils.moveFile(referenceSnapshot, newPath.toFile());
                            testStep.addSnapshot(new Snapshot(new ScreenShot(newPath.getParent().getParent().relativize(newPath).toString()), "Valid-reference", SnapshotCheckType.FALSE), 0, null);
                            // change flag so that it's displayed in report (by default reference image extracted from video are not displayed)
                            snapshot.setDisplayInReport(true);
                        }
                    } catch (SeleniumRobotServerException e) {
                        logger.error("Could not get reference snapshot from server", e);
                    } catch (IOException e) {
                        logger.error("Could not copy reference snapshot", e);
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) ArrayList(java.util.ArrayList) Rectangle(org.openqa.selenium.Rectangle) IOException(java.io.IOException) File(java.io.File) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 37 with TestStep

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

the class SeleniumRobotServerTestRecorder method recordResults.

private void recordResults(SeleniumRobotSnapshotServerConnector serverConnector, Map<ITestContext, Set<ITestResult>> resultSet) {
    for (Entry<ITestContext, Set<ITestResult>> entry : resultSet.entrySet()) {
        List<ITestResult> methodResults = entry.getValue().stream().sorted((r1, r2) -> Long.compare(r1.getStartMillis(), r2.getStartMillis())).collect(Collectors.toList());
        // test case in seleniumRobot naming
        for (ITestResult testResult : methodResults) {
            // do not record this result twice if it's already recorded
            if (TestNGResultUtils.isSeleniumServerReportCreated(testResult) || // NoMoreRetry is set to false when test is being retried => we do not want to record a temp result, only the last one
            (TestNGResultUtils.getNoMoreRetry(testResult) != null && !TestNGResultUtils.getNoMoreRetry(testResult))) {
                continue;
            }
            // issue #81: recreate test context from this context (due to multithreading, this context may be null if parallel testing is used)
            SeleniumTestsContextManager.setThreadContextFromTestResult(entry.getKey(), testResult);
            // skipped tests has never been executed and so attribute (set in TestListener) has not been applied
            String testName = getTestCaseName(testResult);
            // get sessionId from context
            Integer sessionId = TestNGContextUtils.getTestSessionCreated(entry.getKey());
            // record test case
            Integer testCaseId = serverConnector.createTestCase(testName);
            Integer testCaseInSessionId = serverConnector.createTestCaseInSession(sessionId, testCaseId, getTestName(testResult));
            serverConnector.addLogsToTestCaseInSession(testCaseInSessionId, generateExecutionLogs(testResult).toString());
            List<TestStep> testSteps = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps();
            if (testSteps == null) {
                continue;
            }
            recordSteps(serverConnector, sessionId, testCaseInSessionId, testSteps, testResult);
            logger.info(String.format("Snapshots has been recorded with TestCaseSessionId: %d", testCaseInSessionId));
            TestNGResultUtils.setSnapshotTestCaseInSessionId(testResult, testCaseInSessionId);
            TestNGResultUtils.setSeleniumServerReportCreated(testResult, true);
        }
    }
}
Also used : ExceptionUtility(com.seleniumtests.util.ExceptionUtility) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ITestResult(org.testng.ITestResult) ArrayList(java.util.ArrayList) SeleniumTestsContextManager(com.seleniumtests.core.SeleniumTestsContextManager) JSONObject(org.json.JSONObject) IReporter(org.testng.IReporter) SeleniumRobotLogger(com.seleniumtests.util.logging.SeleniumRobotLogger) TestNGResultUtils(com.seleniumtests.core.utils.TestNGResultUtils) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) Map(java.util.Map) Path(java.nio.file.Path) ScreenshotUtil(com.seleniumtests.driver.screenshots.ScreenshotUtil) ITestContext(org.testng.ITestContext) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) SeleniumRobotSnapshotServerConnector(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) File(java.io.File) List(java.util.List) Paths(java.nio.file.Paths) Rectangle(org.openqa.selenium.Rectangle) TestStep(com.seleniumtests.reporter.logger.TestStep) Entry(java.util.Map.Entry) SnapshotCheckType(com.seleniumtests.driver.screenshots.SnapshotCheckType) TestNGContextUtils(com.seleniumtests.core.utils.TestNGContextUtils) TestStep(com.seleniumtests.reporter.logger.TestStep) Set(java.util.Set) ITestResult(org.testng.ITestResult) ITestContext(org.testng.ITestContext)

Example 38 with TestStep

use of com.seleniumtests.reporter.logger.TestStep 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)

Example 39 with TestStep

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

the class SeleniumTestsReporter2 method isVideoInReport.

private boolean isVideoInReport(ITestResult testResult) {
    boolean videoInReport = false;
    TestStep lastTestStep = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getLastTestStep();
    if (lastTestStep != null) {
        for (GenericFile f : lastTestStep.getFiles()) {
            if (f.getFile().getName().contains("video")) {
                videoInReport = true;
            }
        }
    }
    return videoInReport;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) GenericFile(com.seleniumtests.reporter.logger.GenericFile)

Example 40 with TestStep

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

the class BugTrackerReporter method generateReport.

@Override
protected void generateReport(Map<ITestContext, Set<ITestResult>> resultSet, String outdir, boolean optimizeReport, boolean finalGeneration) {
    for (Map.Entry<ITestContext, Set<ITestResult>> entry : resultSet.entrySet()) {
        ITestContext context = entry.getKey();
        for (ITestResult testResult : entry.getValue()) {
            // record only when all executions of a test method are done so that intermediate results (a failed test which has been retried) are not present in list
            if (!Boolean.TRUE.equals(TestNGResultUtils.getNoMoreRetry(testResult)) || TestNGResultUtils.isBugtrackerReportCreated(testResult)) {
                continue;
            }
            // done in case it was null (issue #81)
            SeleniumTestsContext testContext = SeleniumTestsContextManager.setThreadContextFromTestResult(context, testResult);
            BugTracker bugtrackerServer = testContext.getBugTrackerInstance();
            if (bugtrackerServer == null) {
                return;
            }
            // get all bugtracker options
            Map<String, String> issueOptions = new HashMap<>();
            for (TestVariable variable : testContext.getConfiguration().values()) {
                if (variable.getName().startsWith("bugtracker.")) {
                    issueOptions.put(variable.getName().replace("bugtracker.", ""), variable.getValue());
                }
            }
            // application data
            String application = testContext.getApplicationName();
            String environment = testContext.getTestEnv();
            String testNgName = testResult.getTestContext().getCurrentXmlTest().getName();
            String testName = TestNGResultUtils.getUniqueTestName(testResult);
            String description = String.format("Test '%s' failed\n", testName);
            if (testResult.getMethod().getDescription() != null && !testResult.getMethod().getDescription().trim().isEmpty()) {
                description += "Test goal: " + TestNGResultUtils.getTestDescription(testResult);
            }
            // search the last step to get screenshots and failure reason
            List<TestStep> testSteps = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps();
            if (testSteps == null) {
                return;
            }
            // create issue only for failed tests and if it has not been created before
            if (testResult.getStatus() == ITestResult.FAILURE) {
                IssueBean issueBean = bugtrackerServer.createIssue(application, environment, testNgName, testName, description, testSteps, issueOptions);
                // log information on issue
                if (issueBean != null) {
                    if (issueBean.getId() != null && issueBean.getAccessUrl() != null) {
                        TestNGResultUtils.setTestInfo(testResult, "Issue", new HyperlinkInfo(issueBean.getId(), issueBean.getAccessUrl()));
                    } else if (issueBean.getId() != null) {
                        TestNGResultUtils.setTestInfo(testResult, "Issue", new StringInfo(issueBean.getId()));
                    }
                    TestNGResultUtils.setTestInfo(testResult, "Issue date", new StringInfo(issueBean.getCreationDate()));
                }
                TestNGResultUtils.setBugtrackerReportCreated(testResult, true);
            // close issue if test is now OK and a previous issue has been created
            } else if (testResult.getStatus() == ITestResult.SUCCESS) {
                bugtrackerServer.closeIssue(application, environment, testNgName, testName);
                TestNGResultUtils.setBugtrackerReportCreated(testResult, true);
            }
        }
    }
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Set(java.util.Set) SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) ITestResult(org.testng.ITestResult) IssueBean(com.seleniumtests.connectors.bugtracker.IssueBean) HashMap(java.util.HashMap) TestVariable(com.seleniumtests.core.TestVariable) BugTracker(com.seleniumtests.connectors.bugtracker.BugTracker) HyperlinkInfo(com.seleniumtests.reporter.info.HyperlinkInfo) ITestContext(org.testng.ITestContext) StringInfo(com.seleniumtests.reporter.info.StringInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TestStep (com.seleniumtests.reporter.logger.TestStep)190 Test (org.testng.annotations.Test)148 GenericTest (com.seleniumtests.GenericTest)120 ArrayList (java.util.ArrayList)80 TestAction (com.seleniumtests.reporter.logger.TestAction)47 File (java.io.File)37 ScreenShot (com.seleniumtests.driver.screenshots.ScreenShot)25 Snapshot (com.seleniumtests.reporter.logger.Snapshot)24 ErrorCause (com.seleniumtests.core.testanalysis.ErrorCause)19 TestMessage (com.seleniumtests.reporter.logger.TestMessage)16 GenericFile (com.seleniumtests.reporter.logger.GenericFile)15 MockitoTest (com.seleniumtests.MockitoTest)12 HashMap (java.util.HashMap)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 ITestResult (org.testng.ITestResult)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 Uft (com.seleniumtests.connectors.extools.Uft)8 DriverExceptions (com.seleniumtests.customexception.DriverExceptions)8 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)7 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)6