All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ats.executor.drivers.engines.ApiDriverEngine Maven / Gradle / Ivy

The newest version!
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */

package com.ats.executor.drivers.engines;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.parsers.ParserConfigurationException;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.xml.sax.SAXException;

import com.ats.AtsSingleton;
import com.ats.data.Dimension;
import com.ats.data.Point;
import com.ats.data.Rectangle;
import com.ats.driver.ApplicationProperties;
import com.ats.element.AtsBaseElement;
import com.ats.element.DialogBox;
import com.ats.element.FoundElement;
import com.ats.element.test.TestElement;
import com.ats.executor.ActionStatus;
import com.ats.executor.ActionTestScript;
import com.ats.executor.SendKeyData;
import com.ats.executor.TestBound;
import com.ats.executor.channels.Channel;
import com.ats.executor.drivers.IDriverInfo;
import com.ats.executor.drivers.desktop.DesktopWindow;
import com.ats.executor.drivers.desktop.SystemDriver;
import com.ats.executor.drivers.engines.webservices.ApiExecutor;
import com.ats.executor.drivers.engines.webservices.AtsCookieJar;
import com.ats.executor.drivers.engines.webservices.RestApiExecutor;
import com.ats.executor.drivers.engines.webservices.SoapApiExecutor;
import com.ats.generator.objects.MouseDirection;
import com.ats.generator.variables.CalculatedProperty;
import com.ats.graphic.ImageTemplateMatchingSimple;
import com.ats.script.Project;
import com.ats.script.actions.ActionApi;
import com.ats.script.actions.ActionChannelStart;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;

import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.Response;

public class ApiDriverEngine extends DriverEngine implements IDriverEngine{

	private static final byte[] SOAP_ICON = Base64.getDecoder().decode("iVBORw0KGgoAAAANSUhEUgAAAC4AAAAYCAYAAACFms+HAAAA8HpUWHRSYXcgcHJvZmlsZSB0eXBlIGV4aWYAAHjajVFbbsQgDPznFHsEv2LgOGSTlXqDHn8HME2zUqVawtiDsYchnd9fr/ToJkWSbbl4dSeYVavSEBSatg/PZMMPkzPO+I4nXpcEkGLXmVYJ/ASOmCOvUc+rfjVaATdE23XQWuD7Hd+joZTPRsFAeU6mIy5EI5VgZDN/BiOvJd+edjzpbuVapll8c84Gb0I5e0VchCxDz6MT1TzGQ6OYtICVr1IBJzmVFTKrqk2W2pdpw67DS0IhqyPphx3yITzhK0EBjets/Gr0I+ZvbS6N/rD/PCu9AZrfda5cnwy4AAABg2lDQ1BJQ0MgcHJvZmlsZQAAeJx9kT1Iw0AcxV9TpSKVDu0gopChOtlFRRxLFYtgobQVWnUwufQLmjQkKS6OgmvBwY/FqoOLs64OroIg+AHi7OCk6CIl/i8pt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AzLTAyVDE2OjIwOjA2Ii8+CiAgICA8L3JkZjpTZXE+CiAgIDwveG1wTU06SGlzdG9yeT4KICA8L3JkZjpEZXNjcmlwdGlvbj4KIDwvcmRmOlJERj4KPC94OnhtcG1ldGE+CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA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xuF5fYrVUws/dsYWCX2MBfLt2ARic3Gkes/P4zjcYBIrC8vEzST+HMIQN6odVuUtuvUHu3yf7hbpwoQcxDDRqNA9bfrlJ6u4YgeH4C3/PimgAxiQ2hf22Y2NU6pxuRijgFMTbKq6j0SCSTLC3eBfNjyCFI5CBBKk0QjBOkxgmSE2A+Dx98xzf3HzGZnSaVDMhMTWICnU6Xbq+HxowNQcZZN/cpndONZF4wcxwe1ant1Zi7mWd+boHqTpVm+zD2Acjnb1HIFwcF1um2ebX6knR6ChGP2akcBqgZakp5qxR114+K0L1faFcBrmIcN49QDWm1T6KObI6N8hpJ3+H5Cebm5nhTqtM4OcC5BA5BCDETTBxnnVPqh3v88eI3CvNFsplJxPNotlvs7GxSP9yLgTtElE6nTaNRxwhBegMJXWh2F71ImOmw+UpsTyZx0RlqHsjwrBElSGMGJMqcKIL2lYva0Poie5aR840XBYFiopjJpx2yIrCjbmQQ21S0aziYZ7GdgUQHMfpBRNQP1Cuj55D+J4yfqXFhXuwkV2tA57JhHwQpV1gtH/iXxC4S1UY46sf/7/O4fNKq6zega+CXHP8AKhYWfO2tuKQAAAAASUVORK5CYII=");

