/* +----------------------------------------------------------------------+ | Class: CLauncher | | | | Developper: Eric Gavaldo (eric.gavaldo@xqual.com) | | Version: 1.7 | +----------------------------------------------------------------------+ */ package com.xqual.xlauncher.selenium_html; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Vector; import com.xqual.xagent.launcher.CExecutionStep; import com.xqual.xagent.launcher.CLauncher; import com.xqual.xagent.launcher.CParamParsingException; import com.xqual.xagent.launcher.CReturnStatus; import com.xqual.xagent.launcher.runner.CRunner; import com.xqual.xagent.launcher.runner.IRunner; import com.xqual.xcommon.CAttribute; import com.xqual.xcommon.IConstantsResults; import com.xqual.xcommon.utils.CFileUtils; import com.xqual.xcommon.utils.CUtils; /** * The CLauncherImpl implementation of ILauncher for Selenium HTML. * @author egavaldo */ public class CLauncherImpl extends CLauncher implements IConstantsResults { // +==============================================================+ // | Attributes | // +==============================================================+ static final String TRACE_HEADER = "{selenium html } "; // parameters impacting executing at run time set by the test operator private String testRootPath; private String javaInstallPath; private String seleniumServerJarPath; private String seleniumServerOptions; private String browser; private String domainUrl; //private boolean trustAllSSLCertificates; //private String userExtensions; private File workingDir; private static final String JAVA_INTERPRETER_EXE = CUtils.getExecutableName("java"); // +==============================================================+ // | Constructors | // +==============================================================+ public CLauncherImpl() { super(TRACE_HEADER); } // +==============================================================+ // | Methods | // +==============================================================+ @Override public CReturnStatus initialize(int sutId, String sutName, String sutVersion) { setSutDetails(sutId, sutName, sutVersion); setDefaultTestcaseMustBeCreated(true); // if there is not testcase, let the systems create a default one // check the configuration sent by the manager printConfiguration(); Vector executionSteps = new Vector(); try { // retrieve the parameters we need testRootPath = getStringParamValue("General", "Test root path"); // i.e. Y:/XStudio/src/test/selenium/html javaInstallPath = getStringParamValue("Selenium - Html", "Java install path"); // i.e. C:/Program Files/Java/jdk1.6.0_17 seleniumServerJarPath = getStringParamValue("Selenium - Html", "Server JAR path"); // i.e. Y:/Externals/Selenium-1.0/selenium-remote-control-1.0.1/selenium-server-1.0.1/selenium-server.jar seleniumServerOptions = getStringParamValue("Selenium - Html", "Server options"); browser = getStringParamValue("Selenium - Html", "Browser"); // i.e. iehta (it can also firefox or chrome) domainUrl = getStringParamValue("Selenium - Html", "Domain URL"); // i.e. http://www.xqual.com //trustAllSSLCertificates = getBooleanParamValue("Selenium - Html", "Trust all SSL certificates"); //userExtensions = getStringParamValue("Selenium - Html", "User extensions"); // i.e. C:/scripts/myUserExtensions.js } catch (CParamParsingException e) { traceln(LOG_PRIORITY_SEVERE, "parsing error during initialization"); executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Exception during initialize: " + e.getMessage())); return new CReturnStatus(RESULT_FAILURE, executionSteps); } return new CReturnStatus(RESULT_SUCCESS, executionSteps); } @Override public CReturnStatus preRun(int testId, String testPath, String testName, Vector attributes, String additionalInfo) { traceln(LOG_PRIORITY_INFO, "preRun testId=" + testId + " testPath=" + testPath + " [" + testName + "]..."); Vector executionSteps = new Vector(); return new CReturnStatus(RESULT_SUCCESS, executionSteps); } @Override public CReturnStatus run(int testId, String testPath, String testName, int testcaseIndex, String testcaseName, String additionalInfo) { traceln(LOG_PRIORITY_INFO, "run testId=" + testId + " testPath=" + testRootPath + "/" + testPath + "/" + testName + " testcaseIndex=" + testcaseIndex + "..."); Vector executionSteps = new Vector(); workingDir = new File(testRootPath); // parent folder of all the test classes // +------------------------------------+ // | Run the Selenium server // +------------------------------------+ File seleniumServerReportFile = new File("selenium_server_report.html"); if (seleniumServerReportFile.exists()) seleniumServerReportFile.delete(); try { seleniumServerReportFile.createNewFile(); } catch (IOException e) { traceln(LOG_PRIORITY_SEVERE, "Could not create file \"selenium_server_report.html\"..."); } File seleniumServerTraceFile = new File("selenium_server_traces.txt"); if (seleniumServerTraceFile.exists()) seleniumServerTraceFile.delete(); try { seleniumServerTraceFile.createNewFile(); } catch (IOException e) { traceln(LOG_PRIORITY_SEVERE, "Could not create file \"selenium_server_traces.txt\"..."); } // if there is already a selenium server running that's not a problem has 2 server cannot listen on the same port this will fail nicely CRunner seleniumServer = new CRunner("[Selenium Server] ", CFileUtils.quoteFilePath(javaInstallPath + "/bin/" + JAVA_INTERPRETER_EXE) + " -jar " + CFileUtils.quoteFilePath(seleniumServerJarPath) + " " + seleniumServerOptions + " " + //(trustAllSSLCertificates ? "-trustAllSSLCertificates " : "") + //(userExtensions != null && userExtensions.length()>0 ? "-userExtensions \"" + userExtensions + "\" " : "") + "-htmlSuite \"*" + browser + "\" \"" + domainUrl + "\" " + CFileUtils.quoteFilePath(testRootPath + "/" + testPath + "/" + testName + ".html") + " " + CFileUtils.quoteFilePath(seleniumServerReportFile.getAbsolutePath()) + " " + "-log " + CFileUtils.quoteFilePath(seleniumServerTraceFile.getAbsolutePath()), workingDir); short result = seleniumServer.requestAction(IRunner.START_PROCESS, IRunner.WAIT_END_OF_EXECUTION); // +------------------------------------+ // | Run the Selenium server // +------------------------------------+ seleniumServer.killProcess(); // +----------------------------------------------+ // | Add the output console and report to the test // +----------------------------------------------+ addAttachment(seleniumServerTraceFile); addAttachment(seleniumServerReportFile); /* if (result == RESULT_FAILURE) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, "execution process failed")); parseResultFile(executionSteps); // to have the details anyway return new CReturnStatus(RESULT_FAILURE, executionSteps); } else { executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "execution process succeeded")); return parseResultFile(executionSteps); } */ return parseResultFile(executionSteps); // even when succeeding, it looks like selenium exits with return code > 0 } @Override public CReturnStatus postRun(int testId, String testPath, String testName) { traceln(LOG_PRIORITY_INFO, "postRun testId=" + testId + " testPath=" + testPath + ":" + testName + "..."); Vector executionSteps = new Vector(); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "postRun: succeeded")); return new CReturnStatus(RESULT_SUCCESS, executionSteps); } @Override public CReturnStatus terminate() { Vector executionSteps = new Vector(); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "Terminate")); return new CReturnStatus(RESULT_SUCCESS, executionSteps); } // +--------------------------+ // ¦ Utilities ¦ // +--------------------------+ private CReturnStatus parseResultFile(Vector executionSteps) { // parse the result file to get the result and the execution steps File resultFile = new File("selenium_server_report.html"); if (!resultFile.exists()) { traceln(LOG_PRIORITY_SEVERE, "Result file not found!"); executionSteps.add(new CExecutionStep(RESULT_FAILURE, "run: result file not found!")); return new CReturnStatus(RESULT_FAILURE, executionSteps); } else { executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "run: result file found")); } String line; boolean errorDetected = false; try { FileInputStream fileInputStream = new FileInputStream(resultFile); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); while ((line = bufferedReader.readLine()) != null) { line = line.trim(); System.out.println(">" + line); if (line.startsWith("result:")) { line = bufferedReader.readLine().trim(); String globalResult = line.substring(4, line.length()-5); if (globalResult.equalsIgnoreCase("passed")) { executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "Result: " + globalResult)); } else { executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Result: " + globalResult)); errorDetected = true; } } else if (line.startsWith("totalTime:")) { line = bufferedReader.readLine().trim(); String totalTime = line.substring(4, line.length()-5); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "totalTime: " + totalTime)); } else if (line.startsWith("numTestTotal:")) { line = bufferedReader.readLine().trim(); String numTestTotal = line.substring(4, line.length()-5); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "numTestTotal: " + numTestTotal)); } else if (line.startsWith("numTestPasses:")) { line = bufferedReader.readLine().trim(); String numTestPasses = line.substring(4, line.length()-5); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "numTestPasses: " + numTestPasses)); } else if (line.startsWith("numTestFailures:")) { line = bufferedReader.readLine().trim(); int numTestFailures = Integer.parseInt(line.substring(4, line.length()-5)); executionSteps.add(new CExecutionStep(numTestFailures > 0 ? RESULT_FAILURE: RESULT_SUCCESS, "numTestFailures: " + numTestFailures)); } else if (line.endsWith("status_passed\">")) { line = bufferedReader.readLine().trim(); String[] array = line.split(">"); String testName = array[1].substring(1, array[1].length()-9); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "passed: " + testName)); } else if (line.endsWith("status_failed\">")) { line = bufferedReader.readLine().trim(); String[] array = line.split(">"); String testName = array[1].substring(1, array[1].length()-9); executionSteps.add(new CExecutionStep(RESULT_FAILURE, "failed: " + testName)); } else { //traceln(LOG_PRIORITY_SEVERE, "unknown tag!"); } } } catch (Exception e) { traceln(LOG_PRIORITY_SEVERE, "exception whle parsing the result file: " + e); executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Exception while parsing the result file: " + e)); errorDetected = true; } if (errorDetected) { return new CReturnStatus(RESULT_FAILURE, executionSteps); } else{ return new CReturnStatus(RESULT_SUCCESS, executionSteps); } } }