honkster-webrat 0.4.4.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. data/.document +4 -0
  2. data/.gitignore +16 -0
  3. data/History.txt +56 -1
  4. data/Rakefile +79 -91
  5. data/VERSION +1 -0
  6. data/lib/webrat/{mechanize.rb → adapters/mechanize.rb} +5 -1
  7. data/lib/webrat/adapters/merb.rb +11 -0
  8. data/lib/webrat/adapters/rack.rb +25 -0
  9. data/lib/webrat/{rails.rb → adapters/rails.rb} +6 -30
  10. data/lib/webrat/adapters/sinatra.rb +9 -0
  11. data/lib/webrat/core/configuration.rb +6 -16
  12. data/lib/webrat/core/elements/area.rb +2 -2
  13. data/lib/webrat/core/elements/element.rb +3 -3
  14. data/lib/webrat/core/elements/field.rb +107 -31
  15. data/lib/webrat/core/elements/form.rb +4 -4
  16. data/lib/webrat/core/elements/label.rb +4 -4
  17. data/lib/webrat/core/elements/link.rb +5 -5
  18. data/lib/webrat/core/elements/select_option.rb +15 -2
  19. data/lib/webrat/core/locators/area_locator.rb +3 -3
  20. data/lib/webrat/core/locators/button_locator.rb +6 -6
  21. data/lib/webrat/core/locators/field_by_id_locator.rb +3 -3
  22. data/lib/webrat/core/locators/field_labeled_locator.rb +2 -2
  23. data/lib/webrat/core/locators/field_named_locator.rb +3 -3
  24. data/lib/webrat/core/locators/form_locator.rb +1 -1
  25. data/lib/webrat/core/locators/label_locator.rb +2 -2
  26. data/lib/webrat/core/locators/link_locator.rb +7 -7
  27. data/lib/webrat/core/locators/select_option_locator.rb +5 -5
  28. data/lib/webrat/core/locators.rb +1 -1
  29. data/lib/webrat/core/logging.rb +4 -5
  30. data/lib/webrat/core/matchers/have_content.rb +2 -7
  31. data/lib/webrat/core/matchers/have_xpath.rb +3 -28
  32. data/lib/webrat/core/methods.rb +5 -4
  33. data/lib/webrat/core/mime.rb +11 -22
  34. data/lib/webrat/core/scope.rb +17 -2
  35. data/lib/webrat/core/session.rb +28 -12
  36. data/lib/webrat/core/xml.rb +41 -84
  37. data/lib/webrat/core.rb +0 -1
  38. data/lib/webrat/integrations/merb.rb +10 -0
  39. data/lib/webrat/integrations/rails.rb +25 -0
  40. data/lib/webrat/integrations/rspec-rails.rb +10 -0
  41. data/lib/webrat/integrations/selenium.rb +11 -0
  42. data/lib/webrat/rspec-rails.rb +2 -10
  43. data/lib/webrat/selenium/application_servers/external.rb +1 -1
  44. data/lib/webrat/selenium/application_servers.rb +1 -1
  45. data/lib/webrat/selenium/location_strategy_javascript/label.js +31 -13
  46. data/lib/webrat/selenium/location_strategy_javascript/webrat.js +1 -0
  47. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +24 -4
  48. data/lib/webrat/selenium/selenium_rc_server.rb +4 -2
  49. data/lib/webrat/selenium/selenium_session.rb +21 -2
  50. data/lib/webrat/selenium/silence_stream.rb +1 -1
  51. data/lib/webrat/selenium.rb +0 -11
  52. data/lib/webrat.rb +10 -19
  53. data/spec/fakes/test_adapter.rb +37 -0
  54. data/spec/integration/mechanize/Rakefile +7 -0
  55. data/spec/integration/mechanize/config.ru +2 -0
  56. data/spec/integration/mechanize/sample_app.rb +20 -0
  57. data/spec/integration/mechanize/spec/mechanize_spec.rb +22 -0
  58. data/spec/integration/mechanize/spec/spec_helper.rb +27 -0
  59. data/spec/integration/merb/.gitignore +21 -0
  60. data/spec/integration/merb/Rakefile +35 -0
  61. data/spec/integration/merb/app/controllers/application.rb +2 -0
  62. data/spec/integration/merb/app/controllers/exceptions.rb +13 -0
  63. data/spec/integration/merb/app/controllers/testing.rb +27 -0
  64. data/spec/integration/merb/app/views/exceptions/not_acceptable.html.erb +63 -0
  65. data/spec/integration/merb/app/views/exceptions/not_found.html.erb +47 -0
  66. data/spec/integration/merb/app/views/layout/application.html.erb +12 -0
  67. data/spec/integration/merb/app/views/testing/show_form.html.erb +27 -0
  68. data/spec/integration/merb/app/views/testing/upload.html.erb +9 -0
  69. data/spec/integration/merb/config/environments/development.rb +15 -0
  70. data/spec/integration/merb/config/environments/rake.rb +11 -0
  71. data/spec/integration/merb/config/environments/test.rb +14 -0
  72. data/spec/integration/merb/config/init.rb +25 -0
  73. data/spec/integration/merb/config/rack.rb +11 -0
  74. data/spec/integration/merb/config/router.rb +34 -0
  75. data/spec/integration/merb/spec/spec.opts +1 -0
  76. data/spec/integration/merb/spec/spec_helper.rb +26 -0
  77. data/spec/integration/merb/spec/webrat_spec.rb +39 -0
  78. data/spec/integration/merb/tasks/merb.thor/app_script.rb +31 -0
  79. data/spec/integration/merb/tasks/merb.thor/common.rb +64 -0
  80. data/spec/integration/merb/tasks/merb.thor/gem_ext.rb +124 -0
  81. data/spec/integration/merb/tasks/merb.thor/main.thor +150 -0
  82. data/spec/integration/merb/tasks/merb.thor/ops.rb +93 -0
  83. data/spec/integration/merb/tasks/merb.thor/utils.rb +40 -0
  84. data/spec/integration/rack/Rakefile +5 -0
  85. data/spec/integration/rack/app.rb +89 -0
  86. data/spec/integration/rack/test/helper.rb +21 -0
  87. data/spec/integration/rack/test/webrat_rack_test.rb +73 -0
  88. data/spec/integration/rails/.gitignore +3 -0
  89. data/spec/integration/rails/Rakefile +30 -0
  90. data/spec/integration/rails/app/controllers/application.rb +15 -0
  91. data/spec/integration/rails/app/controllers/buttons_controller.rb +7 -0
  92. data/spec/integration/rails/app/controllers/fields_controller.rb +4 -0
  93. data/spec/integration/rails/app/controllers/links_controller.rb +7 -0
  94. data/spec/integration/rails/app/controllers/webrat_controller.rb +46 -0
  95. data/spec/integration/rails/app/helpers/buttons_helper.rb +2 -0
  96. data/spec/integration/rails/app/helpers/fields_helper.rb +2 -0
  97. data/spec/integration/rails/app/helpers/links_helper.rb +2 -0
  98. data/spec/integration/rails/app/views/buttons/show.html.erb +11 -0
  99. data/spec/integration/rails/app/views/fields/show.html.erb +9 -0
  100. data/spec/integration/rails/app/views/links/show.html.erb +5 -0
  101. data/spec/integration/rails/app/views/webrat/before_redirect_form.html.erb +4 -0
  102. data/spec/integration/rails/app/views/webrat/buttons.html.erb +11 -0
  103. data/spec/integration/rails/app/views/webrat/form.html.erb +28 -0
  104. data/spec/integration/rails/app/views/webrat/within.html.erb +3 -0
  105. data/spec/integration/rails/config/boot.rb +109 -0
  106. data/spec/integration/rails/config/environment.rb +12 -0
  107. data/spec/integration/rails/config/environments/development.rb +17 -0
  108. data/spec/integration/rails/config/environments/selenium.rb +22 -0
  109. data/spec/integration/rails/config/environments/test.rb +22 -0
  110. data/spec/integration/rails/config/initializers/inflections.rb +10 -0
  111. data/spec/integration/rails/config/initializers/mime_types.rb +5 -0
  112. data/spec/integration/rails/config/initializers/new_rails_defaults.rb +17 -0
  113. data/spec/integration/rails/config/locales/en.yml +5 -0
  114. data/spec/integration/rails/config/routes.rb +19 -0
  115. data/spec/integration/rails/public/404.html +30 -0
  116. data/spec/integration/rails/public/422.html +30 -0
  117. data/spec/integration/rails/public/500.html +33 -0
  118. data/spec/integration/rails/script/about +4 -0
  119. data/spec/integration/rails/script/console +3 -0
  120. data/spec/integration/rails/script/dbconsole +3 -0
  121. data/spec/integration/rails/script/destroy +3 -0
  122. data/spec/integration/rails/script/generate +3 -0
  123. data/spec/integration/rails/script/performance/benchmarker +3 -0
  124. data/spec/integration/rails/script/performance/profiler +3 -0
  125. data/spec/integration/rails/script/performance/request +3 -0
  126. data/spec/integration/rails/script/plugin +3 -0
  127. data/spec/integration/rails/script/process/inspector +3 -0
  128. data/spec/integration/rails/script/process/reaper +3 -0
  129. data/spec/integration/rails/script/process/spawner +3 -0
  130. data/spec/integration/rails/script/runner +3 -0
  131. data/spec/integration/rails/script/server +3 -0
  132. data/spec/integration/rails/test/integration/button_click_test.rb +80 -0
  133. data/spec/integration/rails/test/integration/fill_in_test.rb +24 -0
  134. data/spec/integration/rails/test/integration/link_click_test.rb +27 -0
  135. data/spec/integration/rails/test/integration/webrat_test.rb +122 -0
  136. data/spec/integration/rails/test/test_helper.rb +26 -0
  137. data/spec/integration/sinatra/Rakefile +5 -0
  138. data/spec/integration/sinatra/classic_app.rb +64 -0
  139. data/spec/integration/sinatra/modular_app.rb +16 -0
  140. data/spec/integration/sinatra/test/classic_app_test.rb +37 -0
  141. data/spec/integration/sinatra/test/modular_app_test.rb +18 -0
  142. data/spec/integration/sinatra/test/test_helper.rb +17 -0
  143. data/spec/private/core/configuration_spec.rb +87 -0
  144. data/spec/private/core/field_spec.rb +85 -0
  145. data/spec/private/core/link_spec.rb +24 -0
  146. data/spec/private/core/session_spec.rb +200 -0
  147. data/spec/private/mechanize/mechanize_adapter_spec.rb +73 -0
  148. data/spec/private/nokogiri_spec.rb +77 -0
  149. data/spec/private/rails/attaches_file_spec.rb +81 -0
  150. data/spec/private/rails/rails_adapter_spec.rb +86 -0
  151. data/spec/private/selenium/application_servers/rails_spec.rb +26 -0
  152. data/spec/public/basic_auth_spec.rb +24 -0
  153. data/spec/public/check_spec.rb +191 -0
  154. data/spec/public/choose_spec.rb +118 -0
  155. data/spec/public/click_area_spec.rb +106 -0
  156. data/spec/public/click_button_spec.rb +494 -0
  157. data/spec/public/click_link_spec.rb +511 -0
  158. data/spec/public/fill_in_spec.rb +224 -0
  159. data/spec/public/locators/field_by_xpath_spec.rb +19 -0
  160. data/spec/public/locators/field_labeled_spec.rb +172 -0
  161. data/spec/public/locators/field_with_id_spec.rb +16 -0
  162. data/spec/public/matchers/contain_spec.rb +114 -0
  163. data/spec/public/matchers/have_selector_spec.rb +146 -0
  164. data/spec/public/matchers/have_tag_spec.rb +39 -0
  165. data/spec/public/matchers/have_xpath_spec.rb +136 -0
  166. data/spec/public/reload_spec.rb +10 -0
  167. data/spec/public/save_and_open_spec.rb +70 -0
  168. data/spec/public/select_date_spec.rb +112 -0
  169. data/spec/public/select_datetime_spec.rb +137 -0
  170. data/spec/public/select_spec.rb +455 -0
  171. data/spec/public/select_time_spec.rb +100 -0
  172. data/spec/public/selenium/application_server_factory_spec.rb +49 -0
  173. data/spec/public/selenium/application_servers/external_spec.rb +12 -0
  174. data/spec/public/selenium/selenium_session_spec.rb +37 -0
  175. data/spec/public/set_hidden_field_spec.rb +5 -0
  176. data/spec/public/submit_form_spec.rb +5 -0
  177. data/spec/public/visit_spec.rb +58 -0
  178. data/spec/public/within_spec.rb +177 -0
  179. data/spec/rcov.opts +1 -0
  180. data/spec/spec.opts +2 -0
  181. data/spec/spec_helper.rb +54 -0
  182. data/vendor/selenium-server.jar +0 -0
  183. data/webrat.gemspec +360 -0
  184. metadata +292 -27
  185. data/lib/webrat/core/xml/hpricot.rb +0 -19
  186. data/lib/webrat/core/xml/nokogiri.rb +0 -76
  187. data/lib/webrat/core/xml/rexml.rb +0 -24
  188. data/lib/webrat/merb_multipart_support.rb +0 -27
  189. data/lib/webrat/merb_session.rb +0 -80
  190. data/lib/webrat/rack.rb +0 -24
  191. data/lib/webrat/rack_test.rb +0 -32
  192. data/lib/webrat/sinatra.rb +0 -44
