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

org.catools.web.drivers.CChromeDriverBuilder Maven / Gradle / Ivy

package org.catools.web.drivers;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.catools.common.collections.CHashMap;
import org.catools.common.collections.CList;
import org.catools.common.collections.interfaces.CMap;
import org.catools.common.config.CPathConfigs;
import org.catools.common.io.CFile;
import org.catools.common.text.CStringUtil;
import org.catools.web.config.CChromeConfigs;
import org.catools.web.config.CWebDriverManagerConfigs;
import org.catools.web.utils.CWebDriverUtil;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import static org.catools.web.config.CGridConfigs.getHubURL;
import static org.catools.web.config.CGridConfigs.isUseRemoteDriver;

public class CChromeDriverBuilder implements CDriverBuilder {
    private CMap prefs = new CHashMap<>();
    private CList> plugins = new CList<>();
    private ChromeOptions options = new ChromeOptions();
    private Rectangle windowsSize = null;

    public CChromeDriverBuilder() {
        if (CStringUtil.isNotBlank(CChromeConfigs.getBinaryPath())) {
            setBinary(CChromeConfigs.getBinaryPath());
        }
        addArguments(CChromeConfigs.getDefaultArguments());
        addPluginsToEnable(CChromeConfigs.getPluginsToEnable());
        addPluginsToDisable(CChromeConfigs.getPluginsToDisable());
        setPageLoadStrategy(CChromeConfigs.getPageLoadStrategy());
        setDownloadFolder(CPathConfigs.getTmpDownloadFolder());
        setOpenPdfInNewTab(true);

        prefs.put("plugins.plugins_list", plugins);

        if (CStringUtil.isNotBlank(CChromeConfigs.getChromeMobileEmulationDeviceName())) {
            setDeviceEmulation(CChromeConfigs.getChromeMobileEmulationDeviceName());
        }

        setHeadless(CChromeConfigs.isInHeadLessMode());
    }

    @Override
    public RemoteWebDriver build(Logger logger, Consumer beforeInit, Consumer afterInit) {
        buildChromeOptions();
        if (beforeInit != null) {
            beforeInit.accept(options);
        }

        if (CWebDriverManagerConfigs.isEnabled()) {
            WebDriverManager.chromedriver().setup();
        }

        RemoteWebDriver webDriver = isUseRemoteDriver() ? new RemoteWebDriver(getHubURL(), options) : new ChromeDriver(options);
        CWebDriverUtil.setDriverWindowsSize(webDriver, getWindowsSize());

        if (afterInit != null) {
            afterInit.accept(webDriver);
        }
        return webDriver;
    }

    @Override
    public CChromeDriverBuilder setBinary(String path) {
        options.setBinary(path);
        return this;
    }

    @Override
    public CChromeDriverBuilder addArguments(Iterable args) {
        for (String arg : args) {
            options.addArguments(arg);
        }
        return this;
    }

    @Override
    public CChromeDriverBuilder setHeadless(boolean value) {
        options.setHeadless(value);
        addArguments(CChromeConfigs.getHeadLessArguments());
        return this;
    }

    @Override
    public CChromeDriverBuilder setDownloadFolder(CFile tempDownloadFolder) {
        prefs.put("download.default_directory", tempDownloadFolder.getCanonicalPath());
        return this;
    }

    @Override
    public CChromeDriverBuilder setOpenPdfInNewTab(boolean value) {
        prefs.put("plugins.always_open_pdf_externally", value);
        return this;
    }

    @Override
    public CChromeDriverBuilder setWindowsSize(Rectangle windowsSize) {
        this.windowsSize = windowsSize;
        return this;
    }

    @Override
    public Rectangle getWindowsSize() {
        return windowsSize;
    }

    @Override
    public CChromeDriverBuilder setPageLoadStrategy(PageLoadStrategy pageLoadStrategy) {
        options.setCapability(CapabilityType.PAGE_LOAD_STRATEGY, pageLoadStrategy.toString());
        return this;
    }

    public CChromeDriverBuilder setDeviceEmulation(String deviceName) {
        Map mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceName", deviceName);
        options.setExperimentalOption("mobileEmulation", mobileEmulation);
        return this;
    }

    public CChromeDriverBuilder addPluginsToDisable(Iterable pluginsToDisable) {
        new CList<>(pluginsToDisable).forEach(p -> addPlugin(false, p));
        return this;
    }

    public CChromeDriverBuilder addPluginsToEnable(Iterable pluginsToEnable) {
        new CList<>(pluginsToEnable).forEach(p -> addPlugin(true, p));
        return this;
    }

    public CChromeDriverBuilder addPlugin(boolean flag, String pluginName) {
        CMap plugin = new CHashMap<>();
        plugin.put("enabled", flag);
        plugin.put("name", pluginName);
        plugins.add(plugin);
        return this;
    }

    private ChromeOptions buildChromeOptions() {
        setSystemProperties();
        options.setExperimentalOption("prefs", prefs);

        options.setAcceptInsecureCerts(true);

        options.setCapability(ChromeOptions.CAPABILITY, options);
        options.setCapability("download.directory_upgrade", false);
        options.setCapability("download.prompt_for_download", false);
        return options;
    }

    private void setSystemProperties() {
        if (!CWebDriverManagerConfigs.isEnabled() && CStringUtil.isNotBlank(CChromeConfigs.getDriverPath())) {
            String chromeDriverPath = CChromeConfigs.getDriverPath();
            System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy