aslakhellesoy-webrat 0.3.2.2 → 0.4.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/History.txt +158 -19
  2. data/Rakefile +36 -11
  3. data/lib/webrat.rb +8 -11
  4. data/lib/webrat/core/configuration.rb +67 -25
  5. data/lib/webrat/core/elements/area.rb +7 -7
  6. data/lib/webrat/core/elements/element.rb +11 -11
  7. data/lib/webrat/core/elements/field.rb +70 -51
  8. data/lib/webrat/core/elements/form.rb +17 -17
  9. data/lib/webrat/core/elements/label.rb +8 -8
  10. data/lib/webrat/core/elements/link.rb +13 -11
  11. data/lib/webrat/core/elements/select_option.rb +9 -9
  12. data/lib/webrat/core/locators.rb +2 -2
  13. data/lib/webrat/core/locators/area_locator.rb +10 -10
  14. data/lib/webrat/core/locators/button_locator.rb +13 -13
  15. data/lib/webrat/core/locators/field_by_id_locator.rb +8 -8
  16. data/lib/webrat/core/locators/field_labeled_locator.rb +19 -13
  17. data/lib/webrat/core/locators/field_locator.rb +7 -7
  18. data/lib/webrat/core/locators/field_named_locator.rb +10 -10
  19. data/lib/webrat/core/locators/form_locator.rb +6 -6
  20. data/lib/webrat/core/locators/label_locator.rb +9 -9
  21. data/lib/webrat/core/locators/link_locator.rb +12 -12
  22. data/lib/webrat/core/locators/locator.rb +5 -5
  23. data/lib/webrat/core/locators/select_option_locator.rb +11 -11
  24. data/lib/webrat/core/logging.rb +7 -4
  25. data/lib/webrat/core/matchers/have_content.rb +19 -15
  26. data/lib/webrat/core/matchers/have_selector.rb +44 -22
  27. data/lib/webrat/core/matchers/have_tag.rb +11 -61
  28. data/lib/webrat/core/matchers/have_xpath.rb +89 -35
  29. data/lib/webrat/core/methods.rb +13 -11
  30. data/lib/webrat/core/mime.rb +3 -3
  31. data/lib/webrat/core/save_and_open_page.rb +9 -9
  32. data/lib/webrat/core/scope.rb +82 -62
  33. data/lib/webrat/core/session.rb +61 -18
  34. data/lib/webrat/core/xml.rb +16 -16
  35. data/lib/webrat/core/xml/hpricot.rb +3 -3
  36. data/lib/webrat/core/xml/nokogiri.rb +14 -14
  37. data/lib/webrat/core/xml/rexml.rb +3 -3
  38. data/lib/webrat/core_extensions/blank.rb +1 -1
  39. data/lib/webrat/core_extensions/deprecate.rb +1 -1
  40. data/lib/webrat/core_extensions/detect_mapped.rb +4 -4
  41. data/lib/webrat/core_extensions/meta_class.rb +1 -1
  42. data/lib/webrat/core_extensions/tcp_socket.rb +27 -0
  43. data/lib/webrat/mechanize.rb +9 -9
  44. data/lib/webrat/merb.rb +5 -61
  45. data/lib/webrat/merb_session.rb +67 -0
  46. data/lib/webrat/rack.rb +45 -14
  47. data/lib/webrat/rails.rb +31 -11
  48. data/lib/webrat/rspec-rails.rb +2 -2
  49. data/lib/webrat/selenium.rb +27 -39
  50. data/lib/webrat/selenium/application_server_factory.rb +40 -0
  51. data/lib/webrat/selenium/application_servers.rb +5 -0
  52. data/lib/webrat/selenium/application_servers/base.rb +44 -0
  53. data/lib/webrat/selenium/application_servers/external.rb +24 -0
  54. data/lib/webrat/selenium/application_servers/merb.rb +48 -0
  55. data/lib/webrat/selenium/application_servers/rails.rb +42 -0
  56. data/lib/webrat/selenium/application_servers/sinatra.rb +35 -0
  57. data/lib/webrat/selenium/location_strategy_javascript/button.js +14 -7
  58. data/lib/webrat/selenium/location_strategy_javascript/label.js +1 -2
  59. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +4 -1
  60. data/lib/webrat/selenium/matchers.rb +4 -108
  61. data/lib/webrat/selenium/matchers/have_content.rb +66 -0
  62. data/lib/webrat/selenium/matchers/have_selector.rb +49 -0
  63. data/lib/webrat/selenium/matchers/have_tag.rb +72 -0
  64. data/lib/webrat/selenium/matchers/have_xpath.rb +45 -0
  65. data/lib/webrat/selenium/selenium_rc_server.rb +84 -0
  66. data/lib/webrat/selenium/selenium_session.rb +86 -68
  67. data/lib/webrat/selenium/silence_stream.rb +18 -0
  68. metadata +21 -16
  69. data/lib/webrat/core_extensions/hash_with_indifferent_access.rb +0 -131
  70. data/lib/webrat/sinatra.rb +0 -29
