lib/js_test_driver/config.rb in js-test-driver-rails-0.4.3 vs lib/js_test_driver/config.rb in js-test-driver-rails-0.5.0.pre1
- old
+ new
@@ -7,11 +7,12 @@
#
# The configuration is very basic, however the fact that it is done in Ruby, gives the
# user a significant amount of freedom in terms of what is and is not loaded, and so on
class Config
- def initialize(attributes = {})
+ def initialize(runtime_config, attributes = {})
+ @runtime_config = runtime_config
self.attributes = attributes
end
# Adds a file to be loaded, the path *must* be relative to the root_dir (which is the current dir by default)
#
@@ -35,10 +36,20 @@
browsers.each do |browser|
self.browsers << browser
end
end
+ # This allows you to control browsers on remote machines with Selenium
+ # To be able to use this, the remote server has to be running selenium-server
+ #
+ # Available options are: host and browser (as in WebDriver::Remote::Capabilities)
+ def remote_browser(host, opts = {})
+ remote_browser = JsTestDriver::RemoteBrowser.new(host, opts)
+ remote_browsers << remote_browser
+ browser remote_browser_file_name(remote_browser.name)
+ end
+
# Defines a HTML fixture directory
#
# the first argument is the directory to scan for html fixtures
# you can pass also :name and :namespace arguments to define the name and the namespace of the fixture
#
@@ -71,19 +82,34 @@
'jar' => File.join(vendor_directory, 'coverage.jar'),
'module' => 'com.google.jstestdriver.coverage.CoverageModule'
}
end
+ # Adds a proxy matcher to configuration. This can be used for integration testing.
+ def proxy(pattern)
+ return Proxy.new(pattern, proxies)
+ end
+
def measure_coverage?
!!@measure_coverage
end
# Plugins to include in the config
def plugins
@plugins ||= []
end
+ def guess_local_ip
+ orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
+ UDPSocket.open do |s|
+ s.connect '64.233.187.99', 1
+ return s.addr.last
+ end
+ ensure
+ Socket.do_not_reverse_lookup = orig
+ end
+
# config variable which has a regular setter,
# but also can be set by calling the "getter" with an argument
# and if called without an argument the getter will return the passed block
#
# Ex.
@@ -130,63 +156,93 @@
def html_fixtures
@html_fixtures ||= []
end
+ def proxies
+ @proxies ||= []
+ end
+
attr_writer :browsers
def browsers
@browsers ||= []
end
+ attr_writer :remote_browsers
+
+ def remote_browsers
+ @remote_browsers ||= []
+ end
+
def to_s
hash = {'server' => server, 'basepath' => base_path}
hash['load'] = loaded_files unless loaded_files.empty?
hash['exclude'] = map_paths(excluded_files) unless excluded_files.empty?
hash['plugin'] = plugins unless plugins.empty?
+ hash['proxy'] = proxies unless proxies.empty?
return hash.to_yaml
end
- def self.parse(string)
- config = new
- config.instance_eval(string)
- return config
+ # this is where the config files are saved (ex. RAILS_ROOT/.js_test_driver)
+ def config_dir
+ runtime_config.generated_files_dir
end
- attr_writer :config_dir
+ # this is where the config files are saved (ex. RAILS_ROOT/.js_test_driver/fixtures)
+ def fixture_dir
+ runtime_config.fixture_dir
+ end
- # this is where the config files are saved (ex. RAILS_ROOT/.js_test_driver)
- def config_dir
- @config_dir ||= File.expand_path(".")
+ def save
+ File.open(runtime_config.config_yml_path, "w+") { |f| f.puts self.to_s }
+ save_fixtures
+ save_remote_browsers
+ return self
end
+ private
+
+ attr_reader :runtime_config
+
def save_fixtures
html_fixtures.each do |fixture|
path = fixture_file_name(fixture)
- FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w+") do |f|
f.puts fixture.to_s
end
end
end
- private
+ def save_remote_browsers
+ remote_browsers.each do |browser|
+ path = remote_browser_file_name(browser.name)
+ File.open(path, "w+") do |f|
+ f.puts browser.to_s
+ end
+ File.chmod(0755, path)
+ end
+ end
+ def fixture_file_name(fixture)
+ File.join(fixture_dir, fixture.namespace, "#{fixture.name}.js")
+ end
+
+ def remote_browser_file_name(name)
+ File.join(runtime_config.remote_browsers_dir, name)
+ end
+
def vendor_directory
this_directory = File.dirname(__FILE__)
return File.expand_path(File.join('..', '..', 'vendor'), this_directory)
end
def expand_globs(paths)
with_expanded_paths = paths.map{|path| File.expand_path(path)}
return with_expanded_paths.map{|path| path.include?('*') ? Dir[path].sort : path}.flatten
end
- def fixture_file_name(fixture)
- File.expand_path(File.join(config_dir, "fixtures", fixture.namespace, "#{fixture.name}.js"))
- end
-
def loaded_files
files = included_files + html_fixtures.collect { |fixture| fixture_file_name(fixture) }
map_paths(files)
end
@@ -200,9 +256,22 @@
end
def attributes=(values)
values.each do |attr, value|
self.send("#{attr}=", value)
+ end
+ end
+
+ class Proxy
+ attr_reader :pattern, :proxies
+
+ def initialize(pattern, proxies)
+ @pattern = pattern
+ @proxies = proxies
+ end
+
+ def to(server)
+ self.proxies << {'matcher' => pattern, 'server' => server}
end
end
end
end