@@ -1,5 +1,4 @@
1
- require "webrat/core/xml/nokogiri"
2
- require "webrat/core/xml/rexml"
1
+ require "webrat/core/xml"
3
2
 
4
3
  module Webrat
5
4
  module Matchers
@@ -16,38 +15,14 @@ module Webrat
16
15
  matched = matches(stringlike)
17
16
 
18
17
  if @options[:count]
19
- matched.size == @options[:count] && (!@block || @block.call(matched))
18
+ matched.size == @options[:count].to_i && (!@block || @block.call(matched))
20
19
  else
21
20
  matched.any? && (!@block || @block.call(matched))
22
21
  end
23
22
  end
24
23
 
25
24
  def matches(stringlike)
26
- if Webrat.configuration.parse_with_nokogiri?
27
- nokogiri_matches(stringlike)
28
- else
29
- rexml_matches(stringlike)
30
- end
31
- end
32
-
33
- def rexml_matches(stringlike)
34
- if REXML::Node === stringlike || Array === stringlike
35
- @query = query.map { |q| q.gsub(%r'^//', './/') }
36
- else
37
- @query = query
38
- end
39
-
40
- add_options_conditions_to(@query)
41
-
42
- @document = Webrat.rexml_document(stringlike)
43
-
44
- @query.map do |q|
45
- if @document.is_a?(Array)
46
- @document.map { |d| REXML::XPath.match(d, q) }
47
- else
48
- REXML::XPath.match(@document, q)
49
- end
50
- end.flatten.compact
25
+ nokogiri_matches(stringlike)
51
26
  end