	private ApiExecutor executor;

	private PrintStream logStream;
	private AtsCookieJar cookieJar;

	public ApiDriverEngine(
			Channel channel,
			ActionStatus status,
			String path,
			IDriverInfo driverInfo,
			SystemDriver systemDriver,
			ApplicationProperties props, 
			ActionTestScript script,
			boolean enableLearning) {

		super(channel, driverInfo, systemDriver, props, 0, 0, enableLearning);

		getSystemDriver().setEngine(new SystemDriverEngine(channel, new DesktopWindow(), enableLearning));

		try {

			final File logsFolder = new File(Project.TARGET_FOLDER + File.separator + Project.LOGS_FOLDER);
			if(!logsFolder.exists()) {
				logsFolder.mkdirs();
			}

			final Path logFile = logsFolder.toPath().resolve(script.getTestName() + "_ws_" + System.currentTimeMillis() + ".log");
			logStream = new PrintStream(logFile.toFile());
			logStream.println("Start ATS ws channel ...");

		} catch (FileNotFoundException e1) {}

		final int maxTry = AtsSingleton.getInstance().getMaxTryWebservice();
		final int timeout = AtsSingleton.getInstance().getWebServiceTimeOut();

		if(channel.isUseCookie()) {
			cookieJar = new AtsCookieJar();
		}

		final Builder builder = createHttpBuilder(
				timeout,
				cookieJar,
				logStream,
				channel.getTopScriptPackage(),
				getClass().getClassLoader());

		if(channel.getPerformance() == ActionChannelStart.NEOLOAD) {
			channel.setNeoloadDesignApi(AtsSingleton.getInstance().getNeoloadDesignApi());
			builder.proxy(AtsSingleton.getInstance().getNeoloadProxy().getHttpProxy());
		}else {
			builder.proxy(AtsSingleton.getInstance().getProxy().getHttpProxy());
		}

		final OkHttpClient client = builder.build();

		if(applicationPath == null) {
			applicationPath = path;
		}

		final Request request = new Request.Builder().url(applicationPath).get().build();

		String wsContent = null;
		try {

			final Response response = client.newCall(request).execute();
			wsContent = CharStreams.toString(new InputStreamReader(response.body().byteStream(), Charsets.UTF_8)).trim();
			response.close();

			String osName = response.header("server");
			if(osName == null || osName.isBlank()) {
				osName = response.protocol().name();
				if(osName == null || osName.isBlank()) {
					osName = "N/A";
				}
			}

			if(wsContent.endsWith("definitions>")) {
				try {
					executor = new SoapApiExecutor(logStream, cookieJar, client, timeout, maxTry, channel, wsContent, applicationPath);
					channel.setApplicationData(Channel.API, osName, ActionApi.SOAP, ((SoapApiExecutor)executor).getOperations(), SOAP_ICON);
				} catch (SAXException | IOException | ParserConfigurationException e) {
					status.setError(ActionStatus.CHANNEL_START_ERROR, e.getMessage());
				}
			}else {
				channel.setApplicationData(Channel.API, osName, ActionApi.REST, REST_ICON);
				executor = new RestApiExecutor(logStream, cookieJar, client, timeout, maxTry, channel, applicationPath);
			}

		} catch (IOException e) {
			status.setError(ActionStatus.CHANNEL_START_ERROR, "service is not responding -> " + e.getMessage());
			e.printStackTrace(logStream);
		}
	}

	@Override
	public void api(ActionStatus status, ActionApi api) {
		executor.execute(status, api);
	}

	@Override
	public String getSource() {
		return executor.getSource();
	}

	@Override
	public List findElements(boolean sysComp, TestElement testObject, String tagName, String[] attributes, String[] attributesValues, Predicate searchPredicate, WebElement startElement) {
		return executor.findElements(channel, testObject, tagName, searchPredicate);
	}

	@Override
	public List findElements(TestElement parent, ImageTemplateMatchingSimple template) {
		return null;
	}

