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;
}
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;
}
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);
}
}
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();
}
}
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;
}
Aggregations