module Support
  module Browser
    # Module containing constants for specific browser configurations. If they get too large, then
    # move them to separate classes (like firefox profiles).
    module Options
      # Chrome capabilities are basically preferences.
      # rubocop:disable Style/MutableConstant
      CHROME_CAPABILITIES = {
        platform: :any,
        javascript_enabled: true,
        css_selectors_enabled: true,
        takes_screenshot: false,
        native_events: false,
        rotatable: false,
        firefox_profile: nil,
        proxy: nil,
        trustAllSSLCertificates: true,
        acceptInsecureCerts: true,
        acceptSslCerts: true,
        "chromeOptions" => {
          "prefs" => {
            # disable browser geolocation
            "profile.default_content_setting_values.geolocation": 2,
            # disable password manager popup
            "credentials_enable_service": false,
            # enables flash
            "profile.default_content_setting_values.plugins": 1,
            "profile.content_settings.plugin_whitelist.adobe-flash-player": 1,
            "profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player": 1
          }
        }
      }
      # rubocop:enable Style/MutableConstant

      # Trying performance improvement suggestion in http://stackoverflow.com/a/21506086
      # require 'selenium/webdriver/remote/http/persistent'
      # client = Selenium::WebDriver::Remote::Http::Persistent.new
      # resulted in suite runtime increasing by a lot and we are seeing the following error in Jeeves03:
      # too many connection resets (due to Net::ReadTimeout - Net::ReadTimeout)
      #   after 9311 requests on 259683400, last used 180.038384 seconds ago
      #   (Net::HTTP::Persistent::Error)

      # Also preferences, but separated for some unknown reason.
      def self.chrome_switches
        options = [
          # disable popup blocking so generate_checks.feature can run
          "disable-popup-blocking",

          # Fix for error message in chromedriver with Chrome v35
          # "You are using an unsupported command-line flag: --ignore-certificate-errors.
          #  Stability and security will suffer."
          # https://code.google.com/p/chromedriver/issues/detail?id=799
          "--ignore-certificate-errors",
          "--allow-insecure-localhost",
          "--allow-running-insecure-content",
          "--disable-web-security",
          # Gets rid of "Chrome is being controlled by automated test software" infobar
          "disable-infobars",

          # rubocop:disable Metrics/LineLength
          "--unsafely-treat-insecure-origin-as-secure=https://frontend:8080,https://localhost:6005,https://localhost:8080",
          # rubocop:enable Metrics/LineLength

          # Enables flash for Chrome 69+ for CUCUMBER_PROFILE=default
          # https://stackoverflow.com/a/52254172/4851355
          "--disable-features=EnableEphemeralFlashPermission",
          "--verbose"
        ]

        options << "--headless" if MISC_SETTINGS[:headless]
        options
      end
    end
  end
end