	@Override
	public String getAttribute(ActionStatus status, FoundElement element, String attributeName, int maxTry) {
		return executor.getElementAttribute(element.getId(), attributeName);
	}

	@Override
	public CalculatedProperty[] getAttributes(FoundElement element, boolean reload) {
		return executor.getElementAttributes(element.getId());
	}

	@Override
	public CalculatedProperty[] getHtmlAttributes(FoundElement element) {
		return getAttributes(element, true);
	}

	@Override
	public void setSysProperty(String propertyName, String propertyValue) {

	}

	@Override
	public void refreshElementMapLocation() {}

	@Override
	public void setWindowToFront() {
	}

	@Override
	public void goToUrl(ActionStatus status, String url) {
	}

	@Override
	public void close() {
		logStream.println("Close ATS WebService channel");
	}

	@Override
	public List findSelectOptions(TestBound dimension, TestElement element) {
		return Collections.emptyList();
	}

	@Override
	public void selectOptionsItem(ActionStatus status, TestElement element, CalculatedProperty selectProperty, boolean keepSelect) {
	}

	@Override
	public void loadParents(FoundElement hoverElement) {
	}

	@Override
	public WebElement getRootElement(Channel cnl) {
		return null;
	}

	//------------------------------------------------------------------------------------------------------------------------------------

	@Override
	public void waitAfterAction(ActionStatus status) {}

	@Override
	public void updateDimensions() {}

	@Override
	public FoundElement getElementFromPoint(Boolean syscomp, Double x, Double y) {
		return null;
	}

	@Override
	public FoundElement getElementFromRect(Boolean syscomp, Double x, Double y, Double w, Double h) {
		return null;
	}

	@Override
	public boolean switchWindow(ActionStatus status, int index, int tries, boolean refresh) {
		return true;
	}

	@Override
	public void closeWindow(ActionStatus status) {
	}

	@Override
	public Object executeScript(ActionStatus status, String script, Object... params) {
		return null;
	}

	@Override
	public void scroll(FoundElement element) {}

	@Override
	public void scroll(int value) {}

	@Override
	public void scroll(FoundElement element, int delta) {}

	@Override
	public void middleClick(ActionStatus status, MouseDirection position, TestElement element) {}

	@Override
	public void mouseMoveToElement(ActionStatus status, FoundElement foundElement, MouseDirection position, boolean desktopDragDrop, int offsetX, int offsetY) {}

	@Override
	public void sendTextData(ActionStatus status, TestElement element, ArrayList textActionList, int waitChar, ActionTestScript topScript) {}

	@Override
	public void clearText(ActionStatus status, TestElement testElement, MouseDirection md) {}

	@Override
	public void mouseClick(ActionStatus status, FoundElement element, MouseDirection position, int offsetX, int offsetY) {}

	@Override
	public void drag(ActionStatus status, FoundElement element, MouseDirection position, int offsetX, int offsetY, boolean offset) {}

	@Override
	public void keyDown(Keys key) {}

	@Override
	public void keyUp(Keys key) {}

	@Override
	public void drop(FoundElement element, MouseDirection md, boolean desktopDriver) {}
	
	@Override
	public void swipe(ActionStatus status, FoundElement foundElement, MouseDirection position, MouseDirection direction) {}

	@Override
	public void moveByOffset(int hDirection, int vDirection) {}

	@Override
	public void doubleClick() {}

	@Override
	public void rightClick() {}

	@Override
	public DialogBox switchToAlert() {
		return null;
	}

	@Override
	public boolean switchToDefaultContent(boolean dialog) {return true;}

	@Override
	public void switchToFrameId(String id) {}

	@Override
	public void buttonClick(ActionStatus status, String id) {}

	@Override
	public void tap(int count, FoundElement element) {}

	@Override
	public void press(int duration, ArrayList paths, FoundElement element) {}

	@Override
	public void windowState(ActionStatus status, Channel channel, String state) {}

	@Override
	public Object executeJavaScript(ActionStatus status, String script, TestElement element) {
		return null;
	}

	@Override
	public Object executeJavaScript(ActionStatus status, String script, boolean returnValue) {
		return null;
	}

	@Override
	public String getTitle() {
		return "";
	}

	@Override
	public String getTextData(FoundElement e) {//TODO implement text data
		return "";
	}

