<commit_before>/*-
 * #%L
 * Bobcat
 * %%
 * Copyright (C) 2016 Cognifide Ltd.
 * %%
 * 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.
 * #L%
 */
package com.cognifide.qa.bb.provider.selenium.webdriver;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cognifide.qa.bb.constants.ConfigKeys;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobilePlatform;

/**
 * Enum represent available web driver types.
 */
public enum WebDriverType {
  FIREFOX {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return new FirefoxDriver(capabilities);
    }
  },
  MARIONETTE {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      DesiredCapabilities caps = DesiredCapabilities.firefox();
      caps.setCapability("marionette", true);
      return new FirefoxDriver(capabilities);
    }
  },
  CHROME {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return new ChromeDriver(capabilities);
    }
  },
  IE {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return new InternetExplorerDriver(capabilities);
    }
  },
  SAFARI {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return new SafariDriver(capabilities);
    }
  },
  HTML {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return new HtmlUnitDriver(capabilities);
    }
  },
  GHOST {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return new PhantomJSDriver();
    }
  },
  APPIUM {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return createMobileDriver(capabilities, properties);
    }

    private WebDriver createMobileDriver(Capabilities capabilities, Properties properties) {
      final URL url;
      try {
        url = new URL(properties.getProperty(ConfigKeys.WEBDRIVER_APPIUM_URL));
      } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
      }

      final String platform = properties.getProperty(ConfigKeys.WEBDRIVER_CAP_PLATFORM_NAME);
      switch (platform) {
        case MobilePlatform.ANDROID:
          return new AndroidDriver(url, capabilities);

        case MobilePlatform.IOS:
          return new IOSDriver(url, capabilities);

        default:
          throw new IllegalArgumentException(String.format(
              "webdriver.cap.platformName not configured correctly. Set it either to %s or %s",
              MobilePlatform.ANDROID, MobilePlatform.IOS));
      }
    }
  },
  REMOTE {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      final URL url;
      try {
        url = new URL(properties.getProperty(ConfigKeys.WEBDRIVER_URL));
      } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
      }
      WebDriver driver = new RemoteWebDriver(url, capabilities);
      if (Boolean.valueOf(properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE))) {
        driver.get(properties.getProperty(ConfigKeys.BASE_URL));
        Cookie cookie = new Cookie(
            properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_NAME),
            properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_VALUE),
            "." + properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_DOMAIN), "/",
            DateUtils.addMonths(new Date(), 1));
        driver.manage().addCookie(cookie);
      }
      return driver;
    }
  },
  XVFB {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      final File firefoxPath = new File(properties.getProperty(ConfigKeys.WEBDRIVER_FIREFOX_BIN));
      FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
      firefoxBinary.setEnvironmentProperty("DISPLAY",
          properties.getProperty(ConfigKeys.WEBDRIVER_XVFB_ID));
      return new FirefoxDriver(firefoxBinary, null, capabilities);
    }
  };

  private static final Logger LOG = LoggerFactory.getLogger(WebDriverType.class);

  public abstract WebDriver create(Capabilities capabilities, Properties properties);

  /**
   * Returns WebDriverType for name
   *
   * @param typeName name of web driver type
   * @return WebDriverType
   */
  public static WebDriverType get(String typeName) {
    WebDriverType webDriverType = WebDriverType.HTML;
    if (StringUtils.isNotBlank(typeName)) {
      try {
        webDriverType = WebDriverType.valueOf(typeName.toUpperCase());
      } catch (IllegalArgumentException e) {
        LOG.error("Illegal type: " + typeName, e);
      }
    }
    return webDriverType;
  }
}
<commit_msg>75 | webdriver proxy cookie for other webdriver types<commit_after>/*-
 * #%L
 * Bobcat
 * %%
 * Copyright (C) 2016 Cognifide Ltd.
 * %%
 * 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.
 * #L%
 */
package com.cognifide.qa.bb.provider.selenium.webdriver;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cognifide.qa.bb.constants.ConfigKeys;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobilePlatform;

/**
 * Enum represent available web driver types.
 */