52
27
 
53
28
  def nokogiri_matches(stringlike)
@@ -16,10 +16,10 @@ module Webrat
16
16
  end
17
17
 
18
18
  def webrat_session
19
- if Webrat.configuration.mode == :rack_test
20
- @_webrat_session ||= ::Webrat::RackTestSession.new(rack_test_session)
21
- else
22
- @_webrat_session ||= ::Webrat.session_class.new(self)
19
+ @_webrat_session ||= begin
20
+ session = Webrat.session_class.new
21
+ session.adapter = Webrat.adapter_class.new(self) if session.respond_to?(:adapter=)
22
+ session
23
23
  end
24
24
  end
25
25
 
@@ -39,6 +39,7 @@ module Webrat
39
39
  :unchecks, :uncheck,
40
40
  :chooses, :choose,
41
41
  :selects, :select,
42
+ :unselects, :unselect,
42
43
  :attaches_file, :attach_file,
43
44
  :current_page,
44
45
  :current_url,
@@ -1,29 +1,18 @@
1
1
  module Webrat #:nodoc:
2
2
  module MIME #:nodoc:
3
+ MIME_TYPES = Rack::Mime::MIME_TYPES.dup.merge(
4
+ ".multipart_form" => "multipart/form-data",
5
+ ".url_encoded_form" => "application/x-www-form-urlencoded"
6
+ ).freeze
3
7
 
