js-test-driver-rails 0.4.3 → 0.5.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,225 +1,13 @@
1
1
  module JsTestDriver
2
- # The Runner class is used
3
- class Runner
4
-
5
- def initialize(attributes = {})
6
- self.attributes = attributes
7
- end
8
-
9
- # configuration, by default it's parsed from config_path
10
- attr_writer :config
11
-
12
- def config
13
- @config ||= parse_config
14
- end
15
-
16
- attr_writer :generated_files_dir
17
-
18
- # this is where the generated files will be saved, by default this is `pwd`/.js_test_driver
19
- #
20
- # the generated files are the config yml file and the fixture files
21
- def generated_files_dir
22
- @generated_files_dir || default_generated_files_dir
23
- end
24
-
25
- # this is the path of the config file, by default its `pwd`/config/js_test_driver.rb
26
- attr_writer :config_path
27
-
28
- def config_path
29
- @config_path ||= default_config_path
30
- end
31
-
32
- # this is the path to the js test driver jar file, by default it's stored relatively to this file
33
- attr_writer :jar_path
34
-
35
- def jar_path
36
- @jar_path ||= default_jar_path
37
- end
38
-
39
- # this is where the config yml file will be saved, by default its saved in the generated files
40
- # directory under the name jsTestDriver.conf
41
- attr_writer :tmp_path
42
-
43
- def config_yml_path
44
- @tmp_path ||= default_config_yml_path
45
- end
46
-
47
- # starts the server on the default port specified in the config file
48
- def start_server
49
- command = execute_jar
50
-
51
- add_start_server(command)
52
-
53
- command.run
54
- end
55
-
56
- # captures the browsers
57
- #
58
- # by default it will capture the browsers specified in the config,
59
- # but you can pass an argument like 'opera,chrome,firefox' to capture opera, chrome and firefox
60
- def capture_browsers(browsers = nil)
61
- browsers ||= ''
62
- browsers = browsers.split(',')
63
- browsers = config.browsers if browsers.empty?
64
-
65
- url = config.server + "/capture"
66
-
67
- browsers.each do |browser|
68
- spawn("#{browser} \'#{url}\'")
69
- end
70
- end
71
-
72
- # runs the tests specified by the argument
73
- #
74
- # by default it will run all the tests, but you can pass a string like:
75
- # 'TestCase' or 'TestCase.test'
76
- # to run either a single test case or a single test
77
- def run_tests(tests = nil)
78
- command = execute_jar
79
-
80
- add_with_config(command)
81
- add_run_tests(command, tests)
82
-
83
- command.run
84
- end
85
-
86
- def start_server_capture_and_run(tests = nil, browsers = nil, output_xml_path = nil, console = nil)
87
- command = execute_jar
88
-
89
- add_start_server(command)
90
- add_with_config(command)
91
- add_capture_browsers(command, browsers)
92
- add_run_tests(command, tests)
93
- add_output_directory(command, output_xml_path) if output_xml_path
94
- add_capture_console(command) if console
95
-
96
- result = command.run
97
-
98
- if config.measure_coverage? && output_xml_path
99
- generate_html_coverage_report(output_xml_path)
100
- end
101
-
102
- return result
103
- end
104
-
105
- def add_start_server(command)
106
- command.option('--port', config.port)
107
- end
108
-
109
- def add_run_tests(command, tests)
110
- command.option('--tests', tests || "all")
111
- end
112
-
113
- def add_capture_browsers(command, browsers)
114
- browsers ||= config.browsers.join(',')
115
- raise ArgumentError.new("No browsers defined!") if browsers == ""
116
- command.option('--browser', browsers)
117
- end
118
-
119
- def add_output_directory(command, path)
120
- path = File.expand_path(path)
121
- FileUtils.mkdir_p(path) unless File.exists?(path)
122
- command.option('--testOutput', path)
123
- end
124
-
125
- def add_capture_console(command)
126
- command.option('--captureConsole')
127
- end
128
-
129
- def add_with_config(command)
130
- save_config_file(config_yml_path)
131
- command.option('--config', config_yml_path)
132
- end
133
-
134
- def execute_jar
135
- Command.new('java').option('-jar', jar_path)
136
- end
137
-
138
- def generate_html_coverage_report(output_path)
139
- unless genhtml_installed?
140
- puts "Could not find genhtml. You must install lcov (sudo apt-get install lcov)"
141
- return
142
- end
143
-
144
- output_path = File.expand_path(output_path)
145
-
146
- config_file_name = File.basename(config_yml_path)
147
- coverage_file_name = config_file_name + '-coverage.dat'
148
- coverage_file_path = File.join(output_path, coverage_file_name)
149
- coverage_out_dir = File.join(output_path, 'coverage')
150
-
151
- system("genhtml -o #{coverage_out_dir} #{coverage_file_path}")
152
- end
153
-
154
- protected
155
2
 