	//------------------------------------------------------------------------------------------------------------------------------------
	// init http client
	//------------------------------------------------------------------------------------------------------------------------------------

	private static Builder createHttpBuilder(int timeout, AtsCookieJar cookieJar, PrintStream logStream, String packageName, ClassLoader classLoader){

		final Path certsFolderPath = Paths.get("").toAbsolutePath().resolve(Project.SRC_FOLDER).resolve(Project.ASSETS_FOLDER).resolve(Project.CERTS_FOLDER);

		File certsFolder = certsFolderPath.toFile();
		if(!certsFolder.exists()) {
			final URL certsFolderUrl = classLoader.getResource(Project.ASSETS_FOLDER + "/" + Project.CERTS_FOLDER);
			if(certsFolderUrl != null) {
				certsFolder = new File(certsFolderUrl.getPath());
			}
		}

		File pfxFile = null;

		if(certsFolder.exists()) {
			File[] pfxFiles = certsFolder.listFiles((dir, name) -> name.toLowerCase().endsWith(".pfx"));
			if(pfxFiles.length > 0) {
				pfxFile = pfxFiles[0];
				logStream.println("Using pfx file -> " + pfxFile.getAbsolutePath());
			}
		}

		final Builder builder = new Builder();
		if(cookieJar != null) {
			builder.cookieJar(cookieJar);
		}

		builder.connectTimeout(timeout, TimeUnit.SECONDS)
		.writeTimeout(timeout, TimeUnit.SECONDS)
		.readTimeout(timeout, TimeUnit.SECONDS)
		.cache(null)
		.followRedirects(true)
		.followSslRedirects(true);

		final TrustManager [] trustManager = getTrustManager ();

		try {

			final SSLSocketFactory sslSocketFactory = getSslSocketFactory(pfxFile, trustManager, logStream);

			builder.sslSocketFactory (sslSocketFactory, (X509TrustManager)trustManager [0]);
			builder.hostnameVerifier(new HostnameVerifier() {
				@Override
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
		} catch (NoSuchAlgorithmException | KeyManagementException | CertificateException | IOException e) {
			e.printStackTrace(logStream);
		}

		return builder;
	}

	private static SSLSocketFactory getSslSocketFactory(File pfxFile, TrustManager [] trustManager, PrintStream errorStream) throws KeyManagementException, NoSuchAlgorithmException, CertificateException, IOException {
		final SSLContext sslContext = SSLContext.getInstance ("SSL");

		KeyManager[] keyManager = null;

		if(pfxFile != null && pfxFile.exists()) {

			final String password = "";
			final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");

			try {
				final KeyStore keyStore = KeyStore.getInstance("PKCS12");
				final InputStream keyInput = new FileInputStream(pfxFile);

				keyStore.load(keyInput, password.toCharArray());
				keyInput.close();

				keyManagerFactory.init(keyStore, password.toCharArray());

				keyManager = keyManagerFactory.getKeyManagers();

			} catch (KeyStoreException | UnrecoverableKeyException e) {
				e.printStackTrace(errorStream);
			}

		}else {
			errorStream.println("No pfx files found in the project");
		}

		sslContext.init (keyManager, trustManager, new SecureRandom ());
		return sslContext.getSocketFactory();
	}

	private static TrustManager[] getTrustManager () {
		final X509TrustManager manager = new X509TrustManager () {

			@Override
			public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			}

			@Override
			public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			}

			@Override
			public X509Certificate[] getAcceptedIssuers() {
				return new java.security.cert.X509Certificate[]{};
			}
		};

		return new TrustManager [] { manager };
	}

	@Override
	protected void setPosition(Point pt) {
	}

	@Override
	protected void setSize(Dimension dim) {
	}

	@Override
	public List loadSelectOptions(TestElement element) {
		return Collections.emptyList();
	}

	@Override
	public int getNumWindows() {
		return 0;
	}

	@Override
	public String getUrl() {
		return executor.getCurrentUrl();
	}

	@Override
	public Rectangle getBoundRect(TestElement testElement) {
		return null;
	}

	@Override
	public void mouseMoveToElement(FoundElement element) {
	}

	@Override
	public String getCookies() {
		return "";
	}

	@Override
	public void quit() {
	}

	@Override
	public String getHeaders(ActionStatus status) {
		return "";
	}

	@Override
	public String getSelectedText(TestElement e) {
		return ""; //NOT implemented
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy