Search in sources :

Example 6 with Recorder

use of org.cerberus.engine.entity.Recorder in project cerberus-source by cerberustesting.

the class RecorderService method recordServiceCall.

@Override
public List<TestCaseExecutionFile> recordServiceCall(TestCaseExecution testCaseExecution, TestCaseStepActionExecution testCaseStepActionExecution, Integer control, String property, AppService se) {
    // Used for logging purposes
    String logPrefix = Infos.getInstance().getProjectNameAndVersion() + " - ";
    List<TestCaseExecutionFile> objectFileList = new ArrayList<TestCaseExecutionFile>();
    TestCaseExecutionFile object = null;
    String test = null;
    String testCase = null;
    String step = null;
    String index = null;
    String sequence = null;
    if (testCaseStepActionExecution != null) {
        test = testCaseExecution.getTest();
        testCase = testCaseExecution.getTestCase();
        step = String.valueOf(testCaseStepActionExecution.getStep());
        index = String.valueOf(testCaseStepActionExecution.getIndex());
        sequence = String.valueOf(testCaseStepActionExecution.getSequence());
    }
    String controlString = control.equals(0) ? null : String.valueOf(control);
    long runId = testCaseExecution.getId();
    int propertyIndex = 0;
    if (!(StringUtil.isNullOrEmpty(property))) {
        propertyIndex = 1;
    }
    try {
        // Service Call META data information.
        Recorder recorderRequest = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, property, propertyIndex, "call", "json", false);
        recordFile(recorderRequest.getFullPath(), recorderRequest.getFileName(), se.toJSONOnExecution().toString());
        // Index file created to database.
        object = testCaseExecutionFileFactory.create(0, runId, recorderRequest.getLevel(), "Service Call", recorderRequest.getRelativeFilenameURL(), "JSON", "", null, "", null);
        testCaseExecutionFileService.save(object);
        objectFileList.add(object);
        // REQUEST.
        if (!(StringUtil.isNullOrEmpty(se.getServiceRequest()))) {
            String messageFormatExt = "txt";
            String messageFormat = TestCaseExecutionFile.FILETYPE_TXT;
            if (se.getServiceRequest().startsWith("{")) {
                // TODO find a better solution to guess the format of the request.
                messageFormatExt = "json";
                messageFormat = TestCaseExecutionFile.FILETYPE_JSON;
            } else if (se.getServiceRequest().startsWith("<")) {
                messageFormatExt = "xml";
                messageFormat = TestCaseExecutionFile.FILETYPE_XML;
            }
            recorderRequest = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, property, propertyIndex, "request", messageFormatExt, false);
            recordFile(recorderRequest.getFullPath(), recorderRequest.getFileName(), se.getServiceRequest());
            // Index file created to database.
            object = testCaseExecutionFileFactory.create(0, runId, recorderRequest.getLevel(), "Request", recorderRequest.getRelativeFilenameURL(), messageFormat, "", null, "", null);
            testCaseExecutionFileService.save(object);
            objectFileList.add(object);
        }
        // RESPONSE if exists.
        if (!(StringUtil.isNullOrEmpty(se.getResponseHTTPBody()))) {
            String messageFormatExt = "txt";
            String messageFormat = TestCaseExecutionFile.FILETYPE_TXT;
            switch(se.getResponseHTTPBodyContentType()) {
                case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON:
                    messageFormatExt = "json";
                    messageFormat = TestCaseExecutionFile.FILETYPE_JSON;
                    break;
                case AppService.RESPONSEHTTPBODYCONTENTTYPE_XML:
                    messageFormatExt = "xml";
                    messageFormat = TestCaseExecutionFile.FILETYPE_XML;
                    break;
                default:
                    messageFormatExt = "txt";
                    messageFormat = TestCaseExecutionFile.FILETYPE_TXT;
                    break;
            }
            Recorder recorderResponse = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, property, propertyIndex, "response", messageFormatExt, false);
            recordFile(recorderResponse.getFullPath(), recorderResponse.getFileName(), se.getResponseHTTPBody());
            // Index file created to database.
            object = testCaseExecutionFileFactory.create(0, runId, recorderResponse.getLevel(), "Response", recorderResponse.getRelativeFilenameURL(), messageFormat, "", null, "", null);
            testCaseExecutionFileService.save(object);
            objectFileList.add(object);
        }
    } catch (Exception ex) {
        LOG.error(logPrefix + ex.toString());
    }
    return objectFileList;
}
Also used : ArrayList(java.util.ArrayList) Recorder(org.cerberus.engine.entity.Recorder) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) TestCaseExecutionFile(org.cerberus.crud.entity.TestCaseExecutionFile) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with Recorder