@@ -9,6 +9,9 @@ module Webrat
9
9
  class PageLoadError < WebratError
10
10
  end
11
11
 
12
+ class InfiniteRedirectError < WebratError
13
+ end
14
+
12
15
  def self.session_class
13
16
  case Webrat.configuration.mode
14
17
  when :rails
@@ -17,18 +20,19 @@ module Webrat
17
20
  MerbSession
18
21
  when :selenium
19
22
  SeleniumSession
20
- when :rack
21
- RackSession
22
- when :sinatra
23
- SinatraSession
24
23
  when :mechanize
25
24
  MechanizeSession
25
+ when :rack, :sinatra
26
+ RackSession
26
27
  else
27
28
  raise WebratError.new(<<-STR)
28
29
  Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
29
30
 
30
31
  Please ensure you have a Webrat configuration block that specifies a mode
31
32
  in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
33
+
34
+ This configure block supercedes the need to require "webrat/<framework>".
35
+
32
36
  For example:
33
37
 
34
38
  Webrat.configure do |config|
@@ -45,12 +49,16 @@ For example:
45
49
  attr_reader :current_url
46
50
  attr_reader :elements
47
51
 
48
- def initialize(context = nil) #:nodoc:
52
+ def_delegators :@adapter, :response, :request,
53
+ :response_code, :response_body, :response_body=, :response_code=,
54
+ :get, :post, :put, :delete
55
+
56
+ def initialize(adapter=nil)
49
57
  @http_method = :get
50
58
  @data = {}
51
59
  @default_headers = {}
52
60
  @custom_headers = {}
53
- @context = context
61
+ @adapter = adapter
54
62
 
55
63
  reset
56
64
  end
@@ -61,6 +69,7 @@ For example:
61
69
 
62
70
  # For backwards compatibility -- removing in 1.0
63
71
  def current_page #:nodoc:
72
+ warn "current_page is deprecated and will be going away in the next release. Use current_url instead."
64
73
  page = OpenStruct.new
65
74
  page.url = @current_url
66
75
  page.http_method = @http_method
@@ -71,7 +80,7 @@ For example:
71
80
  def doc_root #:nodoc:
72
81
  nil
73
82
  end
74
-
83
+
75
84
  def header(key, value)
76
85
  @custom_headers[key] = value
77
86
  end
@@ -94,11 +103,8 @@ For example:
94
103
  h['HTTP_REFERER'] = @current_url if @current_url
95
104
 
96
105
  debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
97
- if h.empty?
98
- send "#{http_method}", url, data || {}
99
- else
100
- send "#{http_method}", url, data || {}, h
101
- end
106
+
107
+ process_request(http_method, url, data, h)
102
108
 
103
109
  save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
104
110
  raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
@@ -109,11 +115,30 @@ For example:
109
115
  @http_method = http_method
110
116
  @data = data
111
117
 
112
- request_page(response_location, :get, data) if internal_redirect?
118
+ if internal_redirect?
119
+ check_for_infinite_redirects
120
+ request_page(response_location, :get, {})
121
+ end
113
122
 
114
123
  return response
115
124
  end
116
125
 
