sr-webrat 0.4.4.0 → 0.4.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/.document +4 -0
  2. data/.gitignore +15 -0
  3. data/lib/webrat/core/configuration.rb +102 -0
  4. data/lib/webrat/core/elements/area.rb +31 -0
  5. data/lib/webrat/core/elements/element.rb +33 -0
  6. data/lib/webrat/core/elements/field.rb +405 -0
  7. data/lib/webrat/core/elements/form.rb +103 -0
  8. data/lib/webrat/core/elements/label.rb +31 -0
  9. data/lib/webrat/core/elements/link.rb +92 -0
  10. data/lib/webrat/core/elements/select_option.rb +35 -0
  11. data/lib/webrat/core/locators/area_locator.rb +38 -0
  12. data/lib/webrat/core/locators/button_locator.rb +54 -0
  13. data/lib/webrat/core/locators/field_by_id_locator.rb +37 -0
  14. data/lib/webrat/core/locators/field_labeled_locator.rb +56 -0
  15. data/lib/webrat/core/locators/field_locator.rb +25 -0
  16. data/lib/webrat/core/locators/field_named_locator.rb +41 -0
  17. data/lib/webrat/core/locators/form_locator.rb +19 -0
  18. data/lib/webrat/core/locators/label_locator.rb +34 -0
  19. data/lib/webrat/core/locators/link_locator.rb +66 -0
  20. data/lib/webrat/core/locators/locator.rb +20 -0
  21. data/lib/webrat/core/locators/select_option_locator.rb +59 -0
  22. data/lib/webrat/core/locators.rb +20 -0
  23. data/lib/webrat/core/logging.rb +24 -0
  24. data/lib/webrat/core/matchers/have_content.rb +73 -0
  25. data/lib/webrat/core/matchers/have_selector.rb +74 -0
  26. data/lib/webrat/core/matchers/have_tag.rb +21 -0
  27. data/lib/webrat/core/matchers/have_xpath.rb +147 -0
  28. data/lib/webrat/core/matchers.rb +4 -0
  29. data/lib/webrat/core/methods.rb +63 -0
  30. data/lib/webrat/core/mime.rb +29 -0
  31. data/lib/webrat/core/save_and_open_page.rb +50 -0
  32. data/lib/webrat/core/scope.rb +350 -0
  33. data/lib/webrat/core/session.rb +289 -0
  34. data/lib/webrat/core/xml/hpricot.rb +19 -0
  35. data/lib/webrat/core/xml/nokogiri.rb +76 -0
  36. data/lib/webrat/core/xml/rexml.rb +24 -0
  37. data/lib/webrat/core/xml.rb +115 -0
  38. data/lib/webrat/core.rb +14 -0
  39. data/lib/webrat/core_extensions/blank.rb +58 -0
  40. data/lib/webrat/core_extensions/deprecate.rb +8 -0
  41. data/lib/webrat/core_extensions/detect_mapped.rb +12 -0
  42. data/lib/webrat/core_extensions/meta_class.rb +6 -0
  43. data/lib/webrat/core_extensions/nil_to_param.rb +5 -0
  44. data/lib/webrat/core_extensions/tcp_socket.rb +27 -0
  45. data/lib/webrat/mechanize.rb +74 -0
  46. data/lib/webrat/merb.rb +9 -0
  47. data/lib/webrat/merb_session.rb +65 -0
  48. data/lib/webrat/rack.rb +24 -0
  49. data/lib/webrat/rack_test.rb +36 -0
  50. data/lib/webrat/rails.rb +105 -0
  51. data/lib/webrat/rspec-rails.rb +13 -0
  52. data/lib/webrat/selenium/application_server.rb +73 -0
  53. data/lib/webrat/selenium/location_strategy_javascript/button.js +19 -0
  54. data/lib/webrat/selenium/location_strategy_javascript/label.js +16 -0
  55. data/lib/webrat/selenium/location_strategy_javascript/webrat.js +5 -0
  56. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +9 -0
  57. data/lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js +15 -0
  58. data/lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js +5 -0
  59. data/lib/webrat/selenium/matchers/have_content.rb +66 -0
  60. data/lib/webrat/selenium/matchers/have_selector.rb +49 -0
  61. data/lib/webrat/selenium/matchers/have_tag.rb +72 -0
  62. data/lib/webrat/selenium/matchers/have_xpath.rb +45 -0
  63. data/lib/webrat/selenium/matchers.rb +4 -0
  64. data/lib/webrat/selenium/merb_application_server.rb +48 -0
  65. data/lib/webrat/selenium/rails_application_server.rb +42 -0
  66. data/lib/webrat/selenium/selenium_extensions.js +6 -0
  67. data/lib/webrat/selenium/selenium_rc_server.rb +84 -0
  68. data/lib/webrat/selenium/selenium_session.rb +237 -0
  69. data/lib/webrat/selenium/silence_stream.rb +18 -0
  70. data/lib/webrat/selenium/sinatra_application_server.rb +35 -0
  71. data/lib/webrat/selenium.rb +81 -0
  72. data/lib/webrat/sinatra.rb +44 -0
  73. data/lib/webrat.rb +31 -0
  74. data/spec/fakes/test_session.rb +34 -0
  75. data/spec/integration/merb/.gitignore +21 -0
  76. data/spec/integration/merb/Rakefile +35 -0
  77. data/spec/integration/merb/app/controllers/application.rb +2 -0
  78. data/spec/integration/merb/app/controllers/exceptions.rb +13 -0
  79. data/spec/integration/merb/app/controllers/testing.rb +18 -0
  80. data/spec/integration/merb/app/views/exceptions/not_acceptable.html.erb +63 -0
  81. data/spec/integration/merb/app/views/exceptions/not_found.html.erb +47 -0
  82. data/spec/integration/merb/app/views/layout/application.html.erb +12 -0
  83. data/spec/integration/merb/app/views/testing/show_form.html.erb +27 -0
  84. data/spec/integration/merb/config/environments/development.rb +15 -0
  85. data/spec/integration/merb/config/environments/rake.rb +11 -0
  86. data/spec/integration/merb/config/environments/test.rb +14 -0
  87. data/spec/integration/merb/config/init.rb +25 -0
  88. data/spec/integration/merb/config/rack.rb +11 -0
  89. data/spec/integration/merb/config/router.rb +33 -0
  90. data/spec/integration/merb/spec/spec.opts +1 -0
  91. data/spec/integration/merb/spec/spec_helper.rb +24 -0
  92. data/spec/integration/merb/spec/webrat_spec.rb +32 -0
  93. data/spec/integration/merb/tasks/merb.thor/app_script.rb +31 -0
  94. data/spec/integration/merb/tasks/merb.thor/common.rb +64 -0
  95. data/spec/integration/merb/tasks/merb.thor/gem_ext.rb +124 -0
  96. data/spec/integration/merb/tasks/merb.thor/main.thor +150 -0
  97. data/spec/integration/merb/tasks/merb.thor/ops.rb +93 -0
  98. data/spec/integration/merb/tasks/merb.thor/utils.rb +40 -0
  99. data/spec/integration/rack/Rakefile +5 -0
  100. data/spec/integration/rack/rack_app.rb +66 -0
  101. data/spec/integration/rack/test/test_helper.rb +23 -0
  102. data/spec/integration/rack/test/webrat_rack_test.rb +41 -0
  103. data/spec/integration/rails/.gitignore +3 -0
  104. data/spec/integration/rails/Rakefile +30 -0
  105. data/spec/integration/rails/app/controllers/application.rb +15 -0
  106. data/spec/integration/rails/app/controllers/webrat_controller.rb +42 -0
  107. data/spec/integration/rails/app/views/webrat/before_redirect_form.html.erb +4 -0
  108. data/spec/integration/rails/app/views/webrat/buttons.html.erb +11 -0
  109. data/spec/integration/rails/app/views/webrat/form.html.erb +28 -0
  110. data/spec/integration/rails/config/boot.rb +109 -0
  111. data/spec/integration/rails/config/environment.rb +12 -0
  112. data/spec/integration/rails/config/environments/development.rb +17 -0
  113. data/spec/integration/rails/config/environments/selenium.rb +22 -0
  114. data/spec/integration/rails/config/environments/test.rb +22 -0
  115. data/spec/integration/rails/config/initializers/inflections.rb +10 -0
  116. data/spec/integration/rails/config/initializers/mime_types.rb +5 -0
  117. data/spec/integration/rails/config/initializers/new_rails_defaults.rb +17 -0
  118. data/spec/integration/rails/config/locales/en.yml +5 -0
  119. data/spec/integration/rails/config/routes.rb +15 -0
  120. data/spec/integration/rails/public/404.html +30 -0
  121. data/spec/integration/rails/public/422.html +30 -0
  122. data/spec/integration/rails/public/500.html +33 -0
  123. data/spec/integration/rails/script/about +4 -0
  124. data/spec/integration/rails/script/console +3 -0
  125. data/spec/integration/rails/script/dbconsole +3 -0
  126. data/spec/integration/rails/script/destroy +3 -0
  127. data/spec/integration/rails/script/generate +3 -0
  128. data/spec/integration/rails/script/performance/benchmarker +3 -0
  129. data/spec/integration/rails/script/performance/profiler +3 -0
  130. data/spec/integration/rails/script/performance/request +3 -0
  131. data/spec/integration/rails/script/plugin +3 -0
  132. data/spec/integration/rails/script/process/inspector +3 -0
  133. data/spec/integration/rails/script/process/reaper +3 -0
  134. data/spec/integration/rails/script/process/spawner +3 -0
  135. data/spec/integration/rails/script/runner +3 -0
  136. data/spec/integration/rails/script/server +3 -0
  137. data/spec/integration/rails/test/integration/button_click_test.rb +80 -0
  138. data/spec/integration/rails/test/integration/webrat_test.rb +87 -0
  139. data/spec/integration/rails/test/test_helper.rb +25 -0
  140. data/spec/integration/sinatra/Rakefile +5 -0
  141. data/spec/integration/sinatra/classic_app.rb +64 -0
  142. data/spec/integration/sinatra/modular_app.rb +16 -0
  143. data/spec/integration/sinatra/test/classic_app_test.rb +37 -0
  144. data/spec/integration/sinatra/test/modular_app_test.rb +18 -0
  145. data/spec/integration/sinatra/test/test_helper.rb +16 -0
  146. data/spec/private/core/configuration_spec.rb +106 -0
  147. data/spec/private/core/field_spec.rb +85 -0
  148. data/spec/private/core/link_spec.rb +24 -0
  149. data/spec/private/core/logging_spec.rb +10 -0
  150. data/spec/private/core/session_spec.rb +198 -0
  151. data/spec/private/mechanize/mechanize_session_spec.rb +81 -0
  152. data/spec/private/merb/merb_session_spec.rb +42 -0
  153. data/spec/private/nokogiri_spec.rb +77 -0
  154. data/spec/private/rails/attaches_file_spec.rb +81 -0
  155. data/spec/private/rails/rails_session_spec.rb +110 -0
  156. data/spec/public/basic_auth_spec.rb +24 -0
  157. data/spec/public/check_spec.rb +191 -0
  158. data/spec/public/choose_spec.rb +118 -0
  159. data/spec/public/click_area_spec.rb +106 -0
  160. data/spec/public/click_button_spec.rb +496 -0
  161. data/spec/public/click_link_spec.rb +511 -0
  162. data/spec/public/fill_in_spec.rb +209 -0
  163. data/spec/public/locators/field_by_xpath_spec.rb +19 -0
  164. data/spec/public/locators/field_labeled_spec.rb +172 -0
  165. data/spec/public/locators/field_with_id_spec.rb +16 -0
  166. data/spec/public/matchers/contain_spec.rb +114 -0
  167. data/spec/public/matchers/have_selector_spec.rb +135 -0
  168. data/spec/public/matchers/have_tag_spec.rb +39 -0
  169. data/spec/public/matchers/have_xpath_spec.rb +123 -0
  170. data/spec/public/reload_spec.rb +10 -0
  171. data/spec/public/save_and_open_spec.rb +51 -0
  172. data/spec/public/select_date_spec.rb +112 -0
  173. data/spec/public/select_datetime_spec.rb +137 -0
  174. data/spec/public/select_spec.rb +249 -0
  175. data/spec/public/select_time_spec.rb +100 -0
  176. data/spec/public/set_hidden_field_spec.rb +5 -0
  177. data/spec/public/submit_form_spec.rb +5 -0
  178. data/spec/public/visit_spec.rb +58 -0
  179. data/spec/public/within_spec.rb +177 -0
  180. data/spec/rcov.opts +1 -0
  181. data/spec/spec.opts +2 -0
  182. data/spec/spec_helper.rb +50 -0
  183. data/vendor/selenium-server.jar +0 -0
  184. data/webrat.gemspec +214 -0
  185. metadata +186 -2