4
- def self.mime_type(string_or_symbol) #:nodoc:
5
- if string_or_symbol.is_a?(String)
6
- string_or_symbol
7
- else
8
- case string_or_symbol
9
- when :text then "text/plain"
10
- when :html then "text/html"
11
- when :js then "text/javascript"
12
- when :css then "text/css"
13
- when :ics then "text/calendar"
14
- when :csv then "text/csv"
15
- when :xml then "application/xml"
16
- when :rss then "application/rss+xml"
17
- when :atom then "application/atom+xml"
18
- when :yaml then "application/x-yaml"
19
- when :multipart_form then "multipart/form-data"
20
- when :url_encoded_form then "application/x-www-form-urlencoded"
21
- when :json then "application/json"
22
- else
23
- raise ArgumentError.new("Invalid Mime type: #{string_or_symbol.inspect}")
24
- end
25
- end
8
+ def mime_type(type)
9
+ return type if type.nil? || type.to_s.include?("/")
10
+ type = ".#{type}" unless type.to_s[0] == ?.
11
+ MIME_TYPES.fetch(type) { |type|
12
+ raise ArgumentError.new("Invalid Mime type: #{type}")
13
+ }
26
14
  end
27
15
 
16
+ module_function :mime_type
28
17
  end
29
18
  end
@@ -112,6 +112,21 @@ module Webrat
112
112
 
113
113
  webrat_deprecate :selects, :select
114
114
 
