/* +----------------------------------------------------------------------+ | Class: CLauncher | | | | Developper: Eric Gavaldo (eric.gavaldo@xqual.com) | | Version: 1.4 | +----------------------------------------------------------------------+ */ package com.xqual.xlauncher.selenium_python; 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 Python. * @author egavaldo */ public class CLauncherImpl extends CLauncher implements IConstantsResults { // +==============================================================+ // | Attributes | // +==============================================================+ static final String TRACE_HEADER = "{selenium pythn} "; static final String TRACE_FILE_NAME = "selenium_python_traces.txt"; // parameters impacting executing at run time set by the test operator private String testRootPath; private String pythonInstallPath; private String javaInstallPath; private String seleniumServerJarPath; private String seleniumServerOptions; //private String seleniumPythonLibPath; private String pyunitInterpreter; private File workingDir; private File seleniumServerTraceFile; private CRunner seleniumServer = null; private static final String JAVA_INTERPRETER_EXE = CUtils.getExecutableName("java"); private static final String PYTHON_INTERPRETER_EXE = CUtils.getExecutableName("python"); // +==============================================================+ // | 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/python_pyunit pythonInstallPath = getStringParamValue("Selenium - Python", "Python install path"); // i.e. C:/Python26 seleniumServerJarPath = getStringParamValue("Selenium - Python", "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 - Python", "Server options"); //seleniumPythonLibPath = getStringParamValue("Selenium - Python", "Selenium python lib path"); // i.e. Y:/Externals/Selenium-1.0/selenium-remote-control-1.0.1/selenium-python-client-driver-1.0.1 javaInstallPath = getStringParamValue("Java", "Java install path"); // i.e. C:/Program Files/Java/jdk1.6.0_17 pyunitInterpreter = CFileUtils.quoteFilePath(pythonInstallPath + "/" + PYTHON_INTERPRETER_EXE) + " "; } 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); } // +------------------------------------+ // | Run the Selenium server // +------------------------------------+ 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 seleniumServer = new CRunner("[Selenium Server] ", CFileUtils.quoteFilePath(javaInstallPath + "/bin/" + JAVA_INTERPRETER_EXE) + " -jar " + CFileUtils.quoteFilePath(seleniumServerJarPath) + " " + seleniumServerOptions, workingDir); short result = seleniumServer.requestAction(IRunner.START_PROCESS, IRunner.DO_NOT_WAIT_END_OF_EXECUTION, seleniumServerTraceFile); if (result == RESULT_SUCCESS) { executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "initialize: selenium server successfully launched!")); traceln(LOG_PRIORITY_INFO, "selenium server launched successfully"); } else { executionSteps.add(new CExecutionStep(RESULT_FAILURE, "initialize: couldn't run selenium server! Maybe a server is already running")); traceln(LOG_PRIORITY_SEVERE, "couldn't run selenium server!"); } // let the time to the server to initialize (this is done only once per campaign session) CUtils.sleep(7000); 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 + "]..."); // +------------------------------------+ // | Set the module path // +------------------------------------+ // This is the equivalent of a class path but in python you cannot pass it in argument :( // // SET PYTHONPATH=.; // // i.e. PYTHONPATH=.;Y:\Externals\pyunit-1.4.1;Y:\Externals\Selenium-1.0\selenium-remote-control-1.0.1\selenium-python-client-driver-1.0.1 // // Unfortunately, this cannot be done programatically so THIS MUST BE DONE MANUALLY (ONLY ONCE ON THE HOST) workingDir = new File(testRootPath + (testPath.length() > 0 ? "/" + testPath : "")); // parent folder of all the test classes 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(); // +------------------------------------+ // | Interpret the script // +------------------------------------+ File seleniumTraceFile = new File(TRACE_FILE_NAME); if (seleniumTraceFile.exists()) seleniumTraceFile.delete(); try { seleniumTraceFile.createNewFile(); } catch (IOException e) { traceln(LOG_PRIORITY_SEVERE, "Could not create file \"" + TRACE_FILE_NAME + "\"..."); } // +------------------------------------+ // | Run the PyUnit test class // +------------------------------------+ CRunner pyUnitRunner = new CRunner("[" + testId + "] "+ testPath + "." + testName, pyunitInterpreter.toString() + " " + testName + ".py", workingDir); // here we redirect the output to a file for later parsing short result = pyUnitRunner.requestAction(IRunner.START_PROCESS, IRunner.WAIT_END_OF_EXECUTION, seleniumTraceFile); // +------------------------------------+ // | Add the output console to the test // +------------------------------------+ addAttachment(seleniumTraceFile); 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); } } @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() { traceln(LOG_PRIORITY_INFO, "terminate killing selenium server..."); // +------------------------------------+ // | Run the Selenium server // +------------------------------------+ seleniumServer.killProcess(); 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(TRACE_FILE_NAME); 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; boolean testHaveBeenRun = false; try { FileInputStream fileInputStream = new FileInputStream(resultFile); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); int nbLines = 0; while ((line = bufferedReader.readLine()) != null) { nbLines++; line = line.trim(); System.out.println(">" + line); if (line.startsWith("FAIL:")) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, line)); errorDetected = true; } else if (line.startsWith("Ran ")) { if (line.startsWith("Ran 0 tests")) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, line)); errorDetected = true; } else { executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, line)); } testHaveBeenRun = true; } else if (line.startsWith("FAILED (")) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, line)); errorDetected = true; } else { //traceln(LOG_PRIORITY_SEVERE, "unknown tag!"); } } if (nbLines == 0) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, "empty trace")); errorDetected = true; } } 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 || !testHaveBeenRun) { return new CReturnStatus(RESULT_FAILURE, executionSteps); } else { return new CReturnStatus(RESULT_SUCCESS, executionSteps); } } }