use of org.cerberus.engine.entity.Recorder in project cerberus-source by cerberustesting.

the class RecorderService method recordScreenshot.

@Override
public TestCaseExecutionFile recordScreenshot(TestCaseExecution testCaseExecution, TestCaseStepActionExecution testCaseStepActionExecution, Integer control) {
    TestCaseExecutionFile object = null;
    String test = testCaseStepActionExecution.getTest();
    String testCase = testCaseStepActionExecution.getTestCase();
    String step = String.valueOf(testCaseStepActionExecution.getStep());
    String index = String.valueOf(testCaseStepActionExecution.getIndex());
    String sequence = String.valueOf(testCaseStepActionExecution.getSequence());
    String controlString = control.equals(0) ? null : String.valueOf(control);
    long runId = testCaseExecution.getId();
    String applicationType = testCaseExecution.getApplicationObj().getType();
    // Used for logging purposes
    String logPrefix = Infos.getInstance().getProjectNameAndVersion() + " - [" + test + " - " + testCase + " - step: " + step + " action: " + sequence + "] ";
    LOG.debug(logPrefix + "Doing screenshot.");
    /**
     * Take Screenshot and write it
     */
    File newImage = null;
    if (applicationType.equals(Application.TYPE_GUI) || applicationType.equals(Application.TYPE_APK) || applicationType.equals(Application.TYPE_IPA)) {
        newImage = this.webdriverService.takeScreenShotFile(testCaseExecution.getSession());
    } else if (applicationType.equals(Application.TYPE_FAT)) {
        newImage = this.sikuliService.takeScreenShotFile(testCaseExecution.getSession());
    }
    if (newImage != null) {
        try {
            Recorder recorder = this.initFilenames(runId, test, testCase, step, index, sequence, controlString, null, 0, "screenshot", "png", false);
            LOG.debug(logPrefix + "FullPath " + recorder.getFullPath());
            File dir = new File(recorder.getFullPath());
            if (!dir.exists()) {
                LOG.debug(logPrefix + "Create directory for execution " + recorder.getFullPath());
                dir.mkdirs();
            }
            // Getting the max size of the screenshot.
            long maxSizeParam = parameterService.getParameterIntegerByKey("cerberus_screenshot_max_size", "", 1048576);
            if (maxSizeParam < newImage.length()) {
                LOG.warn(logPrefix + "Screen-shot size exceeds the maximum defined in configurations " + newImage.getName() + " destination: " + recorder.getRelativeFilenameURL());
            }
            // copies the temp file to the execution file
            FileUtils.copyFile(newImage, new File(recorder.getFullFilename()));
            LOG.debug(logPrefix + "Copy file finished with success - source: " + newImage.getName() + " destination: " + recorder.getRelativeFilenameURL());
            // Index file created to database.
            object = testCaseExecutionFileFactory.create(0, testCaseExecution.getId(), recorder.getLevel(), "Screenshot", recorder.getRelativeFilenameURL(), "PNG", "", null, "", null);
            testCaseExecutionFileService.save(object);
            // deletes the temporary file
            FileUtils.forceDelete(newImage);
            LOG.debug(logPrefix + "Temp file deleted with success " + newImage.getName());
            LOG.debug(logPrefix + "Screenshot done in : " + recorder.getRelativeFilenameURL());
        } catch (IOException ex) {
            LOG.error(logPrefix + ex.toString());
        } catch (CerberusException ex) {
            LOG.error(logPrefix + ex.toString());
        }
    } else {
        LOG.warn(logPrefix + "Screenshot returned null.");
    }
    return object;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) Recorder(org.cerberus.engine.entity.Recorder) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) TestCaseExecutionFile(org.cerberus.crud.entity.TestCaseExecutionFile) IOException(java.io.IOException) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) File(java.io.File) TestCaseExecutionFile(org.cerberus.crud.entity.TestCaseExecutionFile)

Example 8 with Recorder

use of org.cerberus.engine.entity.Recorder in project cerberus-source by cerberustesting.

the class RecorderService method initFilenames.