126
+ def check_for_infinite_redirects
127
+ if current_url == response_location
128
+ @_identical_redirect_count ||= 0
129
+ @_identical_redirect_count += 1
130
+ end
131
+
132
+ if infinite_redirect_limit_exceeded?
133
+ raise InfiniteRedirectError.new("#{Webrat.configuration.infinite_redirect_limit} redirects to the same URL (#{current_url.inspect})")
134
+ end
135
+ end
136
+
137
+ def infinite_redirect_limit_exceeded?
138
+ Webrat.configuration.infinite_redirect_limit &&
139
+ (@_identical_redirect_count || 0) > Webrat.configuration.infinite_redirect_limit
140
+ end
141
+
117
142
  def success_code? #:nodoc:
118
143
  (200..499).include?(response_code)
119
144
  end
@@ -122,8 +147,17 @@ For example:
122
147
  response_code / 100 == 3
123
148
  end
124
149
 
125
- def internal_redirect? #:nodoc:
126
- redirect? && current_host == response_location_host
150
+ def internal_redirect?
151
+ return false unless redirect?
152
+ #should keep internal_redirects if the subdomain changes
153
+ current_host_domain = current_host.split('.')[-2..-1].join('.') rescue current_host
154
+ response_location_host_domain = response_location_host.split('.')[-2..-1].join('.') rescue response_location_host
155
+ current_host_domain == response_location_host_domain
156
+ end
157
+
158
+ #easy helper to pull out where we were redirected to
159
+ def redirected_to
160
+ redirect? ? response_location : nil
127
161
  end
128
162
 
129
163
  def exception_caught? #:nodoc:
@@ -136,11 +170,11 @@ For example:
136
170
 
137
171
  # Reloads the last page requested. Note that this will resubmit forms
138
172
  # and their data.
139
- def reloads
173
+ def reload
140
174
  request_page(@current_url, @http_method, @data)
141
175
  end
142
176
 
143
- webrat_deprecate :reload, :reloads
177
+ webrat_deprecate :reloads, :reload
144
178
 
145
179
 
146
180
  # Works like click_link, but only looks for the link text within a given selector
@@ -222,9 +256,18 @@ For example:
222
256
  def_delegators :current_scope, :field_by_xpath
223
257
  def_delegators :current_scope, :field_with_id
224
258
  def_delegators :current_scope, :select_option
259
+ def_delegators :current_scope, :field_named
225
260
 
226
261
  private
227
262
 
263
+ def process_request(http_method, url, data, headers)
264
+ if headers.empty?
265
+ send "#{http_method}", url, data || {}
266
+ else
267
+ send "#{http_method}", url, data || {}, headers
268
+ end
269
+ end
270
+
228
271
  def response_location
229
272
  response.headers["Location"]
230
273
  end
@@ -242,6 +285,6 @@ For example:
242
285
  @_scopes = nil
243
286
  @_page_scope = nil
244
287
  end
245
-
288
+
246
289
  end
247
290
  end
@@ -4,7 +4,7 @@ require "webrat/core/xml/rexml"
4
4
 
5
5
  module Webrat #:nodoc:
6
6
  module XML #:nodoc:
7
-
7
+
8
8
  def self.document(stringlike) #:nodoc:
9
9
  if Webrat.configuration.parse_with_nokogiri?
10
10
  Webrat.nokogiri_document(stringlike)
@@ -12,7 +12,7 @@ module Webrat #:nodoc:
12
12
  Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
13
13
  end
14
14
  end
15
-
15
+
16
16
  def self.html_document(stringlike) #:nodoc:
17
17
  if Webrat.configuration.parse_with_nokogiri?
18
18
  Webrat.html_nokogiri_document(stringlike)
@@ -20,7 +20,7 @@ module Webrat #:nodoc:
20
20
  Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
21
21
  end
22
22
  end
23
-
23
+
24
24
  def self.xml_document(stringlike) #:nodoc:
25
25
  if Webrat.configuration.parse_with_nokogiri?
26
26
  Webrat.xml_nokogiri_document(stringlike)
