Search in sources :

Example 31 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class WebDriverService method doSeleniumActionMouseUp.

@Override
public MessageEvent doSeleniumActionMouseUp(Session session, Identifier identifier) {
    MessageEvent message;
    try {
        AnswerItem answer = this.getSeleniumElement(session, identifier, true, true);
        if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {
            WebElement webElement = (WebElement) answer.getItem();
            if (webElement != null) {
                Actions actions = new Actions(session.getDriver());
                actions.release(webElement);
                actions.build().perform();
                message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEUP);
                message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));
                return message;
            }
        }
        return answer.getResultMessage();
    } catch (NoSuchElementException exception) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEUP_NO_SUCH_ELEMENT);
        message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));
        LOG.debug(exception.toString());
        return message;
    } catch (TimeoutException exception) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);
        message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));
        LOG.warn(exception.toString());
        return message;
    } catch (WebDriverException exception) {
        LOG.warn(exception.toString());
        return parseWebDriverException(exception);
    }
}
Also used : Actions(org.openqa.selenium.interactions.Actions) MessageEvent(org.cerberus.engine.entity.MessageEvent) WebElement(org.openqa.selenium.WebElement) AnswerItem(org.cerberus.util.answer.AnswerItem) NoSuchElementException(org.openqa.selenium.NoSuchElementException) TimeoutException(org.openqa.selenium.TimeoutException) WebDriverException(org.openqa.selenium.WebDriverException)

Example 32 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class WebDriverService method getValueFromHTMLVisible.

@Override
public String getValueFromHTMLVisible(Session session, Identifier identifier) {
    String result = null;
    AnswerItem answer = this.getSeleniumElement(session, identifier, true, false);
    if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {
        WebElement webElement = (WebElement) answer.getItem();
        if (webElement != null) {
            if (webElement.getTagName().equalsIgnoreCase("select")) {
                Select select = (Select) webElement;
                result = select.getFirstSelectedOption().getText();
            } else if (webElement.getTagName().equalsIgnoreCase("option") || webElement.getTagName().equalsIgnoreCase("input")) {
                result = webElement.getAttribute("value");
            } else {
                result = webElement.getText();
            }
        }
    }
    return result;
}
Also used : Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 33 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class WebDriverService method doSeleniumActionMouseOver.

@Override
public MessageEvent doSeleniumActionMouseOver(Session session, Identifier identifier) {
    MessageEvent message;
    try {
        AnswerItem answer = this.getSeleniumElement(session, identifier, true, true);
        if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {
            WebElement menuHoverLink = (WebElement) answer.getItem();
            if (menuHoverLink != null) {
                Actions actions = new Actions(session.getDriver());
                actions.moveToElement(menuHoverLink);
                actions.build().perform();
                message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEOVER);
                message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));
                return message;
            }
        }
        return answer.getResultMessage();
    } catch (NoSuchElementException exception) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEOVER_NO_SUCH_ELEMENT);
        message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));
        LOG.debug(exception.toString());
        return message;
    } catch (TimeoutException exception) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);
        message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));
        LOG.warn(exception.toString());
        return message;
    } catch (WebDriverException exception) {
        LOG.warn(exception.toString());
        return parseWebDriverException(exception);
    }
}
Also used : Actions(org.openqa.selenium.interactions.Actions) MessageEvent(org.cerberus.engine.entity.MessageEvent) WebElement(org.openqa.selenium.WebElement) AnswerItem(org.cerberus.util.answer.AnswerItem) NoSuchElementException(org.openqa.selenium.NoSuchElementException) TimeoutException(org.openqa.selenium.TimeoutException) WebDriverException(org.openqa.selenium.WebDriverException)

Example 34 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class SikuliService method doSikuliAction.

