hardbap-webrat 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. data/.document +4 -0
  2. data/.gitignore +15 -0
  3. data/History.txt +377 -0
  4. data/MIT-LICENSE.txt +19 -0
  5. data/README.rdoc +85 -0
  6. data/Rakefile +173 -0
  7. data/VERSION +1 -0
  8. data/install.rb +1 -0
  9. data/lib/webrat/core/configuration.rb +102 -0
  10. data/lib/webrat/core/elements/area.rb +31 -0
  11. data/lib/webrat/core/elements/element.rb +33 -0
  12. data/lib/webrat/core/elements/field.rb +411 -0
  13. data/lib/webrat/core/elements/form.rb +103 -0
  14. data/lib/webrat/core/elements/label.rb +31 -0
  15. data/lib/webrat/core/elements/link.rb +93 -0
  16. data/lib/webrat/core/elements/select_option.rb +35 -0
  17. data/lib/webrat/core/locators/area_locator.rb +38 -0
  18. data/lib/webrat/core/locators/button_locator.rb +54 -0
  19. data/lib/webrat/core/locators/field_by_id_locator.rb +37 -0
  20. data/lib/webrat/core/locators/field_labeled_locator.rb +56 -0
  21. data/lib/webrat/core/locators/field_locator.rb +25 -0
  22. data/lib/webrat/core/locators/field_named_locator.rb +41 -0
  23. data/lib/webrat/core/locators/form_locator.rb +19 -0
  24. data/lib/webrat/core/locators/label_locator.rb +34 -0
  25. data/lib/webrat/core/locators/link_locator.rb +74 -0
  26. data/lib/webrat/core/locators/locator.rb +20 -0
  27. data/lib/webrat/core/locators/select_option_locator.rb +59 -0
  28. data/lib/webrat/core/locators.rb +20 -0
  29. data/lib/webrat/core/logging.rb +24 -0
  30. data/lib/webrat/core/matchers/have_content.rb +73 -0
  31. data/lib/webrat/core/matchers/have_selector.rb +74 -0
  32. data/lib/webrat/core/matchers/have_tag.rb +21 -0
  33. data/lib/webrat/core/matchers/have_xpath.rb +147 -0
  34. data/lib/webrat/core/matchers.rb +4 -0
  35. data/lib/webrat/core/methods.rb +63 -0
  36. data/lib/webrat/core/mime.rb +18 -0
  37. data/lib/webrat/core/save_and_open_page.rb +48 -0
  38. data/lib/webrat/core/scope.rb +350 -0
  39. data/lib/webrat/core/session.rb +299 -0
  40. data/lib/webrat/core/xml/hpricot.rb +19 -0
  41. data/lib/webrat/core/xml/nokogiri.rb +76 -0
  42. data/lib/webrat/core/xml/rexml.rb +24 -0
  43. data/lib/webrat/core/xml.rb +115 -0
  44. data/lib/webrat/core.rb +14 -0
  45. data/lib/webrat/core_extensions/blank.rb +58 -0
  46. data/lib/webrat/core_extensions/deprecate.rb +8 -0
  47. data/lib/webrat/core_extensions/detect_mapped.rb +12 -0
  48. data/lib/webrat/core_extensions/meta_class.rb +6 -0
  49. data/lib/webrat/core_extensions/nil_to_param.rb +5 -0
  50. data/lib/webrat/core_extensions/tcp_socket.rb +27 -0
  51. data/lib/webrat/mechanize.rb +74 -0
  52. data/lib/webrat/merb.rb +9 -0
  53. data/lib/webrat/merb_multipart_support.rb +27 -0
  54. data/lib/webrat/merb_session.rb +82 -0
  55. data/lib/webrat/rack.rb +25 -0
  56. data/lib/webrat/rails.rb +106 -0
  57. data/lib/webrat/rspec-rails.rb +10 -0
  58. data/lib/webrat/selenium/application_server_factory.rb +40 -0
  59. data/lib/webrat/selenium/application_servers/base.rb +46 -0
  60. data/lib/webrat/selenium/application_servers/external.rb +26 -0
  61. data/lib/webrat/selenium/application_servers/merb.rb +50 -0
  62. data/lib/webrat/selenium/application_servers/rails.rb +44 -0
  63. data/lib/webrat/selenium/application_servers/sinatra.rb +37 -0
  64. data/lib/webrat/selenium/application_servers.rb +5 -0
  65. data/lib/webrat/selenium/location_strategy_javascript/button.js +19 -0
  66. data/lib/webrat/selenium/location_strategy_javascript/label.js +16 -0
  67. data/lib/webrat/selenium/location_strategy_javascript/webrat.js +5 -0
  68. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +12 -0
  69. data/lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js +15 -0
  70. data/lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js +5 -0
  71. data/lib/webrat/selenium/matchers/have_content.rb +66 -0
  72. data/lib/webrat/selenium/matchers/have_selector.rb +49 -0
  73. data/lib/webrat/selenium/matchers/have_tag.rb +72 -0
  74. data/lib/webrat/selenium/matchers/have_xpath.rb +45 -0
  75. data/lib/webrat/selenium/matchers.rb +4 -0
  76. data/lib/webrat/selenium/selenium_extensions.js +6 -0
  77. data/lib/webrat/selenium/selenium_rc_server.rb +86 -0
  78. data/lib/webrat/selenium/selenium_session.rb +248 -0
  79. data/lib/webrat/selenium/silence_stream.rb +18 -0
  80. data/lib/webrat/selenium.rb +80 -0
  81. data/lib/webrat/sinatra.rb +11 -0
  82. data/lib/webrat.rb +10 -0
  83. data/spec/fakes/test_session.rb +34 -0
  84. data/spec/integration/merb/.gitignore +21 -0
  85. data/spec/integration/merb/Rakefile +35 -0
  86. data/spec/integration/merb/app/controllers/application.rb +2 -0
  87. data/spec/integration/merb/app/controllers/exceptions.rb +13 -0
  88. data/spec/integration/merb/app/controllers/testing.rb +27 -0
  89. data/spec/integration/merb/app/views/exceptions/not_acceptable.html.erb +63 -0
  90. data/spec/integration/merb/app/views/exceptions/not_found.html.erb +47 -0
  91. data/spec/integration/merb/app/views/layout/application.html.erb +12 -0
  92. data/spec/integration/merb/app/views/testing/show_form.html.erb +27 -0
  93. data/spec/integration/merb/app/views/testing/upload.html.erb +9 -0
  94. data/spec/integration/merb/config/environments/development.rb +15 -0
  95. data/spec/integration/merb/config/environments/rake.rb +11 -0
  96. data/spec/integration/merb/config/environments/test.rb +14 -0
  97. data/spec/integration/merb/config/init.rb +25 -0
  98. data/spec/integration/merb/config/rack.rb +11 -0
  99. data/spec/integration/merb/config/router.rb +34 -0
  100. data/spec/integration/merb/spec/spec.opts +1 -0
  101. data/spec/integration/merb/spec/spec_helper.rb +24 -0
  102. data/spec/integration/merb/spec/webrat_spec.rb +39 -0
  103. data/spec/integration/merb/tasks/merb.thor/app_script.rb +31 -0
  104. data/spec/integration/merb/tasks/merb.thor/common.rb +64 -0
  105. data/spec/integration/merb/tasks/merb.thor/gem_ext.rb +124 -0
  106. data/spec/integration/merb/tasks/merb.thor/main.thor +150 -0
  107. data/spec/integration/merb/tasks/merb.thor/ops.rb +93 -0
  108. data/spec/integration/merb/tasks/merb.thor/utils.rb +40 -0
  109. data/spec/integration/rack/Rakefile +5 -0
  110. data/spec/integration/rack/app.rb +89 -0
  111. data/spec/integration/rack/test/helper.rb +20 -0
  112. data/spec/integration/rack/test/webrat_rack_test.rb +73 -0
  113. data/spec/integration/rails/.gitignore +3 -0
  114. data/spec/integration/rails/Rakefile +30 -0
  115. data/spec/integration/rails/app/controllers/application.rb +15 -0
  116. data/spec/integration/rails/app/controllers/buttons_controller.rb +7 -0
  117. data/spec/integration/rails/app/controllers/fields_controller.rb +4 -0
  118. data/spec/integration/rails/app/controllers/links_controller.rb +7 -0
  119. data/spec/integration/rails/app/controllers/webrat_controller.rb +43 -0
  120. data/spec/integration/rails/app/helpers/buttons_helper.rb +2 -0
  121. data/spec/integration/rails/app/helpers/fields_helper.rb +2 -0
  122. data/spec/integration/rails/app/helpers/links_helper.rb +2 -0
  123. data/spec/integration/rails/app/views/buttons/show.html.erb +11 -0
  124. data/spec/integration/rails/app/views/fields/show.html.erb +9 -0
  125. data/spec/integration/rails/app/views/links/show.html.erb +5 -0
  126. data/spec/integration/rails/app/views/webrat/before_redirect_form.html.erb +4 -0
  127. data/spec/integration/rails/app/views/webrat/buttons.html.erb +11 -0
  128. data/spec/integration/rails/app/views/webrat/form.html.erb +28 -0
  129. data/spec/integration/rails/config/boot.rb +109 -0
  130. data/spec/integration/rails/config/environment.rb +12 -0
  131. data/spec/integration/rails/config/environments/development.rb +17 -0
  132. data/spec/integration/rails/config/environments/selenium.rb +22 -0
  133. data/spec/integration/rails/config/environments/test.rb +22 -0
  134. data/spec/integration/rails/config/initializers/inflections.rb +10 -0
  135. data/spec/integration/rails/config/initializers/mime_types.rb +5 -0
  136. data/spec/integration/rails/config/initializers/new_rails_defaults.rb +17 -0
  137. data/spec/integration/rails/config/locales/en.yml +5 -0
  138. data/spec/integration/rails/config/routes.rb +18 -0
  139. data/spec/integration/rails/public/404.html +30 -0
  140. data/spec/integration/rails/public/422.html +30 -0
  141. data/spec/integration/rails/public/500.html +33 -0
  142. data/spec/integration/rails/script/about +4 -0
  143. data/spec/integration/rails/script/console +3 -0
  144. data/spec/integration/rails/script/dbconsole +3 -0
  145. data/spec/integration/rails/script/destroy +3 -0
  146. data/spec/integration/rails/script/generate +3 -0
  147. data/spec/integration/rails/script/performance/benchmarker +3 -0
  148. data/spec/integration/rails/script/performance/profiler +3 -0
  149. data/spec/integration/rails/script/performance/request +3 -0
  150. data/spec/integration/rails/script/plugin +3 -0
  151. data/spec/integration/rails/script/process/inspector +3 -0
  152. data/spec/integration/rails/script/process/reaper +3 -0
  153. data/spec/integration/rails/script/process/spawner +3 -0
  154. data/spec/integration/rails/script/runner +3 -0
  155. data/spec/integration/rails/script/server +3 -0
  156. data/spec/integration/rails/test/integration/button_click_test.rb +80 -0
  157. data/spec/integration/rails/test/integration/fill_in_test.rb +24 -0
  158. data/spec/integration/rails/test/integration/link_click_test.rb +27 -0
  159. data/spec/integration/rails/test/integration/webrat_test.rb +97 -0
  160. data/spec/integration/rails/test/test_helper.rb +25 -0
  161. data/spec/integration/sinatra/Rakefile +5 -0
  162. data/spec/integration/sinatra/classic_app.rb +64 -0
  163. data/spec/integration/sinatra/modular_app.rb +16 -0
  164. data/spec/integration/sinatra/test/classic_app_test.rb +37 -0
  165. data/spec/integration/sinatra/test/modular_app_test.rb +18 -0
  166. data/spec/integration/sinatra/test/test_helper.rb +16 -0
  167. data/spec/private/core/configuration_spec.rb +116 -0
  168. data/spec/private/core/field_spec.rb +85 -0
  169. data/spec/private/core/link_spec.rb +24 -0
  170. data/spec/private/core/logging_spec.rb +10 -0
  171. data/spec/private/core/session_spec.rb +200 -0
  172. data/spec/private/mechanize/mechanize_session_spec.rb +81 -0
  173. data/spec/private/merb/attaches_file_spec.rb +93 -0
  174. data/spec/private/merb/merb_session_spec.rb +61 -0
  175. data/spec/private/nokogiri_spec.rb +77 -0
  176. data/spec/private/rails/attaches_file_spec.rb +81 -0
  177. data/spec/private/rails/rails_session_spec.rb +112 -0
  178. data/spec/private/selenium/application_servers/rails_spec.rb +26 -0
  179. data/spec/public/basic_auth_spec.rb +24 -0
  180. data/spec/public/check_spec.rb +191 -0
  181. data/spec/public/choose_spec.rb +118 -0
  182. data/spec/public/click_area_spec.rb +106 -0
  183. data/spec/public/click_button_spec.rb +496 -0
  184. data/spec/public/click_link_spec.rb +511 -0
  185. data/spec/public/fill_in_spec.rb +209 -0
  186. data/spec/public/locators/field_by_xpath_spec.rb +19 -0
  187. data/spec/public/locators/field_labeled_spec.rb +172 -0
  188. data/spec/public/locators/field_with_id_spec.rb +16 -0
  189. data/spec/public/matchers/contain_spec.rb +114 -0
  190. data/spec/public/matchers/have_selector_spec.rb +142 -0
  191. data/spec/public/matchers/have_tag_spec.rb +39 -0
  192. data/spec/public/matchers/have_xpath_spec.rb +136 -0
  193. data/spec/public/reload_spec.rb +10 -0
  194. data/spec/public/save_and_open_spec.rb +70 -0
  195. data/spec/public/select_date_spec.rb +112 -0
  196. data/spec/public/select_datetime_spec.rb +137 -0
  197. data/spec/public/select_spec.rb +249 -0
  198. data/spec/public/select_time_spec.rb +100 -0
  199. data/spec/public/selenium/application_server_factory_spec.rb +49 -0
  200. data/spec/public/selenium/application_servers/external_spec.rb +12 -0
  201. data/spec/public/selenium/selenium_session_spec.rb +37 -0
  202. data/spec/public/set_hidden_field_spec.rb +5 -0
  203. data/spec/public/submit_form_spec.rb +5 -0
  204. data/spec/public/visit_spec.rb +58 -0
  205. data/spec/public/within_spec.rb +177 -0
  206. data/spec/rcov.opts +1 -0
  207. data/spec/spec.opts +2 -0
  208. data/spec/spec_helper.rb +52 -0
  209. data/vendor/selenium-server.jar +0 -0
  210. data/webrat.gemspec +345 -0
  211. metadata +360 -0