115
+ # Verifies that a an option element exists on the current page with the specified
116
+ # text. You can optionally restrict the search to a specific select list by
117
+ # assigning <tt>options[:from]</tt> the value of the select list's name or
118
+ # a label. Remove the option's value before the form is submitted.
119
+ #
120
+ # Examples:
121
+ # unselect "January"
122
+ # unselect "February", :from => "event_month"
123
+ # unselect "February", :from => "Event Month"
124
+ def unselect(option_text, options={})
125
+ select_option(option_text, options[:from]).unchoose
126
+ end
127
+
128
+ webrat_deprecate :unselects, :unselect
129
+
115
130
  DATE_TIME_SUFFIXES = {
116
131
  :year => '1i',
117
132
  :month => '2i',
@@ -312,12 +327,12 @@ module Webrat
312
327
  dom = Webrat::XML.html_document(@response_body)
313
328
  end
314
329
 
315
- Webrat.define_dom_method(@response, dom)
330
+ Webrat::XML.define_dom_method(@response, dom)
316
331
  return dom
317
332
  end
318
333
 
319
334
  def scoped_dom
320
- Webrat::XML.css_at(@scope.dom, @selector)
335
+ @scope.dom.css(@selector).first
321
336
  end
322
337
 
323
338
  def locate_field(field_locator, *field_types) #:nodoc:
@@ -13,21 +13,30 @@ module Webrat
13
13
  end
14
14
 
15
15
  def self.session_class
16
+ if Webrat.configuration.mode == :selenium
17
+ SeleniumSession
18
+ else
19
+ Session
20
+ end
21
+ end
22
+
23
+ def self.adapter_class
16
24
  case Webrat.configuration.mode
17
25
  when :rails
18
- RailsSession
26
+ RailsAdapter
19
27
  when :merb
20
- MerbSession
21
- when :selenium
22
- SeleniumSession
28
+ MerbAdapter
23
29
  when :rack
24
- RackSession
30
+ RackAdapter
31
+ when :rack_test
32
+ warn("The :rack_test mode is deprecated. Please use :rack instead")
33
+ require "webrat/rack"
34
+ RackAdapter
25
35
  when :sinatra
26
- SinatraSession
36
+ warn("The :sinatra mode is deprecated. Please use :rack instead")
37
+ SinatraAdapter
27
38
  when :mechanize
28
- MechanizeSession
29
- when :rack_test
30
- RackTestSession
39
+ MechanizeAdapter
31
40
  else
32
41
  raise WebratError.new(<<-STR)
33
42
  Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
@@ -50,16 +59,22 @@ For example:
50
59
  extend Forwardable
51
60
  include Logging
52
61
  include SaveAndOpenPage
62
+
63
+ attr_accessor :adapter
64
+
53
65
  attr_reader :current_url
54
66
  attr_reader :elements
55
67
 
56
- def initialize(context = nil) #:nodoc:
68
+ def_delegators :@adapter, :response, :response_code, :response_body,
69
+ :response_body=, :response_code=,
70
+ :get, :post, :put, :delete
71
+
72
+ def initialize(adapter = nil)
73
+ @adapter = adapter
57
74
  @http_method = :get
58
75
  @data = {}
59
76
  @default_headers = {}
60
77
  @custom_headers = {}
61
- @context = context
62
-
63
78
  reset
64
79
  end
65
80
 
@@ -245,6 +260,7 @@ For example:
245
260
  def_delegators :current_scope, :uncheck, :unchecks
246
261
  def_delegators :current_scope, :choose, :chooses
247
262
  def_delegators :current_scope, :select, :selects
263
+ def_delegators :current_scope, :unselect, :unselects
248
264
  def_delegators :current_scope, :select_datetime, :selects_datetime
249
265
  def_delegators :current_scope, :select_date, :selects_date
250
266
  def_delegators :current_scope, :select_time, :selects_time
@@ -1,115 +1,72 @@
1
- require "webrat/core/xml/nokogiri"
2
- require "webrat/core/xml/hpricot"
3
- require "webrat/core/xml/rexml"
1
+ require "webrat/core_extensions/meta_class"
4
2
 
5
3
  module Webrat #:nodoc:
6
4
  module XML #:nodoc:
7
5
 
8
6
  def self.document(stringlike) #:nodoc:
9
- if Webrat.configuration.parse_with_nokogiri?
10
- Webrat.nokogiri_document(stringlike)
7
+ return stringlike.dom if stringlike.respond_to?(:dom)
8
+
9
+ if Nokogiri::HTML::Document === stringlike
10
+ stringlike
11
+ elsif Nokogiri::XML::NodeSet === stringlike
12
+ stringlike
13
+ elsif stringlike.respond_to?(:body)
14
+ Nokogiri::HTML(stringlike.body.to_s)
11
15
  else
12
- Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
16
+ Nokogiri::HTML(stringlike.to_s)
13
17
  end
14
18
  end
15
19
 
16
20
  def self.html_document(stringlike) #:nodoc:
17
- if Webrat.configuration.parse_with_nokogiri?
18
- Webrat.html_nokogiri_document(stringlike)
21
+ return stringlike.dom if stringlike.respond_to?(:dom)
22
+
23
+ if Nokogiri::HTML::Document === stringlike
24
+ stringlike
25
+ elsif Nokogiri::XML::NodeSet === stringlike
26
+ stringlike
27
+ elsif stringlike.respond_to?(:body)
28
+ Nokogiri::HTML(stringlike.body.to_s)
19
29
  else
20
- Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
30
+ Nokogiri::HTML(stringlike.to_s)
21
31
  end
22
32
  end
23
33
 
24
34
  def self.xml_document(stringlike) #:nodoc:
25
- if Webrat.configuration.parse_with_nokogiri?
26
- Webrat.xml_nokogiri_document(stringlike)
35
+ return stringlike.dom if stringlike.respond_to?(:dom)
36
+
37
+ if Nokogiri::HTML::Document === stringlike
38
+ stringlike
39
+ elsif Nokogiri::XML::NodeSet === stringlike
40
+ stringlike
41
+ elsif stringlike.respond_to?(:body)
42
+ Nokogiri::XML(stringlike.body.to_s)
27
43
  else
28
- Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
44
+ Nokogiri::XML(stringlike.to_s)
29
45
  end
30
46
  end
31
47
 
32
- def self.to_html(element)
33
- if Webrat.configuration.parse_with_nokogiri?
34
- element.to_html
35
- else
36
- element.to_s
48
+ def self.define_dom_method(object, dom) #:nodoc:
49
+ object.meta_class.send(:define_method, :dom) do
50
+ dom
37
51
  end
38
52
  end
39
53
 
40
- def self.inner_html(element)
41
- if Webrat.configuration.parse_with_nokogiri?
42
- element.inner_html
43
- else
44
- element.text
45
- end
46
- end
47
-
48
- def self.all_inner_text(element)
49
- if Webrat.configuration.parse_with_nokogiri?
50
- element.inner_text
51
- else
52
- Hpricot(element.to_s).children.first.inner_text
53
- end
54
- end
54
+ end
55
+ end
55
56
 
56
- def self.inner_text(element)
57
- if Webrat.configuration.parse_with_nokogiri?
58
- element.inner_text
59
- else
60
- if defined?(Hpricot::Doc) && element.is_a?(Hpricot::Doc)
61
- element.inner_text
62
- else
63
- element.text
64
- end
65
- end
66
- end
57
+ module Nokogiri #:nodoc:
58
+ module CSS #:nodoc:
59
+ class XPathVisitor #:nodoc:
67
60
 
68
- def self.xpath_to(element)
69
- if Webrat.configuration.parse_with_nokogiri?
70
- element.path
71
- else
72
- element.xpath
61
+ def visit_pseudo_class_text(node) #:nodoc:
62
+ "@type='text'"
73
63
  end
74
- end
75
-
76
- def self.attribute(element, attribute_name)
77
- return element[attribute_name] if element.is_a?(Hash)
78
64
 
79
- if Webrat.configuration.parse_with_nokogiri?
80
- element[attribute_name]
81
- else
82
- element.attributes[attribute_name]
65
+ def visit_pseudo_class_password(node) #:nodoc:
66
+ "@type='password'"
83
67
  end
84
- end
85
-
86
- def self.xpath_at(*args)
87
- xpath_search(*args).first
88
- end
89
-
90
- def self.css_at(*args)
91
- css_search(*args).first
92
- end
93
-
94
- def self.xpath_search(element, *searches)
95
- searches.flatten.map do |search|
96
- if Webrat.configuration.parse_with_nokogiri?
97
- element.xpath(search)
98
- else
99
- REXML::XPath.match(element, search)
100
- end
101
- end.flatten.compact
102
- end
103
68
 
104
- def self.css_search(element, *searches) #:nodoc:
105
- xpath_search(element, css_to_xpath(*searches))
106
69
  end
107
-
108
- def self.css_to_xpath(*selectors)
109
- selectors.map do |rule|
110
- Nokogiri::CSS.xpath_for(rule, :prefix => ".//")
111
- end.flatten.uniq
112
- end
113
-
114
70
  end
115
71
  end
72
+
data/lib/webrat/core.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "webrat/core/configuration"
2
2
  require "webrat/core/xml"
3
- require "webrat/core/xml/nokogiri"
4
3
  require "webrat/core/logging"
5
4
  require "webrat/core/elements/form"
6
5
  require "webrat/core/scope"
@@ -0,0 +1,10 @@
1
+ module Merb #:nodoc:
2
+ module Test #:nodoc:
3
+ module RequestHelper #:nodoc:
4
+ def request(uri, env = {})
5
+ @_webrat_session ||= Webrat::MerbAdapter.new
6
+ @_webrat_session.response = @_webrat_session.request(uri, env)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ require "action_controller"
2
+ require "action_controller/integration"
3
+
4
+ module ActionController #:nodoc:
5
+ IntegrationTest.class_eval do
6
+ include Webrat::Methods
7
+ include Webrat::Matchers
8
+
9
+ # The Rails version of within supports passing in a model and Webrat
10
+ # will apply a scope based on Rails' dom_id for that model.
11
+ #
12
+ # Example:
13
+ # within User.last do
14
+ # click_link "Delete"
15
+ # end
16
+ def within(selector_or_object, &block)
17
+ if selector_or_object.is_a?(String)
18
+ super
19
+ else
20
+ super('#' + RecordIdentifier.dom_id(selector_or_object), &block)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ # Supports using the matchers in controller, helper, and view specs if you're
2
+ # using rspec-rails. Just add a require statement to spec/spec_helper.rb or env.rb:
3
+ #
4
+ # require 'webrat/rspec-rails'
5
+ #
6
+ require "webrat/core/matchers"
7
+
8
+ Spec::Runner.configure do |config|
9
+ config.include(Webrat::Matchers, :type => [:controller, :helper, :view])
10
+ end
@@ -0,0 +1,11 @@
1
+ require "webrat/selenium"
2
+
3
+ if defined?(ActionController::IntegrationTest)
4
+ module ActionController #:nodoc:
5
+ IntegrationTest.class_eval do
6
+ include Webrat::Methods
7
+ include Webrat::Selenium::Methods
8
+ include Webrat::Selenium::Matchers
9
+ end
10
+ end
11
+ end
@@ -1,10 +1,2 @@
1
- # Supports using the matchers in controller, helper, and view specs if you're
2
- # using rspec-rails. Just add a require statement to spec/spec_helper.rb or env.rb:
3
- #
4
- # require 'webrat/rspec-rails'
5
- #
6
- require "webrat/core/matchers"
7
-
8
- Spec::Runner.configure do |config|
9
- config.include(Webrat::Matchers, :type => [:controller, :helper, :view])
10
- end
1
+ warn("Requiring 'webrat/rspec-rails' is deprecated. Please require 'webrat/integrations/rspec-rails' instead")
2
+ require "webrat/integrations/rspec-rails"
@@ -23,4 +23,4 @@ module Webrat
23
23
  end
24
24
  end
25
25
  end
26
- end
26
+ end
@@ -2,4 +2,4 @@ require "webrat/selenium/application_servers/base"
2
2
  require "webrat/selenium/application_servers/sinatra"
3
3
  require "webrat/selenium/application_servers/merb"
4
4
  require "webrat/selenium/application_servers/rails"
5
- require "webrat/selenium/application_servers/external"
5
+ require "webrat/selenium/application_servers/external"
@@ -1,24 +1,42 @@
1
+ // Credit to: http://simonwillison.net/2006/Jan/20/escape/
2
+ RegExp.escape = function(text) {
3
+ if (!arguments.callee.sRE) {
4
+ var specials = [
5
+ '/', '.', '*', '+', '?', '|',
6
+ '(', ')', '[', ']', '{', '}', '\\'
7
+ ];
8
+ arguments.callee.sRE = new RegExp(
9
+ '(\\' + specials.join('|\\') + ')', 'g'
10
+ );
11
+ }
12
+ return text.replace(arguments.callee.sRE, '\\$1');
13
+ }
14
+
1
15
  var allLabels = inDocument.getElementsByTagName("label");
16
+ var regExp = new RegExp('^\\W*' + RegExp.escape(locator) + '(\\b|$)', 'i');
17
+
2
18
  var candidateLabels = $A(allLabels).select(function(candidateLabel){
3
- var regExp = new RegExp('^' + locator + '\\b', 'i');
4
19
  var labelText = getText(candidateLabel).strip();
5
20
  return (labelText.search(regExp) >= 0);
6
21
  });
22
+
7
23
  if (candidateLabels.length == 0) {
8
24
  return null;
9
25
  }
10
- candidateLabels = candidateLabels.sortBy(function(s) { return s.length * -1; }); //reverse length sort
26
+
27
+ //reverse length sort
28
+ candidateLabels = candidateLabels.sortBy(function(s) {
29
+ return s.length * -1;
30
+ });
31
+
11
32
  var locatedLabel = candidateLabels.first();
12
- var labelFor = locatedLabel.getAttribute('for') || locatedLabel.getAttribute('htmlFor');
13
- if((!labelFor || labelFor == "") && (locatedLabel.hasChildNodes())) {
14
- var inputTags = locatedLabel.getElementsByTagName("input");
15
- var textAreaTags = locatedLabel.getElementsByTagName("textarea");
16
- if(inputTags.length > 0) {
17
- return inputTags[0];
18
- } else if(textAreaTags.length > 0) {
19
- return textAreaTags[0];
20
- } else {
21
- return null;
22
- }
33
+ var labelFor = locatedLabel.getAttribute('for');
34
+
35
+ if ((labelFor == null) && (locatedLabel.hasChildNodes())) {
36
+ return locatedLabel.getElementsByTagName('button')[0]
37
+ || locatedLabel.getElementsByTagName('input')[0]
38
+ || locatedLabel.getElementsByTagName('textarea')[0]
39
+ || locatedLabel.getElementsByTagName('select')[0];
23
40
  }
41
+
24
42
  return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow);
@@ -1,4 +1,5 @@
1
1
  var locationStrategies = selenium.browserbot.locationStrategies;
2
+
2
3
  return locationStrategies['id'].call(this, locator, inDocument, inWindow)
3
4
  || locationStrategies['name'].call(this, locator, inDocument, inWindow)
4
5
  || locationStrategies['label'].call(this, locator, inDocument, inWindow)
@@ -1,12 +1,32 @@
1
1
  var links = inDocument.getElementsByTagName('a');
2
+
2
3
  var candidateLinks = $A(links).select(function(candidateLink) {
3
- var textMatched = PatternMatcher.matches(locator, getText(candidateLink));
4
- var idMatched = PatternMatcher.matches(locator, candidateLink.id);
5
- var titleMatched = PatternMatcher.matches(locator, candidateLink.title);
4
+ var textMatched = false;
5
+ var titleMatched = false;
6
+ var idMatched = false;
7
+
8
+ if (getText(candidateLink).toLowerCase().indexOf(locator.toLowerCase()) != -1) {
9
+ textMatched = true;
10
+ }
11
+
12
+ if (candidateLink.title.toLowerCase().indexOf(locator.toLowerCase()) != -1) {
13
+ titleMatched = true;
14
+ }
15
+
16
+ if (candidateLink.id.toLowerCase().indexOf(locator.toLowerCase()) != -1) {
17
+ idMatched = true;
18
+ }
19
+
6
20
  return textMatched || idMatched || titleMatched;
7
21
  });
22
+
8
23
  if (candidateLinks.length == 0) {
9
24
  return null;
10
25
  }
11
- candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort
26
+
27
+ //reverse length sort
28
+ candidateLinks = candidateLinks.sortBy(function(s) {
29
+ return s.length * -1;
30
+ });
31
+
12
32
  return candidateLinks.first();
@@ -34,7 +34,7 @@ module Webrat
34
34
 
35
35
  @remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0",
36
36
  Webrat.configuration.selenium_server_port,
37
- Webrat.configuration.selenium_browser_startup_timeout)
37
+ :timeout => Webrat.configuration.selenium_browser_startup_timeout)
38
38
  @remote_control.jar_file = jar_path