@Override
public AnswerItem<JSONObject> doSikuliAction(Session session, String action, String locator, String text) {
    AnswerItem<JSONObject> answer = new AnswerItem<JSONObject>();
    MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);
    HttpURLConnection connection = null;
    BufferedReader in = null;
    PrintStream os = null;
    URL url;
    String urlToConnect = "http://" + session.getHost() + ":" + session.getPort() + "/extra/ExecuteSikuliAction";
    try {
        /**
         * Connect to ExecuteSikuliAction Servlet Through SeleniumServer
         */
        url = new URL(urlToConnect);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        JSONObject postParameters = generatePostParameters(action, locator, text, session.getCerberus_selenium_wait_element());
        connection.setDoOutput(true);
        // Send post request
        os = new PrintStream(connection.getOutputStream());
        os.println(postParameters.toString());
        os.println("|ENDS|");
        if (connection == null || connection.getResponseCode() != 200) {
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_SERVER_NOT_REACHABLE);
        }
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        /**
         * Wait here until receiving |ENDR| String
         */
        while (!(inputLine = in.readLine()).equals("|ENDR|")) {
            response.append(inputLine);
        }
        /**
         * Convert received string into JSONObject
         */
        JSONObject objReceived = new JSONObject(response.toString());
        answer.setItem(objReceived);
        if (objReceived.has("status")) {
            if ("Failed".equals(objReceived.getString("status"))) {
                msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
            } else {
                msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);
            }
        } else {
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
        }
        in.close();
    } catch (MalformedURLException ex) {
        LOG.warn(ex);
        MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_SERVER_NOT_REACHABLE);
        mes.setDescription(mes.getDescription().replace("%URL%", urlToConnect));
        msg = mes;
    } catch (FileNotFoundException ex) {
        LOG.warn(ex);
        MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_FILE_NOT_FOUND);
        mes.setDescription(mes.getDescription().replace("%FILE%", locator));
        msg = mes;
    } catch (IOException ex) {
        LOG.warn(ex);
        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    } catch (JSONException ex) {
        LOG.warn(ex);
        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    } catch (MimeTypeException ex) {
        LOG.warn(ex);
        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    } finally {
        if (os != null) {
            os.close();
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : PrintStream(java.io.PrintStream) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) MessageEvent(org.cerberus.engine.entity.MessageEvent) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException) AnswerItem(org.cerberus.util.answer.AnswerItem) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.JSONObject) MimeTypeException(org.apache.tika.mime.MimeTypeException) BufferedReader(java.io.BufferedReader)

Example 35 with AnswerItem

use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.

the class DataLibService method getFromDataLib.