@@ -36,7 +36,7 @@ module Webrat #:nodoc:
36
36
  element.to_s
37
37
  end
38
38
  end
39
-
39
+
40
40
  def self.inner_html(element)
41
41
  if Webrat.configuration.parse_with_nokogiri?
42
42
  element.inner_html
@@ -44,7 +44,7 @@ module Webrat #:nodoc:
44
44
  element.text
45
45
  end
46
46
  end
47
-
47
+
48
48
  def self.all_inner_text(element)
49
49
  if Webrat.configuration.parse_with_nokogiri?
50
50
  element.inner_text
@@ -52,7 +52,7 @@ module Webrat #:nodoc:
52
52
  Hpricot(element.to_s).children.first.inner_text
53
53
  end
54
54
  end
55
-
55
+
56
56
  def self.inner_text(element)
57
57
  if Webrat.configuration.parse_with_nokogiri?
58
58
  element.inner_text
@@ -64,7 +64,7 @@ module Webrat #:nodoc:
64
64
  end
65
65
  end
66
66
  end
67
-
67
+
68
68
  def self.xpath_to(element)
69
69
  if Webrat.configuration.parse_with_nokogiri?
70
70
  element.path
@@ -72,25 +72,25 @@ module Webrat #:nodoc:
72
72
  element.xpath
73
73
  end
74
74
  end
75
-
75
+
76
76
  def self.attribute(element, attribute_name)
77
77
  return element[attribute_name] if element.is_a?(Hash)
78
-
78
+
79
79
  if Webrat.configuration.parse_with_nokogiri?
80
80
  element[attribute_name]
81
81
  else
82
82
  element.attributes[attribute_name]
83
83
  end
84
84
  end
85
-
85
+
86
86
  def self.xpath_at(*args)
87
87
  xpath_search(*args).first
88
88
  end
89
-
89
+
90
90
  def self.css_at(*args)
91
91
  css_search(*args).first
92
92
  end
93
-
93
+
94
94
  def self.xpath_search(element, *searches)
95
95
  searches.flatten.map do |search|
96
96
  if Webrat.configuration.parse_with_nokogiri?
@@ -100,16 +100,16 @@ module Webrat #:nodoc:
100
100
  end
101
101
  end.flatten.compact
102
102
  end
103
-
103
+
104
104
  def self.css_search(element, *searches) #:nodoc:
105
105
  xpath_search(element, css_to_xpath(*searches))
106
106
  end
107
-
107
+
108
108
  def self.css_to_xpath(*selectors)
109
109
  selectors.map do |rule|
110
110
  Nokogiri::CSS.xpath_for(rule, :prefix => ".//")
111
111
  end.flatten.uniq
112
112
  end
113
-
113
+
114
114
  end
115
- end
115
+ end
@@ -1,5 +1,5 @@
1
1
  module Webrat
2
-
2
+
3
3
  def self.hpricot_document(stringlike)
4
4
  return stringlike.dom if stringlike.respond_to?(:dom)
5
5
 
@@ -15,5 +15,5 @@ module Webrat
15
15
  Hpricot(stringlike.to_s)
16
16
  end
17
17
  end
18
-
19
- end
18
+
19
+ end
@@ -1,26 +1,26 @@
1
1
  require "webrat/core_extensions/meta_class"
2
2
 
3
3
  module Webrat
4
-
4
+
5
5
  def self.nokogiri_document(stringlike) #:nodoc:
6
6
  return stringlike.dom if stringlike.respond_to?(:dom)
7
-
7
+
8
8
  if Nokogiri::HTML::Document === stringlike
9
9
  stringlike
10
10
  elsif Nokogiri::XML::NodeSet === stringlike
11
11
  stringlike
12
12
  elsif StringIO === stringlike
13
- Nokogiri.parse(stringlike.string)
13
+ Nokogiri::HTML(stringlike.string)
14
14
  elsif stringlike.respond_to?(:body)
15
- Nokogiri.parse(stringlike.body.to_s)
15
+ Nokogiri::HTML(stringlike.body.to_s)
16
16
  else
