Search in sources :

Example 26 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class ReporterControler method cleanAttachments.

/**
 * Delete all files in html and screenshot folders that are not directly references by any test step
 * @param suites
 */
private void cleanAttachments(Map<ITestContext, Set<ITestResult>> resultSet) {
    List<File> usedFiles = new ArrayList<>();
    List<File> allFiles = new ArrayList<>();
    Set<ITestResult> allResultsSet = new HashSet<>();
    for (Set<ITestResult> rs : resultSet.values()) {
        allResultsSet.addAll(rs);
    }
    for (ITestResult testResult : allResultsSet) {
        // without context, nothing can be done
        SeleniumTestsContext testContext = TestNGResultUtils.getSeleniumRobotTestContext(testResult);
        if (testContext == null) {
            continue;
        }
        // get files referenced by the steps
        for (TestStep testStep : testContext.getTestStepManager().getTestSteps()) {
            try {
                testStep.moveAttachments(testContext.getOutputDirectory());
            } catch (IOException e) {
                logger.error("Cannot move attachment " + e.getMessage());
            }
            usedFiles.addAll(testStep.getAllAttachments());
        }
        allFiles.addAll(listAttachments(testContext));
    }
    for (File file : allFiles) {
        if (!usedFiles.contains(file)) {
            try {
                Files.deleteIfExists(file.toPath());
            } catch (IOException e) {
                logger.info(String.format("File %s not deleted: %s", file.getAbsolutePath(), e.getMessage()));
            }
        }
    }
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) ITestResult(org.testng.ITestResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) HashSet(java.util.HashSet)

Example 27 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class SeleniumTestsReporter2 method generateSuiteSummaryReport.

/**
 * Generate summary report for all test methods
 * @param suites
 * @param suiteName
 * @param map
 * @return	map containing test results
 */