@Override
public AnswerList<HashMap<String, String>> getFromDataLib(TestDataLib lib, TestCaseCountryProperties testCaseCountryProperty, TestCaseExecution tCExecution, TestCaseExecutionData testCaseExecutionData) {
    AnswerItem<HashMap<String, String>> resultColumns;
    AnswerList<HashMap<String, String>> resultData;
    AnswerList<HashMap<String, String>> result;
    MessageEvent msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS);
    // Length contains the nb of rows that the result must fetch. If defined at 0 we force at 1.
    Integer nbRowsRequested = 0;
    try {
        nbRowsRequested = Integer.parseInt(testCaseExecutionData.getLength());
        if (nbRowsRequested < 1) {
            nbRowsRequested = 1;
        }
    } catch (NumberFormatException e) {
        LOG.error(e.toString());
    }
    /**
     * Gets the list of columns (subdata) to get from TestDataLibData.
     */
    resultColumns = getSubDataFromType(lib);
    HashMap<String, String> columnList = null;
    // Manage error message.
    if (resultColumns.getResultMessage().getCode() == MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_SUBDATA.getCode()) {
        AnswerItem answerDecode = new AnswerItem();
        columnList = resultColumns.getItem();
        // Now that we have the list of column with subdata and value, we can try to decode it.
        if (columnList != null) {
            for (Map.Entry<String, String> entry : columnList.entrySet()) {
                // Loop on all Column in order to decode all values.
                // SubData
                String eKey = entry.getKey();
                // Parsing Answer
                String eValue = entry.getValue();
                try {
                    answerDecode = variableService.decodeStringCompletly(eValue, tCExecution, null, false);
                    columnList.put(eKey, (String) answerDecode.getItem());
                    if (!(answerDecode.isCodeStringEquals("OK"))) {
                        // If anything wrong with the decode --> we stop here with decode message in the action result.
                        result = new AnswerList();
                        result.setDataList(null);
                        msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GLOBAL_SUBDATAISSUE);
                        msg.setDescription(msg.getDescription().replace("%SUBDATAMESSAGE%", answerDecode.getMessageDescription().replace("%FIELD%", "Column value '" + eValue + "'")));
                        result.setResultMessage(msg);
                        LOG.debug("Datalib interupted due to decode 'column value' Error.");
                        return result;
                    }
                } catch (CerberusEventException cex) {
                    LOG.warn(cex);
                }
            }
        }
    } else if (resultColumns.getResultMessage().getCode() == MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SUBDATA.getCode()) {
        result = new AnswerList();
        result.setDataList(null);
        msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GLOBAL_SUBDATAISSUE);
        msg.setDescription(msg.getDescription().replace("%SUBDATAMESSAGE%", resultColumns.getMessageDescription()));
        result.setResultMessage(msg);
        return result;
    }
    /**
     * Get List of DataObject in a format List<Map<String>> - 1 item per row
     * with key = column and value = content
     */
    int rowLimit = testCaseCountryProperty.getRowLimit();
    if (testCaseCountryProperty.getNature().equalsIgnoreCase(TestCaseCountryProperties.NATURE_STATIC)) {
        // If Nature of the property is static, we don't need to getch more than reqested record.
        rowLimit = nbRowsRequested;
    }
    resultData = getDataObjectList(lib, columnList, rowLimit, tCExecution, testCaseExecutionData);
    // Manage error message.
    if (resultData.getResultMessage().getCode() == MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_DATA.getCode()) {
        if (resultData.getDataList().size() < nbRowsRequested) {
            // We check if the data provided is enought to provide the answer.
            result = new AnswerList();
            result.setDataList(null);
            msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GLOBAL_NOTENOUGHTDATA);
            msg.setDescription(msg.getDescription().replace("%DATAMESSAGE%", resultData.getMessageDescription()).replace("%NBREQUEST%", Integer.toString(nbRowsRequested)));
            result.setResultMessage(msg);
            return result;
        }
    } else if (resultData.getResultMessage().getCode() == MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GENERIC_NODATA.getCode()) {
        result = new AnswerList();
        result.setDataList(null);
        msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GLOBAL_NODATA);
        msg.setDescription(msg.getDescription().replace("%DATAMESSAGE%", resultData.getMessageDescription()));
        result.setResultMessage(msg);
        return result;
    } else {
        result = new AnswerList();
        result.setDataList(null);
        msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GLOBAL_DATAISSUE);
        msg.setDescription(msg.getDescription().replace("%DATAMESSAGE%", resultData.getMessageDescription()));
        result.setResultMessage(msg);
        return result;
    }
    /**
     * Filter out the result from requested rows depending on the nature
     */
    result = filterWithNature(testCaseCountryProperty.getNature(), resultData, tCExecution, testCaseCountryProperty, nbRowsRequested);
    // Manage error message.
    if (result.getResultMessage().getCode() == MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_NATURE.getCode()) {
        msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS_GETFROMDATALIB_GLOBAL);
        msg.setDescription(msg.getDescription().replace("%DATAMESSAGE%", resultData.getMessageDescription()).replace("%FILTERNATUREMESSAGE%", result.getMessageDescription()));
        result.setResultMessage(msg);
    } else if (result.getResultMessage().getCode() == MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GENERIC_NATURENOMORERECORD.getCode()) {
        // if the script does not return
        result.setDataList(null);
        msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GLOBAL_NODATALEFT);
        msg.setDescription(msg.getDescription().replace("%DATAMESSAGE%", resultData.getMessageDescription()).replace("%FILTERNATUREMESSAGE%", result.getMessageDescription()));
        result.setResultMessage(msg);
    } else {
        // other error had occured
        result.setDataList(null);
        msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_GLOBAL_GENERIC);
        msg.setDescription(msg.getDescription().replace("%DATAMESSAGE%", resultData.getMessageDescription()).replace("%FILTERNATUREMESSAGE%", result.getMessageDescription()));
        result.setResultMessage(msg);
    }
    return result;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) AnswerItem(org.cerberus.util.answer.AnswerItem) CerberusEventException(org.cerberus.exception.CerberusEventException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

AnswerItem (org.cerberus.util.answer.AnswerItem)322 MessageEvent (org.cerberus.engine.entity.MessageEvent)212 JSONObject (org.json.JSONObject)206 ApplicationContext (org.springframework.context.ApplicationContext)98 AnswerList (org.cerberus.util.answer.AnswerList)90 ArrayList (java.util.ArrayList)78 JSONArray (org.json.JSONArray)74 PolicyFactory (org.owasp.html.PolicyFactory)74 List (java.util.List)72 JSONException (org.json.JSONException)69 HashMap (java.util.HashMap)60 ILogEventService (org.cerberus.crud.service.ILogEventService)58 SQLException (java.sql.SQLException)57 Connection (java.sql.Connection)55 PreparedStatement (java.sql.PreparedStatement)53 Answer (org.cerberus.util.answer.Answer)53 ResultSet (java.sql.ResultSet)52 CerberusException (org.cerberus.exception.CerberusException)44 IOException (java.io.IOException)34 ServletException (javax.servlet.ServletException)24