Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now you can use Ruby to configure you config.yml like in Rails fixtures.

Example:

host: <%=`hostname`.gsub(/\n/, '')%>
port_start: 4000
port_end: 4001
max_browser_duration: 1440


git-svn-id: https://svn.openqa.org/svn/selenium-on-rails/selenium-on-rails@122 9274398c-e119-0410-8437-aa71ef7847aa
  • Loading branch information...
commit c037be2bbef0beeae44e1003e138f41c07e50a2d 1 parent bdba02d
eric@8thlight.com authored
View
2  init.rb
@@ -1,5 +1,5 @@
require 'selenium_on_rails_config'
-envs = SeleniumOnRailsConfig.new.get :environments
+envs = SeleniumOnRailsConfig.get :environments
if envs.include? RAILS_ENV
#initialize the plugin
View
6 lib/controllers/selenium_controller.rb
@@ -5,7 +5,7 @@ class SeleniumController < ActionController::Base
include SeleniumOnRails::Renderer
def initialize
- @config = SeleniumOnRailsConfig.new
+ @result_dir = SeleniumOnRailsConfig.get(:result_dir)
end
def setup
@@ -65,9 +65,9 @@ def record
end
def record_table
- return nil unless result_dir = @config.get(:result_dir)
+ return nil unless @result_dir
- cur_result_dir = File.join(result_dir, (params[:logFile] || "default").sub(/\.yml$/, ''))
+ cur_result_dir = File.join(@result_dir, (params[:logFile] || "default").sub(/\.yml$/, ''))
FileUtils.mkdir_p(cur_result_dir)
File.open("#{cur_result_dir}/index.html", "wb") do |f|
f.write <<EOS
View
30 lib/controllers/switch_environment_controller.rb
@@ -1,16 +1,16 @@
-class SwitchEnvironmentController < ActionController::Base
- def index
- readme_path = File.expand_path File.join(File.dirname(__FILE__), '..', 'README')
- render :status => 500, :locals => {:readme_path => readme_path }, :inline => <<END
-<p>
- Selenium on Rails is only activated for <%= SeleniumOnRailsConfig.new.get(:environments).join ', ' %>
- environment<%= SeleniumOnRailsConfig.get(:environments).size > 1 ? 's' : '' %> (you're running
- <%= RAILS_ENV %>).
-</p>
-<p>
- Start your server in a different environment or see <tt><%= readme_path %></tt>
- for information regarding how to change this behavior.
-</p>
-END
- end
+class SwitchEnvironmentController < ActionController::Base
+ def index
+ readme_path = File.expand_path File.join(File.dirname(__FILE__), '..', 'README')
+ render :status => 500, :locals => {:readme_path => readme_path }, :inline => <<END
+<p>
+ Selenium on Rails is only activated for <%= SeleniumOnRailsConfig.get(:environments).join ', ' %>
+ environment<%= SeleniumOnRailsConfig.get(:environments).size > 1 ? 's' : '' %> (you're running
+ <%= RAILS_ENV %>).
+</p>
+<p>
+ Start your server in a different environment or see <tt><%= readme_path %></tt>
+ for information regarding how to change this behavior.
+</p>
+END
+ end
end
View
4 lib/selenium_on_rails/acceptance_test_runner.rb
@@ -5,8 +5,8 @@
require 'tempfile'
-def c(var, default = nil) SeleniumOnRailsConfig.new.get var, default end
-def c_b(var, default = nil) SeleniumOnRailsConfig.new.get(var, default) { yield } end
+def c(var, default = nil) SeleniumOnRailsConfig.get var, default end
+def c_b(var, default = nil) SeleniumOnRailsConfig.get(var, default) { yield } end
BROWSERS = c :browsers, {}
REUSE_EXISTING_SERVER = c :reuse_existing_server, true
View
2  lib/selenium_on_rails/paths.rb
@@ -42,7 +42,7 @@ def skip_file? file
private ###############################################
def find_selenium_path
- sel_dirs = @config.get :selenium_path do
+ sel_dirs = SeleniumOnRailsConfig.get :selenium_path do
File.expand_path(File.dirname(__FILE__) + '/../../selenium-core')
end
View
53 lib/selenium_on_rails_config.rb
@@ -1,26 +1,27 @@
-require 'yaml'
-
-class SeleniumOnRailsConfig
- attr_accessor :configs
-
- def initialize
- @defaults = {:environments => ['test']}
- initialize_configs
- end
-
- def get var, default = nil
- value = @configs[var.to_s]
- value ||= @defaults[var]
- value ||= default
- value ||= yield if block_given?
- value
- end
-
- def initialize_configs
- @configs = {}
- files = [File.expand_path(File.dirname(__FILE__) + '/../config.yml')]
- files << File.join(RAILS_ROOT, 'config', 'selenium.yml')
- files.each { |file| @configs = YAML.load_file(file) if File.exist?(file) }
- end
-
-end
+require 'yaml'
+
+class SeleniumOnRailsConfig
+ @@defaults = {:environments => ['test']}
+ def self.get var, default = nil
+ value = configs[var.to_s]
+ value ||= @@defaults[var]
+ value ||= default
+ value ||= yield if block_given?
+ value
+ end
+
+ private
+ def self.configs
+ @@configs ||= nil
+ unless @@configs
+ files = [File.expand_path(File.dirname(__FILE__) + '/../config.yml')]
+ files << File.join(RAILS_ROOT, 'config', 'selenium.yml')
+ files.each do |file|
+ @@configs = YAML.load(ERB.new(IO.read(file)).result) if File.exist?(file)
+ end
+ @@configs ||= {}
+ end
+ @@configs
+ end
+
+end
View
27 test/fixtures/selenium.yml
@@ -0,0 +1,27 @@
+# Move this file to your rails apps config directory and rename it to selenium.yml in order to configure the plugin
+
+#
+# General settings
+#
+
+environments:
+ - test_cache
+# - development # Uncomment this line to enable in development environment. N.B. your development database will likely be altered/destroyed/abducted
+
+#selenium_path: 'c:\selenium' #path to selenium installation. only needed if you for some reason don't want to use the bundled version of selenium core
+
+#
+# rake test:acceptance settings
+#
+
+browsers:
+ firefox: 'script/openfirefox'
+ # safari: '/Applications/Safari.app/Contents/MacOS/Safari'
+
+port_start: 4000
+port_end: 4001
+#base_url_path: '/'
+#max_browser_duration: 120
+#multi_window: false
+
+#result_dir: 'c:\result' # the directory where the results will be stored after a test:acceptance run
View
2  test/selenium_controller_test.rb
@@ -26,7 +26,7 @@ def teardown
end
def test_record_with_result
- @controller.config.configs["result_dir"] = @result_dir
+ @controller.instance_variable_set(:@result_dir, @result_dir)
post :record, :suite => @suite, "testTable.1" => "<table></table>", "testTable.2" => "<table></table>"
View
49 test/selenium_on_rails_config_test.rb
@@ -1,21 +1,46 @@
require File.dirname(__FILE__) + '/test_helper'
require 'mocha'
+class SeleniumOnRailsConfig
+ def self.reset_config
+ @@configs = nil
+ end
+end
+
class SeleniumOnRailsConfigTest < Test::Unit::TestCase
- def test_config_file_in_config_directory
- File.stubs(:expand_path).returns("temporary")
- File.expects(:exist?).with("temporary").returns(false)
- File.expects(:exist?).with(File.join(RAILS_ROOT, 'config', 'selenium.yml')).returns(true)
- YAML.expects(:load_file).with(File.join(RAILS_ROOT, 'config', 'selenium.yml')).returns({:fake => "hash"})
-
- assert_equal({:fake => "hash"}, SeleniumOnRailsConfig.new.configs)
+ def setup
+ SeleniumOnRailsConfig.reset_config
+ @selenium_file = File.join(RAILS_ROOT, 'config', 'selenium.yml')
+ @config_file = File.expand_path(File.dirname(__FILE__) + '/../config.yml')
+ @selenium_content = File.read(File.dirname(__FILE__) + '/fixtures/selenium.yml')
+ @config_content = File.read(File.dirname(__FILE__) + '/fixtures/config.yml')
+ end
+
+ def test_get_when_config_yml_and_selenium_yaml_not_exists
+ File.expects(:exist?).with(@selenium_file).returns(false)
+ File.expects(:exist?).with(@config_file).returns(false)
+ IO.expects(:read).with(@config_file).never
+ IO.expects(:read).with(@selenium_file).never
+ assert_equal ["test"], SeleniumOnRailsConfig.get(:environments)
end
- def test_setting_config_manually
- config = SeleniumOnRailsConfig.new
- config.configs["test"] = "result"
-
- assert_equal("result", config.configs["test"])
+ def test_get_when_config_yml_not_exists_but_selenium_yaml_exists
+ File.expects(:exist?).with(@selenium_file).returns(true)
+ File.expects(:exist?).with(@config_file).returns(false)
+ IO.expects(:read).with(@config_file).never
+ IO.expects(:read).with(@selenium_file).returns(@selenium_content)
+ assert_equal ["test_cache"], SeleniumOnRailsConfig.get(:environments)
+ assert_equal({"firefox"=>"script/openfirefox"}, SeleniumOnRailsConfig.get(:browsers))
end
+
+ def test_get_when_config_yml_exists_but_selenium_yaml_not
+ File.expects(:exist?).with(@selenium_file).returns(false)
+ File.expects(:exist?).with(@config_file).returns(true)
+ IO.expects(:read).with(@config_file).returns(@config_content)
+ IO.expects(:read).with(@selenium_file).never
+ assert_equal ["test"], SeleniumOnRailsConfig.get(:environments)
+ assert_equal({"firefox"=>"script/openfirefox"}, SeleniumOnRailsConfig.get(:browsers))
+ end
+
end
View
5 test/switch_environment_controller_test.rb
@@ -10,11 +10,8 @@ def setup
end
def test_index
- SeleniumOnRailsConfig.expects(:new).returns(@config)
- @config.expects(:get).with(:environments).returns("hello dolly")
-
+ SeleniumOnRailsConfig.expects(:get).with(:environments).returns("hello dolly")
get :index
-
assert @response.body.include?('hello dolly')
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.