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()));
}
}
}
}
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;
}
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");
}
}
}
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);
}
}
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();
}
}
}
Aggregations