auxesis-webrat 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/History.txt +264 -0
  2. data/MIT-LICENSE.txt +19 -0
  3. data/README.rdoc +85 -0
  4. data/Rakefile +151 -0
  5. data/install.rb +1 -0
  6. data/lib/webrat.rb +34 -0
  7. data/lib/webrat/core.rb +14 -0
  8. data/lib/webrat/core/configuration.rb +89 -0
  9. data/lib/webrat/core/elements/area.rb +31 -0
  10. data/lib/webrat/core/elements/element.rb +33 -0
  11. data/lib/webrat/core/elements/field.rb +404 -0
  12. data/lib/webrat/core/elements/form.rb +132 -0
  13. data/lib/webrat/core/elements/label.rb +31 -0
  14. data/lib/webrat/core/elements/link.rb +90 -0
  15. data/lib/webrat/core/elements/select_option.rb +35 -0
  16. data/lib/webrat/core/locators.rb +20 -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 +66 -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/logging.rb +21 -0
  29. data/lib/webrat/core/matchers.rb +4 -0
  30. data/lib/webrat/core/matchers/have_content.rb +69 -0
  31. data/lib/webrat/core/matchers/have_selector.rb +52 -0
  32. data/lib/webrat/core/matchers/have_tag.rb +71 -0
  33. data/lib/webrat/core/matchers/have_xpath.rb +93 -0
  34. data/lib/webrat/core/methods.rb +61 -0
  35. data/lib/webrat/core/mime.rb +29 -0
  36. data/lib/webrat/core/save_and_open_page.rb +50 -0
  37. data/lib/webrat/core/scope.rb +350 -0
  38. data/lib/webrat/core/session.rb +259 -0
  39. data/lib/webrat/core/xml.rb +115 -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_extensions/blank.rb +58 -0
  44. data/lib/webrat/core_extensions/deprecate.rb +8 -0
  45. data/lib/webrat/core_extensions/detect_mapped.rb +12 -0
  46. data/lib/webrat/core_extensions/hash_with_indifferent_access.rb +131 -0
  47. data/lib/webrat/core_extensions/meta_class.rb +6 -0
  48. data/lib/webrat/core_extensions/nil_to_param.rb +5 -0
  49. data/lib/webrat/mechanize.rb +74 -0
  50. data/lib/webrat/merb.rb +9 -0
  51. data/lib/webrat/merb_session.rb +73 -0
  52. data/lib/webrat/rack.rb +24 -0
  53. data/lib/webrat/rails.rb +105 -0
  54. data/lib/webrat/rspec-rails.rb +13 -0
  55. data/lib/webrat/selenium.rb +99 -0
  56. data/lib/webrat/selenium/location_strategy_javascript/button.js +12 -0
  57. data/lib/webrat/selenium/location_strategy_javascript/label.js +16 -0
  58. data/lib/webrat/selenium/location_strategy_javascript/webrat.js +5 -0
  59. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +9 -0
  60. data/lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js +15 -0
  61. data/lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js +5 -0
  62. data/lib/webrat/selenium/matchers.rb +4 -0
  63. data/lib/webrat/selenium/selenium_extensions.js +6 -0
  64. data/lib/webrat/selenium/selenium_session.rb +237 -0
  65. data/lib/webrat/sinatra.rb +30 -0
  66. data/vendor/selenium-server.jar +0 -0
  67. metadata +136 -0