@@ -0,0 +1,237 @@
1
+ require "webrat/core/save_and_open_page"
2
+ require "webrat/selenium/selenium_rc_server"
3
+ require "webrat/selenium/application_server"
4
+
5
+ module Webrat
6
+ class TimeoutError < WebratError
7
+ end
8
+
9
+ class SeleniumResponse
10
+ attr_reader :body
11
+ attr_reader :session
12
+
13
+ def initialize(session, body)
14
+ @session = session
15
+ @body = body
16
+ end
17
+
18
+ def selenium
19
+ session.selenium
20
+ end
21
+ end
22
+
23
+ class SeleniumSession
24
+ include Webrat::SaveAndOpenPage
25
+ include Webrat::Selenium::SilenceStream
26
+
27
+ def initialize(*args) # :nodoc:
28
+ end
29
+
30
+ def simulate
31
+ end
32
+
33
+ def automate
34
+ yield
35
+ end
36
+
37
+ def visit(url)
38
+ selenium.open(url)
39
+ end
40
+
41
+ webrat_deprecate :visits, :visit
42
+
43
+ def fill_in(field_identifier, options)
44
+ locator = "webrat=#{Regexp.escape(field_identifier)}"
45
+ selenium.wait_for_element locator, :timeout_in_seconds => 5
46
+ selenium.type(locator, "#{options[:with]}")
47
+ end
48
+
49
+ webrat_deprecate :fills_in, :fill_in
50
+
51
+ def response
52
+ SeleniumResponse.new(self, response_body)
53
+ end
54
+
55
+ def response_body #:nodoc:
56
+ selenium.get_html_source
57
+ end
58
+
59
+ def current_url
60
+ selenium.location
61
+ end
62
+
63
+ def click_button(button_text_or_regexp = nil, options = {})
64
+ if button_text_or_regexp.is_a?(Hash) && options == {}
65
+ pattern, options = nil, button_text_or_regexp
66
+ elsif button_text_or_regexp
67
+ pattern = adjust_if_regexp(button_text_or_regexp)
68
+ end
69
+ pattern ||= '*'
70
+ locator = "button=#{pattern}"
71
+
72
+ selenium.wait_for_element locator, :timeout_in_seconds => 5
73
+ selenium.click locator
74
+ end
75
+
76
+ webrat_deprecate :clicks_button, :click_button
77
+
78
+ def click_link(link_text_or_regexp, options = {})
79
+ pattern = adjust_if_regexp(link_text_or_regexp)
80
+ locator = "webratlink=#{pattern}"
81
+ selenium.wait_for_element locator, :timeout_in_seconds => 5
82
+ selenium.click locator
83
+ end
84
+
85
+ webrat_deprecate :clicks_link, :click_link
86
+
87
+ def click_link_within(selector, link_text, options = {})
88
+ locator = "webratlinkwithin=#{selector}|#{link_text}"
89
+ selenium.wait_for_element locator, :timeout_in_seconds => 5
90
+ selenium.click locator
91
+ end
92
+
93
+ webrat_deprecate :clicks_link_within, :click_link_within
94
+
95
+ def select(option_text, options = {})
96
+ id_or_name_or_label = options[:from]
97
+
98
+ if id_or_name_or_label
99
+ select_locator = "webrat=#{id_or_name_or_label}"
100
+ else
101
+ select_locator = "webratselectwithoption=#{option_text}"
102
+ end
103
+
104
+ selenium.wait_for_element select_locator, :timeout_in_seconds => 5
105
+ selenium.select(select_locator, option_text)
106
+ end
107
+
108
+ webrat_deprecate :selects, :select
109
+
110
+ def choose(label_text)
111
+ locator = "webrat=#{label_text}"
112
+ selenium.wait_for_element locator, :timeout_in_seconds => 5
113
+ selenium.click locator
114
+ end
115
+
116
+ webrat_deprecate :chooses, :choose
117
+
118
+ def check(label_text)
119
+ locator = "webrat=#{label_text}"
120
+ selenium.wait_for_element locator, :timeout_in_seconds => 5
121
+ selenium.click locator
122
+ end
123
+ alias_method :uncheck, :check
124
+
125
+ webrat_deprecate :checks, :check
126
+
127
+ def fire_event(field_identifier, event)
128
+ locator = "webrat=#{Regexp.escape(field_identifier)}"
129
+ selenium.fire_event(locator, "#{event}")
130
+ end
131
+
132
+ def key_down(field_identifier, key_code)
133
+ locator = "webrat=#{Regexp.escape(field_identifier)}"
134
+ selenium.key_down(locator, key_code)
135
+ end
136
+
137
+ def key_up(field_identifier, key_code)
138
+ locator = "webrat=#{Regexp.escape(field_identifier)}"
139
+ selenium.key_up(locator, key_code)
140
+ end
141
+
142
+ def wait_for(params={})
143
+ timeout = params[:timeout] || 5
144
+ message = params[:message] || "Timeout exceeded"
145
+
146
+ begin_time = Time.now
147
+
148
+ while (Time.now - begin_time) < timeout
149
+ value = nil
150
+
151
+ begin
152
+ value = yield
153
+ rescue ::Spec::Expectations::ExpectationNotMetError, ::Selenium::CommandError, Webrat::WebratError
154
+ value = nil
155
+ end
156
+
157
+ return value if value
158
+
159
+ sleep 0.25
160
+ end
161
+
162
+ raise Webrat::TimeoutError.new(message + " (after #{timeout} sec)")
163
+ true
164
+ end
165
+
166
+ def selenium
167
+ return $browser if $browser
168
+ setup
169
+ $browser
170
+ end
171
+
172
+ webrat_deprecate :browser, :selenium
173
+
174
+
175
+ def save_and_open_screengrab
176
+ return unless File.exist?(saved_page_dir)
177
+
178
+ filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png"
179
+
180
+ if $browser.chrome_backend?
181
+ $browser.capture_entire_page_screenshot(filename, '')
182
+ else
183
+ $browser.capture_screenshot(filename)
184
+ end
185
+ open_in_browser(filename)
186
+ end
187
+
188
+ protected
189
+
190
+ def setup #:nodoc:
191
+ Webrat::Selenium::SeleniumRCServer.boot
192
+ Webrat::Selenium::ApplicationServer.boot
193
+
194
+ create_browser
195
+ $browser.start
196
+
197
+ extend_selenium
198
+ define_location_strategies
199
+ $browser.window_maximize
200
+ end
201
+
202
+
203
+ def create_browser
204
+ $browser = ::Selenium::Client::Driver.new(Webrat.configuration.selenium_server_address || "localhost",
205
+ Webrat.configuration.selenium_server_port, Webrat.configuration.selenium_browser_key, "http://#{Webrat.configuration.application_address}:#{Webrat.configuration.application_port}")
206
+ $browser.set_speed(0) unless Webrat.configuration.selenium_server_address
207
+
208
+ at_exit do
209
+ silence_stream(STDOUT) do
210
+ $browser.stop
211
+ end
212
+ end
213
+ end
214
+
215
+ def adjust_if_regexp(text_or_regexp) #:nodoc:
216
+ if text_or_regexp.is_a?(Regexp)
217
+ "evalregex:#{text_or_regexp.inspect}"
218
+ else
219
+ "evalregex:/#{text_or_regexp}/"
220
+ end
221
+ end
222
+
223
+ def extend_selenium #:nodoc:
224
+ extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
225
+ extenions_js = File.read(extensions_file)
226
+ selenium.get_eval(extenions_js)
227
+ end
228
+
229
+ def define_location_strategies #:nodoc:
230
+ Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
231
+ strategy_js = File.read(file)
232
+ strategy_name = File.basename(file, '.js')
233
+ selenium.add_location_strategy(strategy_name, strategy_js)
234
+ end
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,18 @@
1
+ module Webrat
2
+ module Selenium
3
+ module SilenceStream
4
+ # active_support already defines silence_stream, no need to do that again if it's already present.
5
+ # http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/kernel/reporting.rb
6
+ unless Kernel.respond_to?(:silence_stream)
7
+ def silence_stream(stream)
8
+ old_stream = stream.dup
9
+ stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
10
+ stream.sync = true
11
+ yield
12
+ ensure
13
+ stream.reopen(old_stream)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ module Webrat
2
+ module Selenium
3
+
4
+ class SinatraApplicationServer < ApplicationServer
5
+
6
+ def start
7
+ fork do
8
+ File.open('rack.pid', 'w') { |fp| fp.write Process.pid }
9
+ exec 'rackup', File.expand_path(Dir.pwd + '/config.ru'), '-p', Webrat.configuration.application_port.to_s
10
+ end
11
+ end
12
+
13
+ def stop
14
+ silence_stream(STDOUT) do
15
+ pid = File.read(pid_file)
16
+ system("kill -9 #{pid}")
17
+ FileUtils.rm_f pid_file
18
+ end
19
+ end
20
+
21
+ def fail
22
+ $stderr.puts
23
+ $stderr.puts
24
+ $stderr.puts "==> Failed to boot the Sinatra application server... exiting!"
25
+ exit
26
+ end
27
+
28
+ def pid_file
29
+ prepare_pid_file(Dir.pwd, 'rack.pid')
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,81 @@
1
+ require "webrat"
2
+ gem "selenium-client", ">=1.2.14"
3
+ require "selenium/client"
4
+ require "webrat/selenium/silence_stream"
5
+ require "webrat/selenium/selenium_session"
6
+ require "webrat/selenium/matchers"
7
+ require "webrat/core_extensions/tcp_socket"
8
+
9
+ module Webrat
10
+ # To use Webrat's Selenium support, you'll need the selenium-client gem installed.
11
+ # Activate it with (for example, in your <tt>env.rb</tt>):
12
+ #
13
+ # require "webrat"
14
+ #
15
+ # Webrat.configure do |config|
16
+ # config.mode = :selenium
17
+ # end
18
+ #
19
+ # == Dropping down to the selenium-client API
20
+ #
21
+ # If you ever need to do something with Selenium not provided in the Webrat API,
22
+ # you can always drop down to the selenium-client API using the <tt>selenium</tt> method.
23
+ # For example:
24
+ #
25
+ # When "I drag the photo to the left" do
26
+ # selenium.dragdrop("id=photo_123", "+350, 0")
27
+ # end
28
+ #
29
+ # == Choosing the underlying framework to test
30
+ #
31
+ # Webrat assumes you're using rails by default but it can also work with sinatra
32
+ # and merb. To take advantage of this you can use the configuration block to
33
+ # set the application_framework variable.
34
+ # require "webrat"
35
+ #
36
+ # Webrat.configure do |config|
37
+ # config.mode = :selenium
38
+ # config.application_port = 4567
39
+ # config.application_framework = :sinatra # could also be :merb
40
+ # end
41
+ #
42
+ # == Auto-starting of the appserver and java server
43
+ #
44
+ # Webrat will automatically start the Selenium Java server process and an instance
45
+ # of Mongrel when a test is run. The Mongrel will run in the "selenium" environment
46
+ # instead of "test", so ensure you've got that defined, and will run on port
47
+ # Webrat.configuration.application_port.
48
+ #
49
+ # == Waiting
50
+ #
51
+ # In order to make writing Selenium tests as easy as possible, Webrat will automatically
52
+ # wait for the correct elements to exist on the page when trying to manipulate them
53
+ # with methods like <tt>fill_in</tt>, etc. In general, this means you should be able to write
54
+ # your Webrat::Selenium tests ignoring the concurrency issues that can plague in-browser
55
+ # testing, so long as you're using the Webrat API.
56
+ module Selenium
57
+ module Methods
58
+ def response
59
+ webrat_session.response
60
+ end
61
+
62
+ def wait_for(*args, &block)
63
+ webrat_session.wait_for(*args, &block)
64
+ end
65
+
66
+ def save_and_open_screengrab
67
+ webrat_session.save_and_open_screengrab
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ if defined?(ActionController::IntegrationTest)
74
+ module ActionController #:nodoc:
75
+ IntegrationTest.class_eval do
76
+ include Webrat::Methods
77
+ include Webrat::Selenium::Methods
78
+ include Webrat::Selenium::Matchers
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,44 @@
1
+ require "webrat/rack"
2
+ require "sinatra/test"
3
+
4
+ module Webrat
5
+ class SinatraSession < RackSession
6
+ include Sinatra::Test
7
+
8
+ attr_reader :request, :response
9
+
10
+ def initialize(context = nil)
11
+ super(context)
12
+
13
+ app = context.respond_to?(:app) ? context.app : Sinatra::Application
14
+ @browser = Sinatra::TestHarness.new(app)
15
+ end
16
+
17
+ %w(get head post put delete).each do |verb|
18
+ class_eval <<-RUBY
19
+ def #{verb}(path, data, headers = {})
20
+ params = data.inject({}) do |data, (key,value)|
21
+ data[key] = Rack::Utils.unescape(value)
22
+ data
23
+ end
24
+ headers["HTTP_HOST"] = "www.example.com"
25
+ @browser.#{verb}(path, params, headers)
26
+ end
27
+ RUBY
28
+ end
29
+
30
+ def response_body
31
+ @browser.body
32
+ end
33
+
34
+ def response_code
35
+ @browser.status
36
+ end
37
+
38
+ private
39
+
40
+ def response
41
+ @browser.response
42
+ end
43
+ end
44
+ end
data/lib/webrat.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "rubygems"
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
4
+
5
+ module Webrat
6
+ # The common base class for all exceptions raised by Webrat.
7
+ class WebratError < StandardError
8
+ end
9
+
10
+ VERSION = '0.4.4'
11
+
12
+ def self.require_xml
13
+ if on_java?
14
+ gem "nokogiri", ">= 1.2.4"
15
+ else
16
+ gem "nokogiri", ">= 1.0.6"
17
+ end
18
+
19
+ require "nokogiri"
20
+ require "webrat/core/xml/nokogiri"
21
+ end
22
+
23
+ def self.on_java?
24
+ RUBY_PLATFORM =~ /java/
25
+ end
26
+
27
+ end
28
+
29
+ Webrat.require_xml
30
+
31
+ require "webrat/core"
@@ -0,0 +1,34 @@
1
+ module Webrat #:nodoc:
2
+ def self.session_class #:nodoc:
3
+ TestSession
4
+ end
5
+
6
+ class TestSession < Session #:nodoc:
7
+ attr_accessor :response_body
8
+ attr_writer :response_code
9
+
10
+ def doc_root
11
+ File.expand_path(File.join(".", "public"))
12
+ end
13
+
14
+ def response
15
+ @response ||= Object.new
16
+ end
17
+
18
+ def response_code
19
+ @response_code || 200
20
+ end
21
+
22
+ def get(url, data, headers = nil)
23
+ end
24
+
25
+ def post(url, data, headers = nil)
26
+ end
27
+
28
+ def put(url, data, headers = nil)
29
+ end
30
+
31
+ def delete(url, data, headers = nil)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ .DS_Store
2
+ log/*
3
+ tmp/*
4
+ TAGS
5
+ *~
6
+ .#*
7
+ schema/schema.rb
8
+ schema/*_structure.sql
9
+ schema/*.sqlite3
10
+ schema/*.sqlite
11
+ schema/*.db
12
+ *.sqlite
13
+ *.sqlite3
14
+ *.db
15
+ src/*
16
+ .hgignore
17
+ .hg/*
18
+ .svn/*
19
+ gems/gems/*
20
+ gems/specifications/*
21
+ merb_profile_results
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake/rdoctask'
3
+
4
+ require 'merb-core'
5
+ require 'merb-core/tasks/merb'
6
+
7
+ include FileUtils
8
+
9
+ # Load the basic runtime dependencies; this will include
10
+ # any plugins and therefore plugin rake tasks.
11
+ init_env = ENV['MERB_ENV'] || 'rake'
12
+ Merb.load_dependencies(:environment => init_env)
13
+
14
+ # Get Merb plugins and dependencies
15
+ Merb::Plugins.rakefiles.each { |r| require r }
16
+
17
+ # Load any app level custom rakefile extensions from lib/tasks
18
+ tasks_path = File.join(File.dirname(__FILE__), "lib", "tasks")
19
+ rake_files = Dir["#{tasks_path}/*.rake"]
20
+ rake_files.each{|rake_file| load rake_file }
21
+
22
+ desc "Start runner environment"
23
+ task :merb_env do
24
+ Merb.start_environment(:environment => init_env, :adapter => 'runner')
25
+ end
26
+
27
+ require 'spec/rake/spectask'
28
+ require 'merb-core/test/tasks/spectasks'
29
+ desc 'Default: run spec examples'
30
+ task :default => 'spec'
31
+
32
+ ##############################################################################
33
+ # ADD YOUR CUSTOM TASKS IN /lib/tasks
34
+ # NAME YOUR RAKE FILES file_name.rake
35
+ ##############################################################################
@@ -0,0 +1,2 @@
1
+ class Application < Merb::Controller
2
+ end
@@ -0,0 +1,13 @@
1
+ class Exceptions < Merb::Controller
2
+
3
+ # handle NotFound exceptions (404)
4
+ def not_found
5
+ render :format => :html
6
+ end
7
+
8
+ # handle NotAcceptable exceptions (406)
9
+ def not_acceptable
10
+ render :format => :html
11
+ end
12
+
13
+ end
@@ -0,0 +1,18 @@
1
+ class Testing < Application
2
+
3
+ def show_form
4
+ render
5
+ end
6
+
7
+ def submit_form
8
+ end
9
+
10
+ def internal_redirect
11
+ redirect "/"
12
+ end
13
+
14
+ def external_redirect
15
+ redirect "http://google.com"
16
+ end
17
+
18
+ end
@@ -0,0 +1,63 @@
1
+ <div id="container">
2
+ <div id="header-container">
3
+ <img src="/images/merb.jpg" />
4
+ <!-- <h1>Mongrel + Erb</h1> -->
5
+ <h2>pocket rocket web framework</h2>
6
+ <hr />
7
+ </div>
8
+
9
+ <div id="left-container">
10
+ <h3>Exception:</h3>
11
+ <p><%= request.exceptions.first.message %></p>
12
+ </div>
13
+
14
+ <div id="main-container">
15
+ <h3>Why am I seeing this page?</h3>
16
+ <p>Merb couldn't find an appropriate content_type to return,
17
+ based on what you said was available via provides() and
18
+ what the client requested.</p>
19
+
20
+ <h3>How to add a mime-type</h3>
21
+ <pre><code>
22
+ Merb.add_mime_type :pdf, :to_pdf, %w[application/pdf], &quot;Content-Encoding&quot; =&gt; &quot;gzip&quot;
23
+ </code></pre>
24
+ <h3>What this means is:</h3>
25
+ <ul>
26
+ <li>Add a mime-type for :pdf</li>
27
+ <li>Register the method for converting objects to PDF as <code>#to_pdf</code>.</li>
28
+ <li>Register the incoming mime-type "Accept" header as <code>application/pdf</code>.</li>
29
+ <li>Specify a new header for PDF types so it will set <code>Content-Encoding</code> to gzip.</li>
30
+ </ul>
31
+
32
+ <h3>You can then do:</h3>
33
+ <pre><code>
34
+ class Foo &lt; Application
35
+ provides :pdf
36
+ end
37
+ </code></pre>
38
+
39
+ <h3>Where can I find help?</h3>
40
+ <p>If you have any questions or if you can't figure something out, please take a
41
+ look at our <a href="http://merbivore.com/"> project page</a>,
42
+ feel free to come chat at irc.freenode.net, channel #merb,
43
+ or post to <a href="http://groups.google.com/group/merb">merb mailing list</a>
44
+ on Google Groups.</p>
45
+
46
+ <h3>What if I've found a bug?</h3>
47
+ <p>If you want to file a bug or make your own contribution to Merb,
48
+ feel free to register and create a ticket at our
49
+ <a href="http://merb.lighthouseapp.com/">project development page</a>
50
+ on Lighthouse.</p>
51
+
52
+ <h3>How do I edit this page?</h3>
53
+ <p>You can change what people see when this happens by editing <tt>app/views/exceptions/not_acceptable.html.erb</tt>.</p>
54
+
55
+ </div>
56
+
57
+ <div id="footer-container">
58
+ <hr />
59
+ <div class="left"></div>
60
+ <div class="right">&copy; 2008 the merb dev team</div>
61
+ <p>&nbsp;</p>
62
+ </div>
63
+ </div>
@@ -0,0 +1,47 @@
1
+ <div id="container">
2
+ <div id="header-container">
3
+ <img src="/images/merb.jpg" />
4
+ <!-- <h1>Mongrel + Erb</h1> -->
5
+ <h2>pocket rocket web framework</h2>
6
+ <hr />
7
+ </div>
8
+
9
+ <div id="left-container">
10
+ <h3>Exception:</h3>
11
+ <p><%= request.exceptions.first.message %></p>
12
+ </div>
13
+
14
+ <div id="main-container">
15
+ <h3>Welcome to Merb!</h3>
16
+ <p>Merb is a light-weight MVC framework written in Ruby. We hope you enjoy it.</p>
17
+
18
+ <h3>Where can I find help?</h3>
19
+ <p>If you have any questions or if you can't figure something out, please take a
20
+ look at our <a href="http://merbivore.com/"> project page</a>,
21
+ feel free to come chat at irc.freenode.net, channel #merb,
22
+ or post to <a href="http://groups.google.com/group/merb">merb mailing list</a>
23
+ on Google Groups.</p>
24
+
25
+ <h3>What if I've found a bug?</h3>
26
+ <p>If you want to file a bug or make your own contribution to Merb,
27
+ feel free to register and create a ticket at our
28
+ <a href="http://merb.lighthouseapp.com/">project development page</a>
29
+ on Lighthouse.</p>
30
+
31
+ <h3>How do I edit this page?</h3>
32
+ <p>You're seeing this page because you need to edit the following files:
33
+ <ul>
34
+ <li>config/router.rb <strong><em>(recommended)</em></strong></li>
35
+ <li>app/views/exceptions/not_found.html.erb <strong><em>(recommended)</em></strong></li>
36
+ <li>app/views/layout/application.html.erb <strong><em>(change this layout)</em></strong></li>
37
+ </ul>
38
+ </p>
39
+ </div>
40
+
41
+ <div id="footer-container">
42
+ <hr />
43
+ <div class="left"></div>
44
+ <div class="right">&copy; 2008 the merb dev team</div>
45
+ <p>&nbsp;</p>
46
+ </div>
47
+ </div>