156
- def genhtml_installed?
157
- !%x[which genhtml].strip.empty?
158
- end
159
-
160
- def parse_config
161
- source = ""
162
- if File.exist?(config_path)
163
- source = File.read(config_path)
164
- else
165
- warn("Could not find JS Test Driver config: '#{config_path}', assuming empty config file!")
166
- end
167
- config = JsTestDriver::Config.parse(source)
168
- config.config_dir = generated_files_dir
169
- return config
170
- end
171
-
172
- def default_config_path
173
- root = defined?(::Rails) ? ::Rails.root.to_s : '.'
174
- return File.expand_path(File.join(root, 'config', 'js_test_driver.rb'))
175
- end
176
-
177
- def default_jar_path
178
- current_dir = File.dirname(__FILE__)
179
- path = File.join(current_dir, '..', '..', 'vendor', 'js_test_driver.jar')
180
- return File.expand_path(path)
181
- end
182
-
183
- def default_config_yml_path
184
- return File.expand_path("jsTestDriver.conf", generated_files_dir)
185
- end
186
-
187
- def default_generated_files_dir
188
- return File.expand_path(".js_test_driver")
189
- end
190
-
191
- private
192
-
193
- def save_config_file(path)
194
- Dir.mkdir(generated_files_dir) unless File.exists?(generated_files_dir)
195
- File.open(path, "w+") { |f| f.puts config.to_s }
196
- config.save_fixtures
197
- end
3
+ class Runner
198
4
 
199
- def attributes=(values)
200
- values.each do |attr, value|
201
- self.send("#{attr}=", value)
202
- end
5
+ def run(command)
6
+ system(command.to_s)
203
7
  end
204
8
 
205
- class Command
206
- def initialize(executable)
207
- @command = "#{executable}"
208
- end
209
-
210
- def option(name, value = nil)
211
- value = "'#{value}'" if value && value =~ /\s/
212
- @command = [@command, name, value].compact.join(' ')
213
- self
214
- end
215
-
216
- def run
217
- system(self.to_s)
218
- end
219
-
220
- def to_s
221
- return @command
222
- end
9
+ def runbg(command)
10
+ spawn(command.to_s)
223
11
  end
224
12
 
225
13
  end