private Recorder initFilenames(long exeID, String test, String testCase, String step, String index, String sequence, String controlString, String property, int propertyIndex, String filename, String extention, boolean manual) throws CerberusException {
    Recorder newRecorder = new Recorder();
    try {
        String rootFolder = "";
        /**
         * Root folder initialisation. The root folder is configures from
         * the parameter cerberus_exeautomedia_path or
         * cerberus_exemanualmedia_path .
         */
        if (!manual) {
            rootFolder = parameterService.getParameterStringByKey("cerberus_exeautomedia_path", "", "");
        } else {
            rootFolder = parameterService.getParameterStringByKey("cerberus_exemanualmedia_path", "", "");
        }
        rootFolder = StringUtil.addSuffixIfNotAlready(rootFolder, File.separator);
        newRecorder.setRootFolder(rootFolder);
        /**
         * SubFolder. Subfolder is split in order to reduce the nb of folder
         * within a folder. 2 levels of 2 digits each. he last level is the
         * execution id.
         */
        String subFolder = getStorageSubFolder(exeID);
        newRecorder.setSubFolder(subFolder);
        String subFolderURL = getStorageSubFolderURL(exeID);
        newRecorder.setSubFolder(subFolderURL);
        /**
         * FullPath. Concatenation of the rootfolder and subfolder.
         */
        String fullPath = rootFolder + subFolder;
        newRecorder.setFullPath(fullPath);
        /**
         * Filename. If filename is not define, we assign it from the test,
         * testcase, step action and control.
         */
        StringBuilder sbfileName = new StringBuilder();
        if (!StringUtil.isNullOrEmpty(test)) {
            sbfileName.append(test).append("-");
        }
        if (!StringUtil.isNullOrEmpty(testCase)) {
            sbfileName.append(testCase).append("-");
        }
        if (!StringUtil.isNullOrEmpty(step)) {
            sbfileName.append("S").append(step).append("-");
        }
        if (!StringUtil.isNullOrEmpty(index)) {
            sbfileName.append("I").append(index).append("-");
        }
        if (!StringUtil.isNullOrEmpty(sequence)) {
            sbfileName.append("A").append(sequence).append("-");
        }
        if (!StringUtil.isNullOrEmpty(controlString)) {
            sbfileName.append("C").append(controlString).append("-");
        }
        if (!StringUtil.isNullOrEmpty(property)) {
            sbfileName.append(property).append("-");
        }
        if (propertyIndex != 0) {
            sbfileName.append(propertyIndex).append("-");
        }
        if (!StringUtil.isNullOrEmpty(filename)) {
            sbfileName.append(filename).append("-");
        }
        String fileName = StringUtil.removeLastChar(sbfileName.toString(), 1) + "." + extention;
        fileName = fileName.replace(" ", "");
        newRecorder.setFileName(fileName);
        /**
         * Level. 5 levels possible. Keys are defined seperated by -. 1/
         * Execution level --> emptyString. 2/ Step level -->
         * test+testcase+Step 3/ Action level --> test+testcase+Step+action
         * 4/ Control level --> test+testcase+Step+action+control 5/
         * Property level --> property+index
         */
        String level = "";
        if (!(StringUtil.isNullOrEmpty(controlString))) {
            level = test + "-" + testCase + "-" + step + "-" + index + "-" + sequence + "-" + controlString;
        } else if (!(StringUtil.isNullOrEmpty(sequence))) {
            level = test + "-" + testCase + "-" + step + "-" + index + "-" + sequence;
        } else if (!(StringUtil.isNullOrEmpty(step))) {
            level = test + "-" + testCase + "-" + step + "-" + index;
        } else if (!(StringUtil.isNullOrEmpty(property))) {
            level = property + "-" + propertyIndex;
        }
        newRecorder.setLevel(level);
        /**
         * Final Filename with full path.
         */
        String fullFilename = rootFolder + File.separator + subFolder + File.separator + fileName;
        newRecorder.setFullFilename(fullFilename);
        String relativeFilenameURL = subFolderURL + "/" + fileName;
        newRecorder.setRelativeFilenameURL(relativeFilenameURL);
    } catch (Exception ex) {
        LOG.error("Error on data init. " + ex.toString());
    }
    return newRecorder;
}
Also used : Recorder(org.cerberus.engine.entity.Recorder) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

Recorder (org.cerberus.engine.entity.Recorder)8 CerberusException (org.cerberus.exception.CerberusException)8 IOException (java.io.IOException)7 TestCaseExecutionFile (org.cerberus.crud.entity.TestCaseExecutionFile)7 IFactoryTestCaseExecutionFile (org.cerberus.crud.factory.IFactoryTestCaseExecutionFile)7 FileNotFoundException (java.io.FileNotFoundException)6 File (java.io.File)5 JSONException (org.json.JSONException)5 FileOutputStream (java.io.FileOutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 ArrayList (java.util.ArrayList)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 AnswerItem (org.cerberus.util.answer.AnswerItem)1 JSONArray (org.json.JSONArray)1