<commit_before>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require "selenium-webdriver"
require "socket"
require File.expand_path(File.dirname(__FILE__) + '/server')

SELENIUM_CONFIG = Setting.from_config("selenium") || {}
SERVER_IP = UDPSocket.open { |s| s.connect('8.8.8.8', 1); s.addr.last }
SERVER_PORT = SELENIUM_CONFIG[:app_port] || 3002
APP_HOST = "#{SERVER_IP}:#{SERVER_PORT}"
SECONDS_UNTIL_COUNTDOWN = 5
SECONDS_UNTIL_GIVING_UP = 60
MAX_SERVER_START_TIME = 30

module SeleniumTestsHelperMethods
  def setup_selenium
    if SELENIUM_CONFIG.empty?
      driver = Selenium::WebDriver.for :firefox
    else
      driver = Selenium::WebDriver.for(
        :remote, 
        :url => 'http://' + (SELENIUM_CONFIG[:host_and_port] || "localhost:4444") + '/wd/hub', 
        :desired_capabilities => (SELENIUM_CONFIG[:browser].try(:to_sym) || :firefox)
      )
    end
    driver.get(app_host)
    driver
  end
  
  def app_host
    "http://#{APP_HOST}"
  end

  def self.start_in_process_webrick_server
    HostUrl.default_host = APP_HOST
    HostUrl.file_host = APP_HOST
    server = SpecFriendlyWEBrickServer
    app = Rack::Builder.new do
      use Rails::Rack::Debugger unless Rails.env.test?
      map '/' do
        use Rails::Rack::Static
        run ActionController::Dispatcher.new
      end
    end.to_app
    server.run(app, :Port => SERVER_PORT, :AccessLog => [])
    shutdown = lambda do
      server.shutdown
      HostUrl.default_host = nil
      HostUrl.file_host = nil
    end
    at_exit { shutdown.call }
    return shutdown
  end
  
  def self.start_forked_webrick_server
    domain_conf_path = File.expand_path(File.dirname(__FILE__) + '/../../config/domain.yml')<commit_msg>support setting a profile for running firefox selenium tests<commit_after>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require "selenium-webdriver"
require "socket"
require File.expand_path(File.dirname(__FILE__) + '/server')

SELENIUM_CONFIG = Setting.from_config("selenium") || {}
SERVER_IP = UDPSocket.open { |s| s.connect('8.8.8.8', 1); s.addr.last }
SERVER_PORT = SELENIUM_CONFIG[:app_port] || 3002
APP_HOST = "#{SERVER_IP}:#{SERVER_PORT}"
SECONDS_UNTIL_COUNTDOWN = 5
SECONDS_UNTIL_GIVING_UP = 60
MAX_SERVER_START_TIME = 30

module SeleniumTestsHelperMethods
  def setup_selenium
    if !SELENIUM_CONFIG[:host_and_port]
      browser = SELENIUM_CONFIG[:browser].try(:to_sym) || :firefox
      options = {}
      if SELENIUM_CONFIG[:firefox_profile].present? && browser == :firefox
        options[:profile] = Selenium::WebDriver::Firefox::Profile.from_name(SELENIUM_CONFIG[:firefox_profile])
      end
      driver = Selenium::WebDriver.for(browser, options)
    else
      driver = Selenium::WebDriver.for(
        :remote, 
        :url => 'http://' + (SELENIUM_CONFIG[:host_and_port] || "localhost:4444") + '/wd/hub', 
        :desired_capabilities => (SELENIUM_CONFIG[:browser].try(:to_sym) || :firefox)
      )
    end
    driver.get(app_host)
    driver.manage.timeouts.implicit_wait = 3
    driver
  end
  
  def app_host
    "http://#{APP_HOST}"
  end

  def self.start_in_process_webrick_server
    HostUrl.default_host = APP_HOST
    HostUrl.file_host = APP_HOST
    server = SpecFriendlyWEBrickServer
    app = Rack::Builder.new do
      use Rails::Rack::Debugger unless Rails.env.test?
      map '/' do
        use Rails::Rack::Static
        run ActionController::Dispatcher.new
      end
    end.to_app
    server.run(app, :Port => SERVER_PORT, :AccessLog => [])
    shutdown = lambda do
      server.shutdown
      HostUrl.default_host = nil
      HostUrl.file_host = nil
    end
    at_exit { shutdown.call }
    return shutdown
  end
  
  def self.start_forked_webrick_server
    domain_conf_path = File.expand_path(File.dirname(__FILE__) + '/../../config/domain.yml')
{'fraction_non_alphanumeric': 0.10812898242904036, 'fraction_numerical': 0.009075111025294458, 'mean_word_length': 3.6793134598012647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '7185663', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1653, 'n_words': 436}