@@ -0,0 +1,155 @@
1
+ module JsTestDriver
2
+ # The RuntimeConfig class holds the various paths that the application
3
+ # uses, as well as the actual JsTestDriver configuration
4
+ class RuntimeConfig
5
+
6
+ def initialize(attributes = {})
7
+ self.attributes = attributes
8
+ end
9
+
10
+ attr_accessor :config_factory
11
+
12
+ # configuration, by default it's parsed from config_path
13
+ attr_writer :config
14
+
15
+ def config
16
+ @config ||= parse_config
17
+ end
18
+
19
+ attr_writer :generated_files_dir
20
+
21
+ # this is where the generated files will be saved, by default this is `pwd`/.js_test_driver
22
+ #
23
+ # the generated files are the config yml file and the fixture files
24
+ def generated_files_dir
25
+ dir(@generated_files_dir || default_generated_files_dir)
26
+ end
27
+
28
+ # this is the path of the config file, by default its `pwd`/config/js_test_driver.rb
29
+ attr_writer :config_path
30
+
31
+ def config_path
32
+ file(@config_path ||= default_config_path)
33
+ end
34
+
35
+ # this is the path to the js test driver jar file, by default it's stored relatively to this file
36
+ attr_writer :jar_path
37
+
38
+ def jar_path
39
+ file(@jar_path ||= default_jar_path)
40
+ end
41
+
42
+ # this is where the config yml file will be saved, by default its saved in the generated files
43
+ # directory under the name jsTestDriver.conf
44
+ attr_writer :tmp_path
45
+
46
+ def config_yml_path
47
+ file(@tmp_path ||= default_config_yml_path)
48
+ end
49
+
50
+ # This is the directory where the coverage HTML files will be saved
51
+ attr_writer :coverage_files_path
52
+
53
+ def coverage_files_path
54
+ dir(@coverage_files_path ||= default_coverage_files_path)
55
+ end
56
+
57
+ # This is the file in which js-test-driver saves coverage data
58
+ # it is not configurable
59
+ def coverage_data_file
60
+ file_name = File.basename(config_yml_path + '-coverage.dat')
61
+ return file(file_name, test_xml_data_path)
62
+ end
63
+
64
+ # This is where the XML files with test results will be saved
65
+ attr_writer :test_xml_data_path
66
+
67
+ def test_xml_data_path
68
+ dir(@test_xml_data_path ||= default_test_xml_data_path)
69
+ end
70
+
71
+ # This is where the fixtures will be saved
72
+ attr_writer :fixture_dir
73
+
74
+ def fixture_dir
75
+ dir(@fixture_dir ||= default_fixture_dir)
76
+ end
77
+
78
+ # This is where the remote browser scripts will be saved
79
+ attr_writer :remote_browsers_dir
80
+
81
+ def remote_browsers_dir
82
+ dir(@remote_browsers_dir ||= default_remote_browsers_dir)
83
+ end
84
+
85
+ protected
86
+
87
+ def parse_config
88
+ source = ""
89
+ if File.exist?(config_path)
90
+ source = File.read(config_path)
91
+ else
92
+ warn("Could not find JS Test Driver config: '#{config_path}', assuming empty config file!")
93
+ end
94
+ return config_factory.parse(source).save
95
+ end
96
+
97
+ def default_config_path
98
+ root = defined?(::Rails) ? ::Rails.root.to_s : '.'
99
+ return File.expand_path(File.join(root, 'config', 'js_test_driver.rb'))
100
+ end
101
+
102
+ def default_jar_path
103
+ current_dir = File.dirname(__FILE__)
104
+ path = File.join(current_dir, '..', '..', 'vendor', 'js_test_driver.jar')
105
+ return File.expand_path(path)
106
+ end
107
+
108
+ def default_config_yml_path
109
+ return file("jsTestDriver.conf", generated_files_dir)
110
+ end
111
+
112
+ def default_generated_files_dir
113
+ return dir(".js_test_driver")
114
+ end
115
+
116
+ def default_coverage_files_path
117
+ return dir('coverage', generated_files_dir)
118
+ end
119
+
120
+ def default_test_xml_data_path
121
+ return dir('tests', generated_files_dir)
122
+ end
123
+
124
+ def default_fixture_dir
125
+ return dir('fixtures', generated_files_dir)
126
+ end
127
+
128
+ def default_remote_browsers_dir
129
+ return dir('browsers', generated_files_dir)
130
+ end
131
+
132
+ private
133
+
134
+ def dir(name, parent = nil)
135
+ path = File.expand_path(name, parent)
136
+ FileUtils.mkdir_p(path) unless File.exist?(path)
137
+ return path
138
+ end
139
+
140
+ def file(name, parent = nil)
141
+ path = File.expand_path(name, parent)
142
+ dir = File.dirname(path)
143
+ FileUtils.mkdir_p(dir) unless File.exist?(dir)
144
+ return path
145
+ end
146
+
147
+ def attributes=(values)
148
+ values.each do |attr, value|
149
+ self.send("#{attr}=", value)
150
+ end
151
+ end
152
+
153
+ end
154
+
155
+ end
@@ -4,25 +4,27 @@ namespace :js_test_driver do
4
4
 
5
5
  desc "Starts the server using the provided configuration variables"
6
6
  task :start_server do
7
- JsTestDriver::Runner.new.start_server
7
+ JsTestDriver::Application.new.start_server
8
8
  end
