Search in sources :

Example 26 with AnswerItem

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

the class SoapService method callSOAP.

@Override
public AnswerItem<AppService> callSOAP(String envelope, String servicePath, String soapOperation, String attachmentUrl, List<AppServiceHeader> header, String token, int timeOutMs, String system) {
    AnswerItem result = new AnswerItem();
    String unescapedEnvelope = StringEscapeUtils.unescapeXml(envelope);
    boolean is12SoapVersion = SOAP_1_2_NAMESPACE_PATTERN.matcher(unescapedEnvelope).matches();
    AppService serviceSOAP = factoryAppService.create("", AppService.TYPE_SOAP, null, "", "", envelope, "", servicePath, "", soapOperation, "", null, "", null);
    serviceSOAP.setTimeoutms(timeOutMs);
    ByteArrayOutputStream out = null;
    MessageEvent message = null;
    if (StringUtil.isNullOrEmpty(servicePath)) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_SERVICEPATHMISSING);
        result.setResultMessage(message);
        return result;
    }
    if (StringUtil.isNullOrEmpty(envelope)) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_ENVELOPEMISSING);
        result.setResultMessage(message);
        return result;
    }
    // If header is null we create the list empty.
    if (header == null) {
        header = new ArrayList<>();
    }
    // We feed the header with token + Standard SOAP header.
    if (token != null) {
        header.add(factoryAppServiceHeader.create(null, "cerberus-token", token, "Y", 0, "", "", null, "", null));
    }
    if (StringUtil.isNullOrEmpty(soapOperation)) {
        header.add(factoryAppServiceHeader.create(null, "SOAPAction", "", "Y", 0, "", "", null, "", null));
    } else {
        header.add(factoryAppServiceHeader.create(null, "SOAPAction", "\"" + soapOperation + "\"", "Y", 0, "", "", null, "", null));
    }
    header.add(factoryAppServiceHeader.create(null, "Content-Type", is12SoapVersion ? SOAPConstants.SOAP_1_2_CONTENT_TYPE : SOAPConstants.SOAP_1_1_CONTENT_TYPE, "Y", 0, "", "", null, "", null));
    serviceSOAP.setHeaderList(header);
    SOAPConnectionFactory soapConnectionFactory;
    SOAPConnection soapConnection = null;
    try {
        // Initialize SOAP Connection
        soapConnectionFactory = SOAPConnectionFactory.newInstance();
        soapConnection = soapConnectionFactory.createConnection();
        LOG.debug("Connection opened");
        // Create SOAP Request
        LOG.debug("Create request");
        SOAPMessage input = createSoapRequest(envelope, soapOperation, header, token);
        // Add attachment File if specified
        if (!StringUtil.isNullOrEmpty(attachmentUrl)) {
            this.addAttachmentPart(input, attachmentUrl);
            // Store the SOAP Call
            out = new ByteArrayOutputStream();
            input.writeTo(out);
            LOG.debug("WS call with attachement : " + out.toString());
            serviceSOAP.setServiceRequest(out.toString());
        } else {
            // Store the SOAP Call
            out = new ByteArrayOutputStream();
            input.writeTo(out);
            LOG.debug("WS call : " + out.toString());
        }
        // We already set the item in order to keep the request message in case of failure of SOAP calls.
        serviceSOAP.setService(servicePath);
        result.setItem(serviceSOAP);
        // Call the WS
        LOG.debug("Calling WS.");
        // Reset previous Authentification.
        Authenticator.setDefault(null);
        serviceSOAP.setProxyWithCredential(false);
        serviceSOAP.setProxyUser(null);
        SOAPMessage soapResponse = null;
        if (proxyService.useProxy(servicePath, system)) {
            // Get Proxy host and port from parameters.
            String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", system, DEFAULT_PROXY_HOST);
            int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", system, DEFAULT_PROXY_PORT);
            serviceSOAP.setProxy(true);
            serviceSOAP.setProxyHost(proxyHost);
            serviceSOAP.setProxyPort(proxyPort);
            // Create the Proxy.
            SocketAddress sockaddr = new InetSocketAddress(proxyHost, proxyPort);
            try (Socket socket = new Socket()) {
                socket.connect(sockaddr, 10000);
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(socket.getInetAddress(), proxyPort));
                if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", system, DEFAULT_PROXYAUTHENT_ACTIVATE)) {
                    // Get the credentials from parameters.
                    String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", system, DEFAULT_PROXYAUTHENT_USER);
                    String proxyPass = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", system, DEFAULT_PROXYAUTHENT_PASSWORD);
                    serviceSOAP.setProxyWithCredential(true);
                    serviceSOAP.setProxyUser(proxyUser);
                    // Define the credential to the proxy.
                    initializeProxyAuthenticator(proxyUser, proxyPass);
                }
                // Call with Proxy.
                soapResponse = sendSOAPMessage(input, servicePath, proxy, timeOutMs);
            } catch (Exception e) {
                LOG.warn(e.toString());
            }
        } else {
            serviceSOAP.setProxy(false);
            serviceSOAP.setProxyHost(null);
            serviceSOAP.setProxyPort(0);
            // Call without proxy.
            soapResponse = sendSOAPMessage(input, servicePath, null, timeOutMs);
        // soapResponse = soapConnection.call(input, servicePath);
        }
        LOG.debug("Called WS.");
        out = new ByteArrayOutputStream();
        // Store the response
        soapResponse.writeTo(out);
        LOG.debug("WS response received.");
        LOG.debug("WS response : " + out.toString());
        message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CALLSOAP);
        message.setDescription(message.getDescription().replace("%SERVICEPATH%", servicePath).replace("%SOAPMETHOD%", soapOperation));
        result.setResultMessage(message);
        // soapResponse.getSOAPPart().getEnvelope().getBody().getFault().getFaultCode();
        // We save convert to string the final response from SOAP request.
        serviceSOAP.setResponseHTTPBody(SoapUtil.convertSoapMessageToString(soapResponse));
        // Get result Content Type.
        serviceSOAP.setResponseHTTPBodyContentType(appServiceService.guessContentType(serviceSOAP, AppService.RESPONSEHTTPBODYCONTENTTYPE_XML));
        result.setItem(serviceSOAP);
    } catch (SOAPException | UnsupportedOperationException | IOException | SAXException | ParserConfigurationException | CerberusException e) {
        LOG.error(e.toString());
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP);
        message.setDescription(message.getDescription().replace("%SERVICEPATH%", servicePath).replace("%SOAPMETHOD%", soapOperation).replace("%DESCRIPTION%", e.getMessage()));
        result.setResultMessage(message);
        return result;
    } finally {
        try {
            if (soapConnection != null) {
                soapConnection.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (SOAPException | IOException ex) {
            LOG.error(ex);
        } finally {
            result.setResultMessage(message);
        }
    }
    return result;
}
Also used : SOAPConnectionFactory(javax.xml.soap.SOAPConnectionFactory) IFactoryAppService(org.cerberus.crud.factory.IFactoryAppService) AppService(org.cerberus.crud.entity.AppService) CerberusException(org.cerberus.exception.CerberusException) MessageEvent(org.cerberus.engine.entity.MessageEvent) InetSocketAddress(java.net.InetSocketAddress) SOAPConnection(javax.xml.soap.SOAPConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AnswerItem(org.cerberus.util.answer.AnswerItem) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) CerberusException(org.cerberus.exception.CerberusException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Proxy(java.net.Proxy) SOAPException(javax.xml.soap.SOAPException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 27 with AnswerItem

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

the class WebDriverService method getValueFromHTML.

@Override
public String getValueFromHTML(Session session, Identifier identifier) {
    AnswerItem answer = this.getSeleniumElement(session, identifier, false, false);
    String result = null;
    if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {
        WebElement webElement = (WebElement) answer.getItem();
        if (webElement != null) {
            if (webElement.getTagName().equalsIgnoreCase("select")) {
                if (webElement.getAttribute("disabled") == null || webElement.getAttribute("disabled").isEmpty()) {
                    Select select = new Select(webElement);
                    result = select.getFirstSelectedOption().getText();
                } else {
                    result = webElement.getText();
                // result = "Unable to retrieve, element disabled ?";
                }
            } else if (webElement.getTagName().equalsIgnoreCase("option") || webElement.getTagName().equalsIgnoreCase("input")) {
                result = webElement.getAttribute("value");
            } else {
                result = webElement.getText();
            }
            /**
             * If return is empty, we search for hidden tags
             */
            if (StringUtil.isNullOrEmpty(result)) {
                String script = "return arguments[0].innerHTML";
                try {
                    result = (String) ((JavascriptExecutor) session.getDriver()).executeScript(script, webElement);
                } catch (Exception e) {
                    LOG.debug("getValueFromHTML locator : '" + identifier.getIdentifier() + "=" + identifier.getLocator() + "', exception : " + e.getMessage());
                }
            }
        }
    } else if (answer.isCodeEquals(MessageEventEnum.ACTION_FAILED_WAIT_NO_SUCH_ELEMENT.getCode())) {
        throw new NoSuchElementException(identifier.getIdentifier() + "=" + identifier.getLocator());
    }
    return result;
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) AnswerItem(org.cerberus.util.answer.AnswerItem) PatternSyntaxException(java.util.regex.PatternSyntaxException) CerberusEventException(org.cerberus.exception.CerberusEventException) TimeoutException(org.openqa.selenium.TimeoutException) AWTException(java.awt.AWTException) WebDriverException(org.openqa.selenium.WebDriverException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchElementException(org.openqa.selenium.NoSuchElementException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) NoSuchElementException(org.openqa.selenium.NoSuchElementException)

Example 28 with AnswerItem

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

the class WebDriverService method doSeleniumActionMouseDown.

@Override
public MessageEvent doSeleniumActionMouseDown(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.clickAndHold(webElement);
                actions.build().perform();
                message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_MOUSEDOWN);
                message.setDescription(message.getDescription().replace("%ELEMENT%", identifier.getIdentifier() + "=" + identifier.getLocator()));
                return message;
            }
        }
        return answer.getResultMessage();
    } catch (NoSuchElementException exception) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_MOUSEDOWN_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 29 with AnswerItem

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

the class WebDriverService method doSeleniumActionSelect.

@Override
public MessageEvent doSeleniumActionSelect(Session session, Identifier object, Identifier property) {
    MessageEvent message;
    try {
        Select select;
        try {
            AnswerItem answer = this.getSeleniumElement(session, object, true, true);
            if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {
                WebElement webElement = (WebElement) answer.getItem();
                if (webElement != null) {
                    select = new Select(webElement);
                    this.selectRequestedOption(select, property, object.getIdentifier() + "=" + object.getLocator());
                    message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SELECT);
                    message.setDescription(message.getDescription().replace("%ELEMENT%", object.getIdentifier() + "=" + object.getLocator()));
                    message.setDescription(message.getDescription().replace("%DATA%", property.getIdentifier() + "=" + property.getLocator()));
                    return message;
                }
            }
            return answer.getResultMessage();
        } catch (NoSuchElementException exception) {
            message = new MessageEvent(MessageEventEnum.ACTION_FAILED_SELECT_NO_SUCH_ELEMENT);
            message.setDescription(message.getDescription().replace("%ELEMENT%", object.getIdentifier() + "=" + object.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 (CerberusEventException ex) {
        LOG.warn(ex);
        return ex.getMessageError();
    }
}
Also used : CerberusEventException(org.cerberus.exception.CerberusEventException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) AnswerItem(org.cerberus.util.answer.AnswerItem) NoSuchElementException(org.openqa.selenium.NoSuchElementException) TimeoutException(org.openqa.selenium.TimeoutException)

Example 30 with AnswerItem

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

the class WebDriverService method doSeleniumActionFocusToIframe.

@Override
public MessageEvent doSeleniumActionFocusToIframe(Session session, Identifier identifier) {
    MessageEvent message;
    try {
        AnswerItem answer = this.getSeleniumElement(session, identifier, false, false);
        if (answer.isCodeEquals(MessageEventEnum.ACTION_SUCCESS_WAIT_ELEMENT.getCode())) {
            WebElement webElement = (WebElement) answer.getItem();
            if (webElement != null) {
                session.getDriver().switchTo().frame(webElement);
                message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_FOCUSTOIFRAME);
                message.setDescription(message.getDescription().replace("%IFRAME%", identifier.getIdentifier() + "=" + identifier.getLocator()));
                return message;
            }
        }
        return answer.getResultMessage();
    } catch (NoSuchElementException exception) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_FOCUS_NO_SUCH_ELEMENT);
        message.setDescription(message.getDescription().replace("%IFRAME%", identifier.getIdentifier() + "=" + identifier.getLocator()));
        LOG.debug(exception.toString());
    } 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);
    }
    return message;
}
Also used : 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)

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