/*
 * Copyright 2015 www.seleniumtests.com
 * Licensed 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.seleniumtests.browserfactory;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.seleniumtests.driver.DriverConfig;
import com.seleniumtests.driver.DriverMode;

import com.seleniumtests.helper.FileUtility;
import com.seleniumtests.helper.OSUtility;

import com.seleniumtests.resources.WebDriverExternalResources;

public class ChromeCapabilitiesFactory implements ICapabilitiesFactory {

    public DesiredCapabilities createCapabilities(final DriverConfig webDriverConfig) {

        DesiredCapabilities capability = null;
        capability = DesiredCapabilities.chrome();
        capability.setBrowserName(DesiredCapabilities.chrome().getBrowserName());

        ChromeOptions options = new ChromeOptions();
        if (webDriverConfig.getUserAgentOverride() != null) {
            options.addArguments("--user-agent=" + webDriverConfig.getUserAgentOverride());
        }

        capability.setCapability(ChromeOptions.CAPABILITY, options);

        if (webDriverConfig.isEnableJavascript()) {
            capability.setJavascriptEnabled(true);
        } else {
            capability.setJavascriptEnabled(false);
        }

        capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

        if (webDriverConfig.getBrowserVersion() != null) {
            capability.setVersion(webDriverConfig.getBrowserVersion());
        }

        if (webDriverConfig.getWebPlatform() != null) {
            capability.setPlatform(webDriverConfig.getWebPlatform());
        }

        if (webDriverConfig.getProxyHost() != null) {
            Proxy proxy = webDriverConfig.getProxy();
            capability.setCapability(CapabilityType.PROXY, proxy);
        }

        if (webDriverConfig.getChromeBinPath() != null) {
            capability.setCapability("chrome.binary", webDriverConfig.getChromeBinPath());
        }

        // Set ChromeDriver for local mode
        if (webDriverConfig.getMode() == DriverMode.LOCAL) {
            String chromeDriverPath = webDriverConfig.getChromeDriverPath();
            if (chromeDriverPath == null) {
                try {
                    if (System.getenv("webdriver.chrome.driver") != null) {
                        System.out.println("get Chrome driver from property:"
                                + System.getenv("webdriver.chrome.driver"));
                        System.setProperty("webdriver.chrome.driver", System.getenv("webdriver.chrome.driver"));
                    } else {
                        handleExtractResources();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } else {
                System.setProperty("webdriver.chrome.driver", chromeDriverPath);
            }
        }

        return capability;
    }

    public void handleExtractResources() throws IOException {
        String dir = this.getClass().getResource("/").getPath();
        dir = FileUtility.decodePath(dir);

        if (!new File(dir).exists()) {
            System.out.println("extracting chrome resources in " + dir);
            FileUtility.extractJar(dir, WebDriverExternalResources.class);
        }

        if (!new File(dir + OSUtility.getSlash() + "chromedriver.exe").exists()) {
            FileUtility.extractJar(dir, WebDriverExternalResources.class);
        }

        if (OSUtility.isWindows()) {
            System.setProperty("webdriver.chrome.driver", dir + "\\chromedriver.exe");
        } else if (OSUtility.isMac()) {
            System.setProperty("webdriver.chrome.driver", dir + "/mac/chromedriver");
        } else {
            System.setProperty("webdriver.chrome.driver", dir + "/chromedriver");
            new File(dir + "/chromedriver").setExecutable(true);
        }
    }

}

@@ -15,10 +15,10 @@
 t 20
+2
 1
-5
  www
@@ -633,58 +633,8 @@
 y;%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0A%0A
 impo
@@ -913,161 +913,8 @@
 e;%0A%0A
-import com.seleniumtests.helper.FileUtility;%0Aimport com.seleniumtests.helper.OSUtility;%0A%0Aimport com.seleniumtests.resources.WebDriverExternalResources;%0A%0A
 publ
@@ -2612,1819 +2612,42 @@
     
-    String chromeDriverPath = webDriverConfig.getChromeDriverPath();%0A            if (chromeDriverPath == null) %7B%0A                try %7B%0A                    if (System.getenv(%22webdriver.chrome.driver%22) != null) %7B%0A                        System.out.println(%22get Chrome driver from property:%22%0A                                + System.getenv(%22webdriver.chrome.driver%22));%0A                        System.setProperty(%22webdriver.chrome.driver%22, System.getenv(%22webdriver.chrome.driver%22));%0A                    %7D else %7B%0A                        handleExtractResources();%0A                    %7D%0A                %7D catch (IOException ex) %7B%0A                    ex.printStackTrace();%0A                %7D%0A            %7D else %7B%0A                System.setProperty(%22webdriver.chrome.driver%22, chromeDriverPath);%0A            %7D%0A        %7D%0A%0A        return capability;%0A    %7D%0A%0A    public void handleExtractResources() throws IOException %7B%0A        String dir = this.getClass().getResource(%22/%22).getPath();%0A        dir = FileUtility.decodePath(dir);%0A%0A        if (!new File(dir).exists()) %7B%0A            System.out.println(%22extracting chrome resources in %22 + dir);%0A            FileUtility.extractJar(dir, WebDriverExternalResources.class);%0A        %7D%0A%0A        if (!new File(dir + OSUtility.getSlash() + %22chromedriver.exe%22).exists()) %7B%0A            FileUtility.extractJar(dir, WebDriverExternalResources.class);%0A        %7D%0A%0A        if (OSUtility.isWindows()) %7B%0A            System.setProperty(%22webdriver.chrome.driver%22, dir + %22%5C%5Cchromedriver.exe%22);%0A        %7D else if (OSUtility.isMac()) %7B%0A            System.setProperty(%22webdriver.chrome.driver%22, dir + %22/mac/chromedriver%22);%0A        %7D else %7B%0A            System.setProperty(%22webdriver.chrome.driver%22, dir + %22/chromedriver%22);%0A            new File(dir + %22/chromedriver%22).setExecutable(true);%0A        %7D%0A    %7D%0A
+%7D%0A%0A        return capability;%0A    %7D
 %0A%7D%0A

Use WebDriver Manager