9
9
 
10
10
  desc "Runs the javascript tests"
11
11
  task :run_tests do
12
- exit(1) unless JsTestDriver::Runner.new.run_tests(ENV['TESTS'])
12
+ exit(1) unless JsTestDriver::Application.new.run_tests(:tests => ENV['TESTS'])
13
13
  end
14
14
 
15
15
  desc "Capture the browsers defined in config"
16
16
  task :capture_browsers do
17
- JsTestDriver::Runner.new.capture_browsers(ENV['BROWSERS'])
17
+ JsTestDriver::Application.new.capture_browsers(:browsers => ENV['BROWSERS'])
18
18
  end
19
19
 
20
20
  desc "Starts the server, captures the browsers, runs the tests - all at the same time"
21
21
  task :run do
22
- config = JsTestDriver::Runner.new
23
- output_path = ENV['OUTPUT_PATH']
24
- output_path = File.join(config.generated_files_dir, 'tests') if ENV['OUTPUT_XML']
25
- config.start_server_capture_and_run(ENV['TESTS'], ENV['BROWSERS'], output_path, ENV['CAPTURE_CONSOLE'])
22
+ app = JsTestDriver::Application.new
23
+
24
+ exit(1) unless app.run(:tests => ENV['TESTS'],
25
+ :browsers => ENV['BROWSERS'],
26
+ :output_xml => !!ENV['OUTPUT_XML'],
27
+ :capture_console => ENV['CAPTURE_CONSOLE'])
26
28
  end
27
29
 
28
- end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module JsTestDriver
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0.pre1"
3
3
  end
@@ -1,6 +1,30 @@
1
1
  require 'json'
2
+ require 'yaml'
2
3
  require 'fileutils'
3
4
 
4
- require File.expand_path(File.join(File.dirname(__FILE__), 'js_test_driver', 'config'))
5
- require File.expand_path(File.join(File.dirname(__FILE__), 'js_test_driver', 'runner'))
6
- require File.expand_path(File.join(File.dirname(__FILE__), 'js_test_driver', 'html_fixture'))
5
+ require 'selenium-webdriver'
6
+
7
+ module JsTestDriver
8
+ autoload :Application, 'js_test_driver/application'
9
+ autoload :Config, 'js_test_driver/config'
10
+ autoload :ConfigFactory, 'js_test_driver/config_factory'
11
+ autoload :HtmlFixture, 'js_test_driver/html_fixture'
12
+ autoload :MissingDependencyError, 'js_test_driver/missing_dependency_error'
13
+ autoload :RemoteBrowser, 'js_test_driver/remote_browser'
14
+ autoload :Runner, 'js_test_driver/runner'
15
+ autoload :RuntimeConfig, 'js_test_driver/runtime_config'
16
+ autoload :VERSION, 'js_test_driver/version'
17
+
18
+ module CLI
19
+ autoload :CaptureBrowsers, 'js_test_driver/cli/capture_browsers'
20
+ autoload :Run, 'js_test_driver/cli/run'
21
+ autoload :RunTests, 'js_test_driver/cli/run_tests'
22
+ autoload :StartServer, 'js_test_driver/cli/start_server'
23
+ end
24
+
25
+ module Commands
26
+ autoload :BaseCommand, 'js_test_driver/commands/base_command'
27
+ autoload :GenerateCoverageReport, 'js_test_driver/commands/generate_coverage_report'
28
+ autoload :JstdJarCommand, 'js_test_driver/commands/jstd_jar_command'
29
+ end
30
+ end
@@ -4,11 +4,10 @@ module JsTestDriver
4
4
  class SampleTest < Test::Unit::TestCase
5
5
 
6
6
  def test_running_sample_specs
7
- config = JsTestDriver::Runner.new(:config_path => File.expand_path('../../sample/config/js_test_driver.rb', __FILE__))
7
+ config_path = File.expand_path('../../sample/config/js_test_driver.rb', __FILE__)
8
+ app = JsTestDriver::Application.new(:config_path => config_path)
8
9
 