public enum WebDriverType {
  FIREFOX {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return handleWebDriverProxyCookie(properties, new FirefoxDriver(capabilities));
    }
  },
  MARIONETTE {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      DesiredCapabilities caps = DesiredCapabilities.firefox();
      caps.setCapability("marionette", true);
      return handleWebDriverProxyCookie(properties, new FirefoxDriver(capabilities));
    }
  },
  CHROME {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return handleWebDriverProxyCookie(properties, new ChromeDriver(capabilities));
    }
  },
  IE {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return handleWebDriverProxyCookie(properties, new InternetExplorerDriver(capabilities));
    }
  },
  SAFARI {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return handleWebDriverProxyCookie(properties, new SafariDriver(capabilities));
    }
  },
  HTML {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return handleWebDriverProxyCookie(properties, new HtmlUnitDriver(capabilities));
    }
  },
  GHOST {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return handleWebDriverProxyCookie(properties, new PhantomJSDriver());
    }
  },
  APPIUM {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      return handleWebDriverProxyCookie(properties, createMobileDriver(capabilities, properties));
    }

    private WebDriver createMobileDriver(Capabilities capabilities, Properties properties) {
      final URL url;
      try {
        url = new URL(properties.getProperty(ConfigKeys.WEBDRIVER_APPIUM_URL));
      } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
      }

      final String platform = properties.getProperty(ConfigKeys.WEBDRIVER_CAP_PLATFORM_NAME);
      switch (platform) {
        case MobilePlatform.ANDROID:
          return new AndroidDriver(url, capabilities);

        case MobilePlatform.IOS:
          return new IOSDriver(url, capabilities);

        default:
          throw new IllegalArgumentException(String.format(
              "webdriver.cap.platformName not configured correctly. Set it either to %s or %s",
              MobilePlatform.ANDROID, MobilePlatform.IOS));
      }
    }
  },
  REMOTE {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      final URL url;
      try {
        url = new URL(properties.getProperty(ConfigKeys.WEBDRIVER_URL));
      } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
      }
      WebDriver driver = new RemoteWebDriver(url, capabilities);
      return handleWebDriverProxyCookie(properties, driver);
    }
  },
  XVFB {
    @Override
    public WebDriver create(Capabilities capabilities, Properties properties) {
      final File firefoxPath = new File(properties.getProperty(ConfigKeys.WEBDRIVER_FIREFOX_BIN));
      FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
      firefoxBinary.setEnvironmentProperty("DISPLAY",
          properties.getProperty(ConfigKeys.WEBDRIVER_XVFB_ID));
      return handleWebDriverProxyCookie(properties,
          new FirefoxDriver(firefoxBinary, null, capabilities));
    }
  };

  private static WebDriver handleWebDriverProxyCookie(Properties properties, WebDriver driver) {
    if (Boolean.valueOf(properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE))) {
      driver.get(properties.getProperty(ConfigKeys.BASE_URL));
      Cookie cookie = new Cookie(
          properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_NAME),
          properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_VALUE),
          "." + properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_DOMAIN), "/",
          DateUtils.addMonths(new Date(), 1));
      driver.manage().addCookie(cookie);
    }
    return driver;
  }

  private static final Logger LOG = LoggerFactory.getLogger(WebDriverType.class);

  public abstract WebDriver create(Capabilities capabilities, Properties properties);

  /**
   * Returns WebDriverType for name
   *
   * @param typeName name of web driver type
   * @return WebDriverType
   */
  public static WebDriverType get(String typeName) {
    WebDriverType webDriverType = WebDriverType.HTML;
    if (StringUtils.isNotBlank(typeName)) {
      try {
        webDriverType = WebDriverType.valueOf(typeName.toUpperCase());
      } catch (IllegalArgumentException e) {
        LOG.error("Illegal type: " + typeName, e);
      }
    }
    return webDriverType;
  }
}

{'fraction_non_alphanumeric': 0.08714362560516406, 'fraction_numerical': 0.002151694459386767, 'mean_word_length': 3.8451228592702904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2020978', 'n_tokens_mistral': 3679, 'n_tokens_neox': 3605, 'n_words': 846}