public Map<ITestContext, List<ITestResult>> generateSuiteSummaryReport(Map<ITestContext, Set<ITestResult>> resultSet, boolean resourcesFromCdn) {
    // build result list for each TestNG test
    Map<ITestContext, List<ITestResult>> methodResultsMap = new LinkedHashMap<>();
    Map<ITestResult, Map<String, String>> testInfosMap = new HashMap<>();
    Map<ITestResult, List<TestStep>> allSteps = new HashMap<>();
    Set<String> allInfoKeys = new HashSet<>();
    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());
        for (ITestResult result : methodResults) {
            SeleniumTestsContext testContext = TestNGResultUtils.getSeleniumRobotTestContext(result);
            String fileName;
            if (testContext != null) {
                fileName = testContext.getRelativeOutputDir() + "/TestReport.html";
                allSteps.put(result, testContext.getTestStepManager().getTestSteps());
            } else {
                fileName = getTestName(result) + "/TestReport.html";
                allSteps.put(result, new ArrayList<>());
            }
            result.setAttribute(METHOD_RESULT_FILE_NAME, fileName);
            // be sure test name is initialized
            TestNGResultUtils.setUniqueTestName(result, getTestName(result));
            // add test info
            Map<String, String> testInfos = TestNGResultUtils.getTestInfoEncoded(result, "html");
            testInfosMap.put(result, testInfos);
            allInfoKeys.addAll(testInfos.keySet());
        }
        methodResultsMap.put(entry.getKey(), methodResults);
    }
    try {
        VelocityEngine ve = initVelocityEngine();
        Template t = ve.getTemplate("/reporter/templates/report.part.suiteSummary.vm");
        VelocityContext context = new VelocityContext();
        List<String> allSortedInfoKeys = new ArrayList<>(allInfoKeys);
        allSortedInfoKeys.sort(null);
        context.put("staticPathPrefix", "");
        // optimize reports means that resources are get from internet
        context.put("localResources", !resourcesFromCdn);
        synchronized (allSteps) {
            // as we use a synchronizedList and we iterate on it
            context.put("tests", methodResultsMap);
            context.put("steps", allSteps);
            context.put("infos", testInfosMap);
            context.put("infoKeys", allSortedInfoKeys);
        }
        StringWriter writer = new StringWriter();
        t.merge(context, writer);
        generateReport(Paths.get(getOutputDirectory(), "SeleniumTestReport.html").toFile(), writer.toString());
    } catch (Exception e) {
        generationErrorMessage = "generateSuiteSummaryReport error:" + e.getMessage();
        logger.error("generateSuiteSummaryReport error: ", e);
    }
    return methodResultsMap;
}
Also used : StepStatus(com.seleniumtests.reporter.logger.TestStep.StepStatus) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) ErrorCause(com.seleniumtests.core.testanalysis.ErrorCause) ExceptionUtility(com.seleniumtests.util.ExceptionUtility) HashMap(java.util.HashMap) FileFilterUtils(org.apache.commons.io.filefilter.FileFilterUtils) ITestResult(org.testng.ITestResult) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Template(org.apache.velocity.Template) SeleniumTestsContextManager(com.seleniumtests.core.SeleniumTestsContextManager) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) IReporter(org.testng.IReporter) VelocityEngine(org.apache.velocity.app.VelocityEngine) SeleniumRobotLogger(com.seleniumtests.util.logging.SeleniumRobotLogger) TestType(com.seleniumtests.driver.TestType) TestNGResultUtils(com.seleniumtests.core.utils.TestNGResultUtils) Map(java.util.Map) StringUtility(com.seleniumtests.util.StringUtility) PrintWriter(java.io.PrintWriter) SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) ITestContext(org.testng.ITestContext) IResultMap(org.testng.IResultMap) StringWriter(java.io.StringWriter) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) Collectors(java.util.stream.Collectors) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) GenericFile(com.seleniumtests.reporter.logger.GenericFile) List(java.util.List) ITestNGMethod(org.testng.ITestNGMethod) Paths(java.nio.file.Paths) TestStep(com.seleniumtests.reporter.logger.TestStep) Entry(java.util.Map.Entry) Collections(java.util.Collections) VelocityEngine(org.apache.velocity.app.VelocityEngine) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) ITestResult(org.testng.ITestResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) ITestContext(org.testng.ITestContext) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) IOException(java.io.IOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IResultMap(org.testng.IResultMap)

Example 28 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class SeleniumTestsReporter2 method fillContextWithTestParams.

/**
 * Fill velocity context with test context
 * @param velocityContext
 */
private void fillContextWithTestParams(VelocityContext velocityContext, ITestResult testResult) {
    SeleniumTestsContext selTestContext = TestNGResultUtils.getSeleniumRobotTestContext(testResult);
    if (selTestContext != null) {
        String browser = selTestContext.getBrowser().getBrowserType();
        String app = selTestContext.getApp();
        String appPackage = selTestContext.getAppPackage();
        TestType testType = selTestContext.getTestType();
        if (browser != null) {
            browser = browser.replace("*", "");
        }
        String browserVersion = selTestContext.getWebBrowserVersion();
        if (browserVersion != null) {
            browser = browser + browserVersion;
        }
        velocityContext.put(APPLICATION, "");
        if (testType == null) {
            velocityContext.put(APPLICATION_TYPE, "Error in initialization");
        // Log URL for web test and app info for app test
        } else if (testType.family().equals(TestType.WEB)) {
            velocityContext.put(APPLICATION_TYPE, "Browser");
            velocityContext.put(APPLICATION, browser);
        } else if (testType.family().equals(TestType.APP)) {
            // Either app Or app package and app activity is specified to run test on app
            if (StringUtils.isNotBlank(appPackage)) {
                velocityContext.put(APPLICATION_TYPE, "App Package");
                velocityContext.put(APPLICATION, appPackage);
            } else if (StringUtils.isNotBlank(app)) {
                velocityContext.put(APPLICATION_TYPE, "App");
                velocityContext.put(APPLICATION, app);
            }
        } else if (testType.family().equals(TestType.NON_GUI)) {
            velocityContext.put(APPLICATION_TYPE, "");
        } else {
            velocityContext.put(APPLICATION_TYPE, "Invalid Test type");
        }
    }
}
Also used : SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) TestType(com.seleniumtests.driver.TestType)

Example 29 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class SeleniumTestsReporter2 method generateSingleTestReport.