9
- output_path = File.expand_path('../../../.js_test_driver/', __FILE__)
10
-
11
- assert config.start_server_capture_and_run(nil, nil, output_path), 'running tests should return a success'
10
+ assert app.run(:output_xml => true), 'running tests should return a success'
12
11
  end
13
12
 
14
13
  end
@@ -1,9 +1,13 @@
1
+ host guess_local_ip
2
+
1
3
  includes 'test/sample/lib/*.js'
2
4
 
3
5
  ['google-chrome', 'firefox', 'chromium-browser'].select{|b| !%x[which #{b}].strip.empty? }.each do |b|
4
6
  browser b
5
7
  end
6
8
 
9
+ remote_browser '192.168.1.106:4444', :browser => :ie
10
+
7
11
  enable_jasmine
8
12
  measure_coverage
9
13
 
data/test/test_helper.rb CHANGED
@@ -8,9 +8,44 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'js_test
8
8
  require 'test/unit'
9
9
  require 'mocha'
10
10
 
11
+ class FakeRunner
12
+
13
+ attr_reader :commands_run, :commands_run_in_bg
14
+
15
+ def initialize
16
+ @commands_run = []
17
+ @commands_run_in_bg = []
18
+ end
19
+
20
+ def run(command)
21
+ @commands_run << command.to_s
22
+ end
23
+
24
+ def runbg(command)
25
+ @commands_run_in_bg << command.to_s
26
+ end
27
+
28
+ def has_run?(command)
29
+ @commands_run.include?(command.to_s)
30
+ end
31
+
32
+ def has_run_in_bg?(command)
33
+ @commands_run_in_bg.include?(command.to_s)
34
+ end
35
+ end
36
+
11
37
  class Test::Unit::TestCase
12
38
 
39
+ attr_reader :application, :runner
40
+
13
41
  def setup
42
+ @application = JsTestDriver::Application.new
43
+ runtime_config.config = runtime_config.config_factory.new
44
+ @runner = FakeRunner.new
45
+ @application.stubs(:runner => @runner)
46
+ end
47
+
48
+ def teardown
14
49
  clean_up_saved_config_files
15
50
  end
16
51
 
@@ -18,6 +53,22 @@ class Test::Unit::TestCase
18
53
  File.expand_path('fixtures', File.dirname(__FILE__))
19
54
  end
20
55
 
56
+ def config
57
+ application.config
58
+ end
59
+
60
+ def runtime_config
61
+ application.runtime_config
62
+ end
63
+
64
+ def assert_run(cmd)
65
+ assert runner.has_run?(cmd), "Expected to have run:\n#{cmd}\nbut only commands run were:\n#{runner.commands_run.join("\n")}"
66
+ end
67
+
68
+ def assert_run_in_bg(cmd)
69
+ assert runner.has_run_in_bg?(cmd), "Expected to have run in bg:\n#{cmd}\nbut only commands run were:\n#{runner.commands_run_in_bg.join("\n")}"
70
+ end
71
+
21
72
  def clean_up_saved_config_files
22
73
  root_dir = File.expand_path('..', File.dirname(__FILE__))
23
74
  Dir["#{root_dir}/**/.js_test_driver"].each do |file|
@@ -25,4 +76,4 @@ class Test::Unit::TestCase
25
76
  end
26
77
  end
27
78
 
28
- end
79
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ module JsTestDriver
4
+ class CaptureBrowsersTest < Test::Unit::TestCase
5
+
6
+ def test_should_capture_default_browsers
7
+ config.browser 'foo', 'bar', 'baz'
8
+ JsTestDriver::Commands::BaseCommand.any_instance.stubs(:ensure_installed!)
9
+
10
+ application.capture_browsers
11
+
12
+ ['foo', 'bar', 'baz'].each do |browser|
13
+ assert_run_in_bg("#{browser} http://localhost:4224/capture?strict")
14
+ end
15
+ end
16
+
17
+ def test_should_capture_given_browsers
18
+ config.browser 'foo', 'bar', 'baz'
19
+ JsTestDriver::Commands::BaseCommand.any_instance.stubs(:ensure_installed!)
20
+
21
+ application.capture_browsers(:browsers => 'aaa,bbb')
22
+
23
+ ['aaa', 'bbb'].each do |browser|
24
+ assert_run_in_bg("#{browser} http://localhost:4224/capture?strict")
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+
@@ -21,7 +21,7 @@ module JsTestDriver
21
21
  end
22
22
 
23
23
  def given_an_empty_config
24
- JsTestDriver::Config.new
24
+ application.config_factory.new
25
25
  end
26
26
 
27
27
  def test_empty_config
@@ -152,7 +152,7 @@ module JsTestDriver
152
152
  str = ""
153
153
 
154
154
  # when
155
- config = JsTestDriver::Config.parse(str)
155
+ config = application.config_factory.parse(str)
156
156
 
157
157
  # then
158
158
  assert_equal [], config.included_files
@@ -172,7 +172,7 @@ module JsTestDriver
172
172
  CONFIG
173
173
 
174
174
  # when
175
- config = JsTestDriver::Config.parse(str)
175
+ config = application.config_factory.parse(str)
176
176
 
177
177
  # then
178
178
  assert_equal ['a', 'b', 'c'].map{|p| File.expand_path(p)}, config.included_files
@@ -183,8 +183,6 @@ module JsTestDriver
183
183
  def test_given_a_config_path_should_expand_the_includes
184
184
  # given
185
185
  config = given_an_empty_config
186
- config.config_dir = "/foo/bbb/ccc"
187
- pwd = File.expand_path('.')
188
186
  config.includes "a/a", "/b/b", "../c"
189
187
 
190
188
  # then
@@ -194,7 +192,7 @@ module JsTestDriver
194
192
  def test_config_with_html_fixtures
195
193
  # given
196
194
  config = given_an_empty_config
197
- config.config_dir = File.expand_path("configs")
195
+ runtime_config.generated_files_dir = File.expand_path("configs")
198
196
 
199
197
  # when
200
198
  config.fixtures "fixture/directory", :name => "fixture_name", :namespace => "fixture_namespace"
@@ -203,23 +201,6 @@ module JsTestDriver
203
201
  assert_config_includes config, 'load' => paths(["configs/fixtures/fixture_namespace/fixture_name.js"])
204
202
  end
205
203
 
206
- def test_should_save_fixtures
207
- # given
208
- config = given_an_empty_config
209
- config.config_dir = "tmp/stuff"
210
- config.fixtures fixture_dir, :name => "fixture_name", :namespace => "fixture_namespace"
211
-
212
- # when
213
- config.save_fixtures
214
-
215
- # then
216
- name = "tmp/stuff/fixtures/fixture_namespace/fixture_name.js"
217
- assert File.exists?(name)
218
- assert_equal File.read(name), config.html_fixtures.first.to_s
219
- ensure
220
- FileUtils.rm_rf("tmp/stuff")
221
- end
222
-
223
204
  def test_should_raise_argument_error_if_the_same_fixture_defined_twice
224
205
  # given
225
206
  config = given_an_empty_config
@@ -266,5 +247,32 @@ module JsTestDriver
266
247
  'module' => 'com.google.jstestdriver.coverage.CoverageModule'}]
267
248
  end
268
249
 
250
+ def test_with_proxies
251
+ # given
252
+ config = given_an_empty_config
253
+
254
+ # when
255
+ config.proxy('/hello/*').to('http://localhost:3111')
256
+ config.proxy('*').to('http://localhost:3222')
257
+
258
+ # then
259
+ assert_config_includes config, 'proxy' => [
260
+ {'matcher' => '/hello/*', 'server' => 'http://localhost:3111'},
261
+ {'matcher' => '*', 'server' => 'http://localhost:3222'}
262
+ ]
263
+ end
264
+
265
+ def test_config_with_remote_browsers
266
+ # given
267
+ config = given_an_empty_config
268
+
269
+ # when
270
+ config.browser 'foo'
271
+ config.remote_browser('192.168.1.12:4444', :browser => :internet_explorer)
272
+
273
+ # then
274
+ assert_equal ['foo', File.join(runtime_config.remote_browsers_dir, 'remote-browser-192.168.1.12:4444-internet_explorer.rb')], config.browsers
275
+ end
276
+
269
277
  end
270
278
  end