17
- Nokogiri.parse(stringlike.to_s)
17
+ Nokogiri::HTML(stringlike.to_s)
18
18
  end
19
19
  end
20
-
20
+
21
21
  def self.html_nokogiri_document(stringlike) #:nodoc:
22
22
  return stringlike.dom if stringlike.respond_to?(:dom)
23
-
23
+
24
24
  if Nokogiri::HTML::Document === stringlike
25
25
  stringlike
26
26
  elsif Nokogiri::XML::NodeSet === stringlike
@@ -33,10 +33,10 @@ module Webrat
33
33
  Nokogiri::HTML(stringlike.to_s)
34
34
  end
35
35
  end
36
-
36
+
37
37
  def self.xml_nokogiri_document(stringlike) #:nodoc:
38
38
  return stringlike.dom if stringlike.respond_to?(:dom)
39
-
39
+
40
40
  if Nokogiri::HTML::Document === stringlike
41
41
  stringlike
42
42
  elsif Nokogiri::XML::NodeSet === stringlike
@@ -49,20 +49,20 @@ module Webrat
49
49
  Nokogiri::XML(stringlike.to_s)
50
50
  end
51
51
  end
52
-
52
+
53
53
  def self.define_dom_method(object, dom) #:nodoc:
54
54
  object.meta_class.send(:define_method, :dom) do
55
55
  dom
56
56
  end
57
57
  end
58
-
58
+
59
59
  end
60
60
 
61
61
 
62
62
  module Nokogiri #:nodoc:
63
63
  module CSS #:nodoc:
64
64
  class XPathVisitor #:nodoc:
65
-
65
+
66
66
  def visit_pseudo_class_text(node) #:nodoc:
67
67
  "@type='text'"
68
68
  end
@@ -70,7 +70,7 @@ module Nokogiri #:nodoc:
70
70
  def visit_pseudo_class_password(node) #:nodoc:
71
71
  "@type='password'"
72
72
  end
73
-
73
+
74
74
  end
75
75
  end
76
- end
76
+ end
@@ -1,5 +1,5 @@
1
1
  module Webrat
2
-
2
+
3
3
  def self.rexml_document(stringlike)
4
4
  stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
5
5
 
@@ -20,5 +20,5 @@ module Webrat
20
20
  end
21
21
  end
22
22
  end
23
-
24
- end
23
+
24
+ end
@@ -12,7 +12,7 @@ class Object #:nodoc:
12
12
  def blank?
13
13
  respond_to?(:empty?) ? empty? : !self
14
14
  end
15
-
15
+
16
16
  # An object is present if it's not blank.
17
17
  def present?
18
18
  !blank?
@@ -5,4 +5,4 @@ class Module #:nodoc:
5
5
  __send__(new_method_name, *args)
6
6
  end
7
7
  end
8
- end
8
+ end
@@ -1,12 +1,12 @@
1
1
  class Array #:nodoc:
2
-
2
+
3
3
  def detect_mapped
4
4
  each do |element|
5
5
  result = yield element
6
6
  return result if result
7
7
  end
8
-
8
+
9
9
  return nil
10
10
  end
11
-
12
- end
11
+
12
+ end
@@ -3,4 +3,4 @@ class ::Object #:nodoc:
3
3
  class << self; self end
4
4
  end
5
5
  end
6
-
6
+
@@ -0,0 +1,27 @@
1
+ class TCPSocket
2
+
3
+ def self.wait_for_service_with_timeout(options)
4
+ start_time = Time.now
5
+
6
+ until listening_service?(options)
7
+ verbose_wait
8
+
9
+ if options[:timeout] && (Time.now > start_time + options[:timeout])
10
+ raise SocketError.new("Socket did not open within #{options[:timeout]} seconds")
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.wait_for_service_termination_with_timeout(options)
16
+ start_time = Time.now
17
+
18
+ while listening_service?(options)
19
+ verbose_wait
20
+
21
+ if options[:timeout] && (Time.now > start_time + options[:timeout])
22
+ raise SocketError.new("Socket did not terminate within #{options[:timeout]} seconds")
23
+ end
24
+ end
25
+ end
26
+
27
+ end