public void generateSingleTestReport(ITestResult testResult, boolean resourcesFromCdn) {
    // issue #81: recreate test context from this context (due to multithreading, this context may be null if parallel testing is done)
    SeleniumTestsContext testContext = SeleniumTestsContextManager.setThreadContextFromTestResult(testResult.getTestContext(), testResult);
    try {
        copyResources();
        // issue #284: copy resources specific to the single test report. They are moved here so that the file can be used without global resources
        FileUtils.copyInputStreamToFile(Thread.currentThread().getContextClassLoader().getResourceAsStream("reporter/templates/seleniumRobot_solo.css"), Paths.get(testContext.getOutputDirectory(), RESOURCES_FOLDER, "seleniumRobot_solo.css").toFile());
        FileUtils.copyInputStreamToFile(Thread.currentThread().getContextClassLoader().getResourceAsStream("reporter/templates/app.min.js"), Paths.get(testContext.getOutputDirectory(), RESOURCES_FOLDER, "app.min.js").toFile());
        if (!SeleniumTestsContextManager.getGlobalContext().getOptimizeReports()) {
            FileUtils.copyInputStreamToFile(Thread.currentThread().getContextClassLoader().getResourceAsStream("reporter/templates/iframeResizer.min.js"), Paths.get(testContext.getOutputDirectory(), RESOURCES_FOLDER, "iframeResizer.min.js").toFile());
        }
        generateExecutionReport(testContext, testResult, getTestStatus(testResult), resourcesFromCdn);
        logger.info("Completed Test Report Generation.");
        // do not recreate this report anymore
        TestNGResultUtils.setHtmlReportCreated(testResult, true);
    } catch (Exception e) {
        logger.error("Error writing test report: " + getTestName(testResult), e);
    }
}
Also used : SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) IOException(java.io.IOException)

Example 30 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class TestManagerReporter method generateReport.

@Override
protected void generateReport(Map<ITestContext, Set<ITestResult>> resultSet, String outdir, boolean optimizeResult, boolean finalGeneration) {
    // record only when all tests are executed so that intermediate results (a failed test which has been retried) are not present in list
    if (!finalGeneration) {
        return;
    }
    // Record test method reports for each result which has not already been recorded
    for (Map.Entry<ITestContext, Set<ITestResult>> entry : resultSet.entrySet()) {
        for (ITestResult testResult : entry.getValue()) {
            SeleniumTestsContext testContext = SeleniumTestsContextManager.setThreadContextFromTestResult(testResult.getTestContext(), testResult);
            TestManager testManager = testContext.getTestManagerInstance();
            if (testManager == null) {
                return;
            }
            testManager.login();
            // do not record twice the same result
            if (!TestNGResultUtils.isTestManagerReportCreated(testResult)) {
                testManager.recordResult(testResult);
                testManager.recordResultFiles(testResult);
                TestNGResultUtils.setTestManagereportCreated(testResult, true);
            }
            testManager.logout();
        }
    }
}
Also used : Set(java.util.Set) SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) ITestResult(org.testng.ITestResult) ITestContext(org.testng.ITestContext) TestManager(com.seleniumtests.connectors.tms.TestManager) Map(java.util.Map)

Aggregations

SeleniumTestsContext (com.seleniumtests.core.SeleniumTestsContext)54 Test (org.testng.annotations.Test)44 GenericTest (com.seleniumtests.GenericTest)43 XmlTest (org.testng.xml.XmlTest)32 ITestResult (org.testng.ITestResult)12 AndroidCapabilitiesFactory (com.seleniumtests.browserfactory.AndroidCapabilitiesFactory)11 DriverConfig (com.seleniumtests.driver.DriverConfig)11 MutableCapabilities (org.openqa.selenium.MutableCapabilities)11 HashMap (java.util.HashMap)9 TestVariable (com.seleniumtests.core.TestVariable)7 ConnectorsTest (com.seleniumtests.ConnectorsTest)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 TestStep (com.seleniumtests.reporter.logger.TestStep)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 ITestContext (org.testng.ITestContext)4 IOException (java.io.IOException)3 Map (java.util.Map)3