@@ -0,0 +1,73 @@
1
+ module Webrat
2
+ module Matchers
3
+
4
+ class HasContent #:nodoc:
5
+ def initialize(content)
6
+ @content = content
7
+ end
8
+
9
+ def matches?(stringlike)
10
+ if Webrat.configuration.parse_with_nokogiri?
11
+ @document = Webrat.nokogiri_document(stringlike)
12
+ else
13
+ @document = Webrat.hpricot_document(stringlike)
14
+ end
15
+
16
+ @element = Webrat::XML.inner_text(@document)
17
+
18
+ case @content
19
+ when String
20
+ @element.include?(@content)
21
+ when Regexp
22
+ @element.match(@content)
23
+ end
24
+ end
25
+
26
+ # ==== Returns
27
+ # String:: The failure message.
28
+ def failure_message
29
+ "expected the following element's content to #{content_message}:\n#{squeeze_space(@element)}"
30
+ end
31
+
32
+ # ==== Returns
33
+ # String:: The failure message to be displayed in negative matches.
34
+ def negative_failure_message
35
+ "expected the following element's content to not #{content_message}:\n#{squeeze_space(@element)}"
36
+ end
37
+
38
+ def squeeze_space(inner_text)
39
+ inner_text.gsub(/^\s*$/, "").squeeze("\n")
40
+ end
41
+
42
+ def content_message
43
+ case @content
44
+ when String
45
+ "include \"#{@content}\""
46
+ when Regexp
47
+ "match #{@content.inspect}"
48
+ end
49
+ end
50
+ end
51
+
52
+ # Matches the contents of an HTML document with
53
+ # whatever string is supplied
54
+ def contain(content)
55
+ HasContent.new(content)
56
+ end
57
+
58
+ # Asserts that the body of the response contain
59
+ # the supplied string or regexp
60
+ def assert_contain(content)
61
+ hc = HasContent.new(content)
62
+ assert hc.matches?(response_body), hc.failure_message
63
+ end
64
+
65
+ # Asserts that the body of the response
66
+ # does not contain the supplied string or regepx
67
+ def assert_not_contain(content)
68
+ hc = HasContent.new(content)
69
+ assert !hc.matches?(response_body), hc.negative_failure_message
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,74 @@
1
+ require "webrat/core/matchers/have_xpath"
2
+
3
+ module Webrat
4
+ module Matchers
5
+
6
+ class HaveSelector < HaveXpath #:nodoc:
7
+ # ==== Returns
8
+ # String:: The failure message.
9
+ def failure_message
10
+ "expected following output to contain a #{tag_inspect} tag:\n#{@document}"
11
+ end
12
+
13
+ # ==== Returns
14
+ # String:: The failure message to be displayed in negative matches.
15
+ def negative_failure_message
16
+ "expected following output to omit a #{tag_inspect}:\n#{@document}"
17
+ end
18
+
19
+ def tag_inspect
20
+ options = @options.dup
21
+ count = options.delete(:count)
22
+ content = options.delete(:content)
23
+
24
+ html = "<#{@expected}"
25
+ options.each do |k,v|
26
+ html << " #{k}='#{v}'"
27
+ end
28
+
29
+ if content
30
+ html << ">#{content}</#{@expected}>"
31
+ else
32
+ html << "/>"
33
+ end
34
+
35
+ html
36
+ end
37
+
38
+ def query
39
+ Nokogiri::CSS.parse(@expected.to_s).map do |ast|
40
+ ast.to_xpath
41
+ end.first
42
+ end
43
+
44
+ end
45
+
46
+ # Matches HTML content against a CSS 3 selector.
47
+ #
48
+ # ==== Parameters
49
+ # expected<String>:: The CSS selector to look for.
50
+ #
51
+ # ==== Returns
52
+ # HaveSelector:: A new have selector matcher.
53
+ def have_selector(name, attributes = {}, &block)
54
+ HaveSelector.new(name, attributes, &block)
55
+ end
56
+ alias_method :match_selector, :have_selector
57
+
58
+
59
+ # Asserts that the body of the response contains
60
+ # the supplied selector
61
+ def assert_have_selector(name, attributes = {}, &block)
62
+ matcher = HaveSelector.new(name, attributes, &block)
63
+ assert matcher.matches?(response_body), matcher.failure_message
64
+ end
65
+
66
+ # Asserts that the body of the response
67
+ # does not contain the supplied string or regepx
68
+ def assert_have_no_selector(name, attributes = {}, &block)
69
+ matcher = HaveSelector.new(name, attributes, &block)
70
+ assert !matcher.matches?(response_body), matcher.negative_failure_message
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,21 @@
1
+ require "webrat/core/matchers/have_selector"
2
+
3
+ module Webrat
4
+ module HaveTagMatcher
5
+
6
+ def have_tag(*args, &block)
7
+ have_selector(*args, &block)
8
+ end
9
+
10
+ alias_method :match_tag, :have_tag
11
+
12
+ def assert_have_tag(*args, &block)
13
+ assert_have_selector(*args, &block)
14
+ end
15
+
16
+ def assert_have_no_tag(*args, &block)
17
+ assert_have_no_selector(*args, &block)
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,147 @@
1
+ require "webrat/core/xml/nokogiri"
2
+ require "webrat/core/xml/rexml"
3
+
4
+ module Webrat
5
+ module Matchers
6
+
7
+ class HaveXpath #:nodoc:
8
+ def initialize(expected, options = {}, &block)
9
+ @expected = expected
10
+ @options = options
11
+ @block = block
12
+ end
13
+
14
+ def matches?(stringlike, &block)
15
+ @block ||= block
16
+ matched = matches(stringlike)
17
+
18
+ if @options[:count]
19
+ matched.size == @options[:count] && (!@block || @block.call(matched))
20
+ else
21
+ matched.any? && (!@block || @block.call(matched))
22
+ end
23
+ end
24
+
25
+ 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
51
+ end
52
+
53
+ def nokogiri_matches(stringlike)
54
+ if Nokogiri::XML::NodeSet === stringlike
55
+ @query = query.gsub(%r'^//', './/')
56
+ else
57
+ @query = query
58
+ end
59
+
60
+ add_options_conditions_to(@query)
61
+
62
+ @document = Webrat::XML.document(stringlike)
63
+ @document.xpath(*@query)
64
+ end
65
+
66
+ def add_options_conditions_to(query)
67
+ add_attributes_conditions_to(query)
68
+ add_content_condition_to(query)
69
+ end
70
+
71
+ def add_attributes_conditions_to(query)
72
+ attribute_conditions = []
73
+
74
+ @options.each do |key, value|
75
+ next if [:content, :count].include?(key)
76
+ attribute_conditions << "@#{key} = #{xpath_escape(value)}"
77
+ end
78
+
79
+ if attribute_conditions.any?
80
+ query << "[#{attribute_conditions.join(' and ')}]"
81
+ end
82
+ end
83
+
84
+ def add_content_condition_to(query)
85
+ if @options[:content]
86
+ query << "[contains(., #{xpath_escape(@options[:content])})]"
87
+ end
88
+ end
89
+
90
+ def query
91
+ @expected
92
+ end
93
+
94
+ # ==== Returns
95
+ # String:: The failure message.
96
+ def failure_message
97
+ "expected following text to match xpath #{@expected}:\n#{@document}"
98
+ end
99
+
100
+ # ==== Returns
101
+ # String:: The failure message to be displayed in negative matches.
102
+ def negative_failure_message
103
+ "expected following text to not match xpath #{@expected}:\n#{@document}"
104
+ end
105
+
106
+ protected
107
+
108
+ def xpath_escape(string)
109
+ if string.include?("'") && string.include?('"')
110
+ parts = string.split("'").map do |part|
111
+ "'#{part}'"
112
+ end
113
+
114
+ "concat(" + parts.join(", \"'\", ") + ")"
115
+ elsif string.include?("'")
116
+ "\"#{string}\""
117
+ else
118
+ "'#{string}'"
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ # Matches HTML content against an XPath query
125
+ #
126
+ # ==== Parameters
127
+ # expected<String>:: The XPath query to look for.
128
+ #
129
+ # ==== Returns
130
+ # HaveXpath:: A new have xpath matcher.
131
+ def have_xpath(expected, options = {}, &block)
132
+ HaveXpath.new(expected, options, &block)
133
+ end
134
+ alias_method :match_xpath, :have_xpath
135
+
136
+ def assert_have_xpath(expected, options = {}, &block)
137
+ hs = HaveXpath.new(expected, options, &block)
138
+ assert hs.matches?(response_body), hs.failure_message
139
+ end
140
+
141
+ def assert_have_no_xpath(expected, options = {}, &block)
142
+ hs = HaveXpath.new(expected, options, &block)
143
+ assert !hs.matches?(response_body), hs.negative_failure_message
144
+ end
145
+
146
+ end
147
+ end
@@ -0,0 +1,4 @@
1
+ require "webrat/core/matchers/have_xpath"
2
+ require "webrat/core/matchers/have_selector"
3
+ require "webrat/core/matchers/have_tag"
4
+ require "webrat/core/matchers/have_content"
@@ -0,0 +1,63 @@
1
+ module Webrat
2
+ module Methods #:nodoc:
3
+
4
+ def self.delegate_to_session(*meths)
5
+ meths.each do |meth|
6
+ self.class_eval <<-RUBY
7
+ def #{meth}(*args, &blk)
8
+ webrat_adapter.#{meth}(*args, &blk)
9
+ end
10
+ RUBY
11
+ end
12
+ end
13
+
14
+ def webrat
15
+ webrat_session
16
+ end
17
+
18
+ def webrat_session
19
+ @_webrat_session ||= ::Webrat::Session.new(webrat_adapter)
20
+ end
21
+
22
+ def webrat_adapter
23
+ @_webrat_adapter ||= Webrat.session_class.new(self)
24
+ end
25
+
26
+ # all of these methods delegate to the @session, which should
27
+ # be created transparently.
28
+ #
29
+ # Note that when using Webrat, #request also uses @session, so
30
+ # that #request and webrat native functions behave interchangably
31
+
32
+ delegate_to_session \
33
+ :visits, :visit,
34
+ :within,
35
+ :header, :http_accept, :basic_auth,
36
+ :save_and_open_page,
37
+ :fills_in, :fill_in,
38
+ :checks, :check,
39
+ :unchecks, :uncheck,
40
+ :chooses, :choose,
41
+ :selects, :select,
42
+ :attaches_file, :attach_file,
43
+ :current_page,
44
+ :current_url,
45
+ :clicks_link, :click_link,
46
+ :clicks_area, :click_area,
47
+ :clicks_button, :click_button,
48
+ :reload, :reloads,
49
+ :clicks_link_within, :click_link_within,
50
+ :field_labeled,
51
+ :select_option,
52
+ :set_hidden_field, :submit_form,
53
+ :request_page, :current_dom,
54
+ :response_body,
55
+ :selects_date, :selects_time, :selects_datetime,
56
+ :select_date, :select_time, :select_datetime,
57
+ :field_by_xpath,
58
+ :field_with_id,
59
+ :selenium,
60
+ :simulate, :automate,
61
+ :field_named
62
+ end
63
+ end
@@ -0,0 +1,18 @@
1
+ module Webrat #:nodoc:
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
7
+
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
+ }
14
+ end
15
+
16
+ module_function :mime_type
17
+ end
18
+ end
@@ -0,0 +1,48 @@
1
+ module Webrat
2
+ module SaveAndOpenPage
3
+ # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
4
+ # web browser if on OS X. Useful for debugging.
5
+ #
6
+ # Example:
7
+ # save_and_open_page
8
+ def save_and_open_page
9
+ return unless File.exist?(saved_page_dir)
10
+
11
+ filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html"
12
+
13
+ File.open(filename, "w") do |f|
14
+ f.write rewrite_css_and_image_references(response_body)
15
+ end
16
+
17
+ open_in_browser(filename)
18
+ end
19
+
20
+ def open_in_browser(path) # :nodoc
21
+ require "launchy"
22
+ Launchy::Browser.run(path)
23
+ rescue LoadError
24
+ warn "Sorry, you need to install launchy to open pages: `gem install launchy`"
25
+ end
26
+
27
+ def rewrite_css_and_image_references(response_html) # :nodoc:
28
+ return response_html unless doc_root
29
+ response_html.gsub(/("|')\/(stylesheets|images)/, '\1' + doc_root + '/\2')
30
+ end
31
+
32
+ def saved_page_dir #:nodoc:
33
+ File.expand_path(".")
34
+ end
35
+
36
+ def doc_root #:nodoc:
37
+ nil
38
+ end
39
+
40
+ private
41
+
42
+ # accessor for testing
43
+ def ruby_platform
44
+ RUBY_PLATFORM
45
+ end
46
+
47
+ end
48
+ end