@@ -0,0 +1,259 @@
1
+ require "forwardable"
2
+ require "ostruct"
3
+
4
+ require "webrat/core/mime"
5
+ require "webrat/core/save_and_open_page"
6
+
7
+ module Webrat
8
+ # A page load or form submission returned an unsuccessful response code (500-599)
9
+ class PageLoadError < WebratError
10
+ end
11
+
12
+ def self.session_class
13
+ case Webrat.configuration.mode
14
+ when :rails
15
+ RailsSession
16
+ when :merb
17
+ MerbSession
18
+ when :selenium
19
+ SeleniumSession
20
+ when :rack
21
+ RackSession
22
+ when :sinatra
23
+ SinatraSession
24
+ when :mechanize
25
+ MechanizeSession
26
+ else
27
+ raise WebratError.new(<<-STR)
28
+ Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
29
+
30
+ Please ensure you have a Webrat configuration block that specifies a mode
31
+ in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
32
+
33
+ This configure block supercedes the need to require "webrat/<framework>".
34
+
35
+ For example:
36
+
37
+ Webrat.configure do |config|
38
+ config.mode = :rails
39
+ end
40
+ STR
41
+ end
42
+ end
43
+
44
+ class Session
45
+ extend Forwardable
46
+ include Logging
47
+ include SaveAndOpenPage
48
+ attr_reader :current_url
49
+ attr_reader :elements
50
+
51
+ def initialize(context = nil) #:nodoc:
52
+ @http_method = :get
53
+ @data = {}
54
+ @default_headers = {}
55
+ @custom_headers = {}
56
+ @context = context
57
+
58
+ reset
59
+ end
60
+
61
+ def current_dom #:nodoc:
62
+ current_scope.dom
63
+ end
64
+
65
+ # For backwards compatibility -- removing in 1.0
66
+ def current_page #:nodoc:
67
+ page = OpenStruct.new
68
+ page.url = @current_url
69
+ page.http_method = @http_method
70
+ page.data = @data
71
+ page
72
+ end
73
+
74
+ def doc_root #:nodoc:
75
+ nil
76
+ end
77
+
78
+ def header(key, value)
79
+ @custom_headers[key] = value
80
+ end
81
+
82
+ def http_accept(mime_type)
83
+ header('Accept', Webrat::MIME.mime_type(mime_type))
84
+ end
85
+
86
+ def basic_auth(user, pass)
87
+ encoded_login = ["#{user}:#{pass}"].pack("m*")
88
+ header('HTTP_AUTHORIZATION', "Basic #{encoded_login}")
89
+ end
90
+
91
+ def headers #:nodoc:
92
+ @default_headers.dup.merge(@custom_headers.dup)
93
+ end
94
+
95
+ def request_page(url, http_method, data) #:nodoc:
96
+ h = headers
97
+ h['HTTP_REFERER'] = @current_url if @current_url
98
+
99
+ debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
100
+ if h.empty?
101
+ send "#{http_method}", url, data || {}
102
+ else
103
+ send "#{http_method}", url, data || {}, h
104
+ end
105
+
106
+ save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
107
+ raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
108
+
109
+ reset
110
+
111
+ @current_url = url
112
+ @http_method = http_method
113
+ @data = data
114
+
115
+ request_page(response_location, :get, {}) if internal_redirect?
116
+
117
+ return response
118
+ end
119
+
120
+ def success_code? #:nodoc:
121
+ (200..499).include?(response_code)
122
+ end
123
+
124
+ def redirect? #:nodoc:
125
+ response_code / 100 == 3
126
+ end
127
+
128
+ def internal_redirect?
129
+ return false unless redirect?
130
+ #should keep internal_redirects if the subdomain changes
131
+ current_host_domain = current_host.split('.')[-2..-1].join('.') rescue current_host
132
+ response_location_host_domain = response_location_host.split('.')[-2..-1].join('.') rescue response_location_host
133
+ current_host_domain == response_location_host_domain
134
+ end
135
+
136
+ #easy helper to pull out where we were redirected to
137
+ def redirected_to
138
+ redirect? ? response_location : nil
139
+ end
140
+
141
+ def exception_caught? #:nodoc:
142
+ response_body =~ /Exception caught/
143
+ end
144
+
145
+ def current_scope #:nodoc:
146
+ scopes.last || page_scope
147
+ end
148
+
149
+ # Reloads the last page requested. Note that this will resubmit forms
150
+ # and their data.
151
+ def reload
152
+ request_page(@current_url, @http_method, @data)
153
+ end
154
+
155
+ webrat_deprecate :reloads, :reload
156
+
157
+
158
+ # Works like click_link, but only looks for the link text within a given selector
159
+ #
160
+ # Example:
161
+ # click_link_within "#user_12", "Vote"
162
+ def click_link_within(selector, link_text)
163
+ within(selector) do
164
+ click_link(link_text)
165
+ end
166
+ end
167
+
168
+ webrat_deprecate :clicks_link_within, :click_link_within
169
+
170
+ def within(selector)
171
+ scopes.push(Scope.from_scope(self, current_scope, selector))
172
+ ret = yield(current_scope)
173
+ scopes.pop
174
+ return ret
175
+ end
176
+
177
+ # Issues a GET request for a page, follows any redirects, and verifies the final page
178
+ # load was successful.
179
+ #
180
+ # Example:
181
+ # visit "/"
182
+ def visit(url = nil, http_method = :get, data = {})
183
+ request_page(url, http_method, data)
184
+ end
185
+
186
+ webrat_deprecate :visits, :visit
187
+
188
+ # Subclasses can override this to show error messages without html
189
+ def formatted_error #:nodoc:
190
+ response_body
191
+ end
192
+
193
+ def scopes #:nodoc:
194
+ @_scopes ||= []
195
+ end
196
+
197
+ def page_scope #:nodoc:
198
+ @_page_scope ||= Scope.from_page(self, response, response_body)
199
+ end
200
+
201
+ def dom
202
+ page_scope.dom
203
+ end
204
+
205
+ def xml_content_type?
206
+ false
207
+ end
208
+
209
+ def simulate
210
+ return if Webrat.configuration.mode == :selenium
211
+ yield
212
+ end
213
+
214
+ def automate
215
+ return unless Webrat.configuration.mode == :selenium
216
+ yield
217
+ end
218
+
219
+ def_delegators :current_scope, :fill_in, :fills_in
220
+ def_delegators :current_scope, :set_hidden_field
221
+ def_delegators :current_scope, :submit_form
222
+ def_delegators :current_scope, :check, :checks
223
+ def_delegators :current_scope, :uncheck, :unchecks
224
+ def_delegators :current_scope, :choose, :chooses
225
+ def_delegators :current_scope, :select, :selects
226
+ def_delegators :current_scope, :select_datetime, :selects_datetime
227
+ def_delegators :current_scope, :select_date, :selects_date
228
+ def_delegators :current_scope, :select_time, :selects_time
229
+ def_delegators :current_scope, :attach_file, :attaches_file
230
+ def_delegators :current_scope, :click_area, :clicks_area
231
+ def_delegators :current_scope, :click_link, :clicks_link
232
+ def_delegators :current_scope, :click_button, :clicks_button
233
+ def_delegators :current_scope, :field_labeled
234
+ def_delegators :current_scope, :field_by_xpath
235
+ def_delegators :current_scope, :field_with_id
236
+ def_delegators :current_scope, :select_option
237
+
238
+ private
239
+
240
+ def response_location
241
+ response.headers["Location"]
242
+ end
243
+
244
+ def current_host
245
+ URI.parse(current_url).host || "www.example.com"
246
+ end
247
+
248
+ def response_location_host
249
+ URI.parse(response_location).host || "www.example.com"
250
+ end
251
+
252
+ def reset
253
+ @elements = {}
254
+ @_scopes = nil
255
+ @_page_scope = nil
256
+ end
257
+
258
+ end
259
+ end
@@ -0,0 +1,115 @@
1
+ require "webrat/core/xml/nokogiri"
2
+ require "webrat/core/xml/hpricot"
3
+ require "webrat/core/xml/rexml"
4
+
5
+ module Webrat #:nodoc:
6
+ module XML #:nodoc:
7
+
8
+ def self.document(stringlike) #:nodoc:
9
+ if Webrat.configuration.parse_with_nokogiri?
10
+ Webrat.nokogiri_document(stringlike)
11
+ else
12
+ Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
13
+ end
14
+ end
15
+
16
+ def self.html_document(stringlike) #:nodoc:
17
+ if Webrat.configuration.parse_with_nokogiri?
18
+ Webrat.html_nokogiri_document(stringlike)
19
+ else
20
+ Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
21
+ end
22
+ end
23
+
24
+ def self.xml_document(stringlike) #:nodoc:
25
+ if Webrat.configuration.parse_with_nokogiri?
26
+ Webrat.xml_nokogiri_document(stringlike)
27
+ else
28
+ Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
29
+ end
30
+ end
31
+
32
+ def self.to_html(element)
33
+ if Webrat.configuration.parse_with_nokogiri?
34
+ element.to_html
35
+ else
36
+ element.to_s
37
+ end
38
+ end
39
+
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
55
+
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
67
+
68
+ def self.xpath_to(element)
69
+ if Webrat.configuration.parse_with_nokogiri?
70
+ element.path
71
+ else
72
+ element.xpath
73
+ end
74
+ end
75
+
76
+ def self.attribute(element, attribute_name)
77
+ return element[attribute_name] if element.is_a?(Hash)
78
+
79
+ if Webrat.configuration.parse_with_nokogiri?
80
+ element[attribute_name]
81
+ else
82
+ element.attributes[attribute_name]
83
+ 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
+
104
+ def self.css_search(element, *searches) #:nodoc:
105
+ xpath_search(element, css_to_xpath(*searches))
106
+ 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
+ end
115
+ end
@@ -0,0 +1,19 @@
1
+ module Webrat
2
+
3
+ def self.hpricot_document(stringlike)
4
+ return stringlike.dom if stringlike.respond_to?(:dom)
5
+
6
+ if Hpricot::Doc === stringlike
7
+ stringlike
8
+ elsif Hpricot::Elements === stringlike
9
+ stringlike
10
+ elsif StringIO === stringlike
11
+ Hpricot(stringlike.string)
12
+ elsif stringlike.respond_to?(:body)
13
+ Hpricot(stringlike.body.to_s)
14
+ else
15
+ Hpricot(stringlike.to_s)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,76 @@
1
+ require "webrat/core_extensions/meta_class"
2
+
3
+ module Webrat
4
+
5
+ def self.nokogiri_document(stringlike) #:nodoc:
6
+ return stringlike.dom if stringlike.respond_to?(:dom)
7
+
8
+ if Nokogiri::HTML::Document === stringlike
9
+ stringlike
10
+ elsif Nokogiri::XML::NodeSet === stringlike
11
+ stringlike
12
+ elsif StringIO === stringlike
13
+ Nokogiri::HTML(stringlike.string)
14
+ elsif stringlike.respond_to?(:body)
15
+ Nokogiri::HTML(stringlike.body.to_s)
16
+ else
17
+ Nokogiri::HTML(stringlike.to_s)
18
+ end
19
+ end
20
+
21
+ def self.html_nokogiri_document(stringlike) #:nodoc:
22
+ return stringlike.dom if stringlike.respond_to?(:dom)
23
+
24
+ if Nokogiri::HTML::Document === stringlike
25
+ stringlike
26
+ elsif Nokogiri::XML::NodeSet === stringlike
27
+ stringlike
28
+ elsif StringIO === stringlike
29
+ Nokogiri::HTML(stringlike.string)
30
+ elsif stringlike.respond_to?(:body)
31
+ Nokogiri::HTML(stringlike.body.to_s)
32
+ else
33
+ Nokogiri::HTML(stringlike.to_s)
34
+ end
35
+ end
36
+
37
+ def self.xml_nokogiri_document(stringlike) #:nodoc:
38
+ return stringlike.dom if stringlike.respond_to?(:dom)
39
+
40
+ if Nokogiri::HTML::Document === stringlike
41
+ stringlike
42
+ elsif Nokogiri::XML::NodeSet === stringlike
43
+ stringlike
44
+ elsif StringIO === stringlike
45
+ Nokogiri::XML(stringlike.string)
46
+ elsif stringlike.respond_to?(:body)
47
+ Nokogiri::XML(stringlike.body.to_s)
48
+ else
49
+ Nokogiri::XML(stringlike.to_s)
50
+ end
51
+ end
52
+
53
+ def self.define_dom_method(object, dom) #:nodoc:
54
+ object.meta_class.send(:define_method, :dom) do
55
+ dom
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+
62
+ module Nokogiri #:nodoc:
63
+ module CSS #:nodoc:
64
+ class XPathVisitor #:nodoc:
65
+
66
+ def visit_pseudo_class_text(node) #:nodoc:
67
+ "@type='text'"
68
+ end
69
+
70
+ def visit_pseudo_class_password(node) #:nodoc:
71
+ "@type='password'"
72
+ end
73
+
74
+ end
75
+ end
76
+ end