39
39
 
40
40
  return @remote_control
@@ -74,7 +74,9 @@ module Webrat
74
74
 
75
75
  def stop
76
76
  silence_stream(STDOUT) do
77
- ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5).stop
77
+ ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0",
78
+ Webrat.configuration.selenium_server_port,
79
+ :timeout => 5).stop
78
80
  end
79
81
  end
80
82
 
@@ -3,6 +3,8 @@ require "webrat/selenium/selenium_rc_server"
3
3
  require "webrat/selenium/application_server_factory"
4
4
  require "webrat/selenium/application_servers/base"
5
5
 
6
+ require "selenium"
7
+
6
8
  module Webrat
7
9
  class TimeoutError < WebratError
8
10
  end
@@ -77,7 +79,12 @@ module Webrat
77
79
  webrat_deprecate :clicks_button, :click_button
78
80
 
79
81
  def click_link(link_text_or_regexp, options = {})
80
- pattern = adjust_if_regexp(link_text_or_regexp)
82
+ if link_text_or_regexp.is_a?(Regexp)
83
+ pattern = "evalregex:#{link_text_or_regexp.inspect}"
84
+ else
85
+ pattern = link_text_or_regexp.to_s
86
+ end
87
+
81
88
  locator = "webratlink=#{pattern}"
82
89
  selenium.wait_for_element locator, :timeout_in_seconds => 5
83
90
  selenium.click locator
@@ -166,7 +173,19 @@ module Webrat
166
173
  sleep 0.25
167
174
  end
168
175
 
169
- raise Webrat::TimeoutError.new(message + " (after #{timeout} sec)")
176
+ error_message = "#{message} (after #{timeout} sec)"
177
+
178
+ if $browser
179
+ error_message += <<-EOS
180
+
181
+
182
+ HTML of the page was:
183
+
184
+ #{selenium.get_html_source}"
185
+ EOS
186
+ end
187
+
188
+ raise Webrat::TimeoutError.new(error_message)
170
189
  true
171
190
  end
172
191
 
@@ -15,4 +15,4 @@ module Webrat
15
15
  end
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -1,5 +1,4 @@
1
1
  require "webrat"
2
- gem "selenium-client", ">=1.2.14"
3
2
  require "selenium/client"
4
3
  require "webrat/selenium/silence_stream"
5
4
  require "webrat/selenium/selenium_session"
@@ -69,13 +68,3 @@ module Webrat
69
68
  end
70
69
  end
71
70
  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