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

org.jspringbot.keyword.selenium.DesiredCapabilitiesBean Maven / Gradle / Ivy

There is a newer version: 1.9
Show newest version
package org.jspringbot.keyword.selenium;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class DesiredCapabilitiesBean implements InitializingBean {
    private static final Logger LOGGER = Logger.getLogger(DesiredCapabilitiesBean.class);

    private DesiredCapabilities capabilities;

    private Proxy proxy;

    private File baseDir;

    private Map chromeOptions = new HashMap();

    private Map mobileEmulation;

    private LoggingPreferences logPrefs;

    private String chromeDriverVersion;

    private String ieDriverVersion;

    private boolean archAutodetect = false;

    private String archValue = "32";


    public DesiredCapabilitiesBean(DesiredCapabilities capabilities) {
        this.capabilities = capabilities;
    }

    public void setFirefoxProfile(FirefoxProfile profile) {
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    }

    public void setChromeDriverVersion(String chromeDriverVersion) {
        this.chromeDriverVersion = chromeDriverVersion;
    }

    public void setIeDriverVersion(String ieDriverVersion) {
        this.ieDriverVersion = ieDriverVersion;
    }

    public void setBaseDir(String baseStrDir) {
        if(StringUtils.isNotBlank(baseStrDir) && !StringUtils.equalsIgnoreCase(baseStrDir, "none")) {
            baseDir = new File(baseStrDir);
            if (!baseDir.isDirectory()) {
                baseDir.mkdirs();
            }
        }
    }

    public void setArchAutodetect(boolean archAutodetect) {
        this.archAutodetect = archAutodetect;
    }

    public void setArchValue(String archValue) {
        this.archValue = archValue;
    }

    public void setChromeDrivers(Map chromeDrivers) throws IOException {
        OsCheck.OSType osType = OsCheck.getOperatingSystemType();

        Resource chromeDriver = chromeDrivers.get(osType);

        if(chromeDriver == null) {
            throw new IllegalArgumentException("Unsupported OS " + osType.name());
        }

        File driverDir = createDriverDir();

        File downloadedFile = new File(driverDir, chromeDriver.getFilename());

        if(!downloadedFile.isFile()) {
            LOGGER.info("Chrome driver version: " + chromeDriverVersion);
            LOGGER.info("Downloading driver: " + chromeDriver.getURL());
            IOUtils.copy(chromeDriver.getInputStream(), new FileOutputStream(downloadedFile));
        }

        LOGGER.info("Chrome driver file: " + downloadedFile.getAbsolutePath());

        File driver = unzip(new FileInputStream(downloadedFile), driverDir);
        driver.setExecutable(true);

        System.setProperty("webdriver.chrome.driver", driver.getAbsolutePath());
    }

    private File createDriverDir() {
        File driverDir;
        if(baseDir != null) {
            driverDir = baseDir;
        } else {
            String userHome = System.getProperty("user.home");
            driverDir = new File(userHome, "jspringbot");

            if(!driverDir.isDirectory()) {
                driverDir.mkdirs();
            }
        }
        return driverDir;
    }

    public void setIeDriver(Map resourceMap) throws IOException {
        File driverDir = createDriverDir();

        String arch = archValue;
        if(archAutodetect) {
            arch = System.getProperty("sun.arch.data.model");
        }

        Resource resource = resourceMap.get(arch);

        File downloadedFile = new File(driverDir, resource.getFilename());
        if(!downloadedFile.isFile()) {
            LOGGER.info("Internet driver version" + ieDriverVersion);
            LOGGER.info("Downloading driver: " + resource.getURL());
            IOUtils.copy(resource.getInputStream(), new FileOutputStream(downloadedFile));
        }

        LOGGER.info("IE driver file: " + downloadedFile.getAbsolutePath());

        File driver = unzip(new FileInputStream(downloadedFile), driverDir);
        driver.setExecutable(true);

        System.setProperty("webdriver.ie.driver", driver.getAbsolutePath());
    }

    public void setChromeOptions(Map chromeOptions) {
        this.chromeOptions = chromeOptions;
    }

    public void setChromeDeviceEmulation(String deviceEmulation) {
        if(StringUtils.isNotBlank(deviceEmulation) && !StringUtils.equalsIgnoreCase(deviceEmulation, "none")) {
            this.mobileEmulation = new HashMap();
            mobileEmulation.put("deviceName", deviceEmulation);
        }
    }

    public void setChromeBrowserLog(String level) {
        if(StringUtils.isNotBlank(level) && !StringUtils.equalsIgnoreCase(level, "none")) {
            if(logPrefs == null) {
                logPrefs = new LoggingPreferences();
            }

            logPrefs.enable(LogType.BROWSER, Level.parse(level));
        }
    }

    public void setChromePerformanceLog(String level) {
        if(StringUtils.isNotBlank(level) && !StringUtils.equalsIgnoreCase(level, "none")) {
            if(logPrefs == null) {
                logPrefs = new LoggingPreferences();
            }

            logPrefs.enable(LogType.PERFORMANCE, Level.parse(level));
        }
    }

    public static File unzip(InputStream in, File dir) throws IOException {
        ZipInputStream zin = null;
        byte[] buf = new byte[2048];

        File entryFile = null;
        try {
            zin = new ZipInputStream(in);

            ZipEntry entry;
            while((entry = zin.getNextEntry()) != null) {
                FileOutputStream out = null;
                entryFile = new File(dir, entry.getName());

                try {
                    out = new FileOutputStream(entryFile);
                    int len;
                    while((len = zin.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(zin);
        }

        return entryFile;
    }

    public void setChromeLogFile(String logFile) {
        if(StringUtils.isNotBlank(logFile) && !StringUtils.equalsIgnoreCase(logFile, "none")) {
            File file = new File(logFile);
            File dir = file.getParentFile();

            if(dir != null && !dir.isDirectory()) {
                dir.mkdirs();
            }

            System.setProperty("webdriver.chrome.logfile", file.getAbsolutePath());
        }
    }

    public void setBrowserName(String browserName) {
        if(!StringUtils.equalsIgnoreCase(browserName, "none")) {
            capabilities.setBrowserName(browserName);
        }
    }

    public void setVersion(String version){
        if(!StringUtils.equalsIgnoreCase(version, "none")) {
            capabilities.setVersion(version);
        }
    }

    public void setPlatform(String platform){
        if(!StringUtils.equalsIgnoreCase(platform, "none")) {
            capabilities.setPlatform(Platform.valueOf(platform));
        }
    }

    public void setProxy(String proxyHost) {
        if(!StringUtils.equalsIgnoreCase(proxyHost, "none")) {
            proxy = new Proxy();

            proxy.setFtpProxy(proxyHost)
                 .setHttpProxy(proxyHost)
                 .setSslProxy(proxyHost);

            capabilities.setCapability(CapabilityType.PROXY, proxy);
        }
    }

    public void setSslProxy(String proxyHost) {
        if(!StringUtils.equalsIgnoreCase(proxyHost, "none")) {
            proxy = new Proxy();
            proxy.setSslProxy(proxyHost);

            capabilities.setCapability(CapabilityType.PROXY, proxy);
        }
    }

    public void setFtpProxy(String proxyHost) {
        if(!StringUtils.equalsIgnoreCase(proxyHost, "none")) {
            proxy = new Proxy();
            proxy.setFtpProxy(proxyHost);

            capabilities.setCapability(CapabilityType.PROXY, proxy);
        }
    }

    public void setHttpProxy(String proxyHost) {
        if(!StringUtils.equalsIgnoreCase(proxyHost, "none")) {
            proxy = new Proxy();
            proxy.setHttpProxy(proxyHost);

            capabilities.setCapability(CapabilityType.PROXY, proxy);
        }
    }

    @SuppressWarnings("unchecked")
    public void setCapabilities(String properties) throws JSONException {
        if(!StringUtils.equalsIgnoreCase(properties, "none")) {
            JSONObject obj = new JSONObject(properties);

            Iterator itr = obj.keys();
            while(itr.hasNext()) {
                String key = itr.next();
                capabilities.setCapability(key, obj.getString(key));
            }
        }
    }

    public void afterPropertiesSet() throws Exception {
        if(MapUtils.isNotEmpty(mobileEmulation)) {
            chromeOptions.put("mobileEmulation", mobileEmulation);
        }
        if(MapUtils.isNotEmpty(chromeOptions)) {
            capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        }
        if(logPrefs != null) {
            capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy