webrat 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. data/.gitignore +4 -1
  2. data/Gemfile +19 -0
  3. data/History.txt +29 -0
  4. data/Rakefile +27 -63
  5. data/Thorfile +117 -0
  6. data/lib/webrat.rb +11 -3
  7. data/lib/webrat/{mechanize.rb → adapters/mechanize.rb} +0 -0
  8. data/lib/webrat/adapters/merb.rb +11 -0
  9. data/lib/webrat/{rack.rb → adapters/rack.rb} +0 -0
  10. data/lib/webrat/{rails.rb → adapters/rails.rb} +2 -23
  11. data/lib/webrat/{sinatra.rb → adapters/sinatra.rb} +0 -2
  12. data/lib/webrat/core.rb +0 -1
  13. data/lib/webrat/core/configuration.rb +6 -16
  14. data/lib/webrat/core/elements/area.rb +2 -2
  15. data/lib/webrat/core/elements/element.rb +3 -3
  16. data/lib/webrat/core/elements/field.rb +106 -30
  17. data/lib/webrat/core/elements/form.rb +4 -4
  18. data/lib/webrat/core/elements/label.rb +4 -4
  19. data/lib/webrat/core/elements/link.rb +6 -6
  20. data/lib/webrat/core/elements/select_option.rb +15 -2
  21. data/lib/webrat/core/locators.rb +1 -1
  22. data/lib/webrat/core/locators/area_locator.rb +3 -3
  23. data/lib/webrat/core/locators/button_locator.rb +6 -6
  24. data/lib/webrat/core/locators/field_by_id_locator.rb +3 -3
  25. data/lib/webrat/core/locators/field_labeled_locator.rb +2 -2
  26. data/lib/webrat/core/locators/field_named_locator.rb +3 -3
  27. data/lib/webrat/core/locators/form_locator.rb +1 -1
  28. data/lib/webrat/core/locators/label_locator.rb +2 -2
  29. data/lib/webrat/core/locators/link_locator.rb +7 -7
  30. data/lib/webrat/core/locators/select_option_locator.rb +5 -5
  31. data/lib/webrat/core/logging.rb +4 -5
  32. data/lib/webrat/core/matchers/have_content.rb +2 -7
  33. data/lib/webrat/core/matchers/have_xpath.rb +3 -28
  34. data/lib/webrat/core/methods.rb +1 -0
  35. data/lib/webrat/core/scope.rb +17 -2
  36. data/lib/webrat/core/session.rb +2 -1
  37. data/lib/webrat/core/xml.rb +41 -84
  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 +11 -0
  41. data/lib/webrat/integrations/selenium.rb +11 -0
  42. data/lib/webrat/rspec-rails.rb +2 -10
  43. data/lib/webrat/selenium.rb +0 -10
  44. data/lib/webrat/selenium/application_servers.rb +1 -1
  45. data/lib/webrat/selenium/application_servers/external.rb +1 -1
  46. data/lib/webrat/selenium/location_strategy_javascript/label.js +29 -3
  47. data/lib/webrat/selenium/location_strategy_javascript/webrat.js +1 -0
  48. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +24 -4
  49. data/lib/webrat/selenium/selenium_rc_server.rb +2 -2
  50. data/lib/webrat/selenium/selenium_session.rb +21 -2
  51. data/lib/webrat/selenium/silence_stream.rb +1 -1
  52. data/spec/integration/mechanize/spec/spec_helper.rb +3 -1
  53. data/spec/integration/rails/app/controllers/{application.rb → application_controller.rb} +0 -0
  54. data/spec/integration/rails/app/controllers/webrat_controller.rb +3 -0
  55. data/spec/integration/rails/app/views/buttons/show.html.erb +0 -2
  56. data/spec/integration/rails/app/views/webrat/buttons.html.erb +0 -2
  57. data/spec/integration/rails/app/views/webrat/within.html.erb +3 -0
  58. data/spec/integration/rails/config/environment.rb +1 -1
  59. data/spec/integration/rails/config/routes.rb +1 -0
  60. data/spec/integration/rails/test/integration/button_click_test.rb +12 -26
  61. data/spec/integration/rails/test/integration/fill_in_test.rb +1 -1
  62. data/spec/integration/rails/test/integration/link_click_test.rb +1 -1
  63. data/spec/integration/rails/test/integration/webrat_test.rb +35 -9
  64. data/spec/integration/rails/test/test_helper.rb +1 -0
  65. data/spec/private/core/configuration_spec.rb +2 -31
  66. data/spec/private/core/field_spec.rb +14 -16
  67. data/spec/private/mechanize/mechanize_adapter_spec.rb +0 -2
  68. data/spec/private/nokogiri_spec.rb +2 -2
  69. data/spec/private/rails/rails_adapter_spec.rb +0 -27
  70. data/spec/public/basic_auth_spec.rb +13 -2
  71. data/spec/public/click_button_spec.rb +10 -12
  72. data/spec/public/click_link_spec.rb +21 -0
  73. data/spec/public/fill_in_spec.rb +15 -0
  74. data/spec/public/matchers/have_selector_spec.rb +4 -0
  75. data/spec/public/select_spec.rb +232 -26
  76. data/spec/spec_helper.rb +2 -0
  77. data/webrat.gemspec +305 -313
  78. metadata +21 -25
  79. data/VERSION +0 -1
  80. data/lib/webrat/core/xml/hpricot.rb +0 -19
  81. data/lib/webrat/core/xml/nokogiri.rb +0 -76
  82. data/lib/webrat/core/xml/rexml.rb +0 -24
  83. data/lib/webrat/merb_adapter.rb +0 -82
  84. data/lib/webrat/merb_multipart_support.rb +0 -27
  85. data/spec/private/core/logging_spec.rb +0 -10
  86. data/spec/private/merb/attaches_file_spec.rb +0 -93
  87. data/spec/private/merb/merb_adapter_spec.rb +0 -61
@@ -20,24 +20,24 @@ module Webrat
20
20
  end
21
21
 
22
22
  def matches_id?(element)
23
- (@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) ||
24
- (!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s)
23
+ (@value.is_a?(Regexp) && element["id"] =~ @value) ||
24
+ (!@value.is_a?(Regexp) && element["id"] == @value.to_s)
25
25
  end
26
26
 
27
27
  def matches_value?(element)
28
- Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
28
+ element["value"] =~ /^\W*#{Regexp.escape(@value.to_s)}/i
29
29
  end
30
30
 
31
31
  def matches_html?(element)
32
- Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i
32
+ element.inner_html =~ /#{Regexp.escape(@value.to_s)}/i
33
33
  end
34
34
 
35
35
  def matches_alt?(element)
36
- Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
36
+ element["alt"] =~ /^\W*#{Regexp.escape(@value.to_s)}/i
37
37
  end
38
38
 
39
39
  def button_elements
40
- Webrat::XML.xpath_search(@dom, *ButtonField.xpath_search)
40
+ @dom.xpath(*ButtonField.xpath_search)
41
41
  end
42
42
 
43
43
  def error_message
@@ -12,15 +12,15 @@ module Webrat
12
12
  def field_element
13
13
  field_elements.detect do |field_element|
14
14
  if @value.is_a?(Regexp)
15
- Webrat::XML.attribute(field_element, "id") =~ @value
15
+ field_element["id"] =~ @value
16
16
  else
17
- Webrat::XML.attribute(field_element, "id") == @value.to_s
17
+ field_element["id"] == @value.to_s
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
22
  def field_elements
23
- Webrat::XML.xpath_search(@dom, *Field.xpath_search)
23
+ @dom.xpath(*Field.xpath_search)
24
24
  end
25
25
 
26
26
  def error_message
@@ -25,7 +25,7 @@ module Webrat
25
25
  end
26
26
 
27
27
  def label_elements
28
- Webrat::XML.xpath_search(@dom, Label.xpath_search)
28
+ @dom.xpath(*Label.xpath_search)
29
29
  end
30
30
 
31
31
  def error_message
@@ -33,7 +33,7 @@ module Webrat
33
33
  end
34
34
 
35
35
  def text(element)
36
- str = Webrat::XML.all_inner_text(element)
36
+ str = element.inner_text
37
37
  str.gsub!("\n","")
38
38
  str.strip!
39
39
  str.squeeze!(" ")
@@ -11,19 +11,19 @@ module Webrat
11
11
 
12
12
  def field_element
13
13
  field_elements.detect do |field_element|
14
- Webrat::XML.attribute(field_element, "name") == @value.to_s
14
+ field_element["name"] == @value.to_s
15
15
  end
16
16
  end
17
17
 
18
18
  def field_elements
19
- Webrat::XML.xpath_search(@dom, *xpath_searches)
19
+ @dom.xpath(*xpath_searches)
20
20
  end
21
21
 
22
22
  def xpath_searches
23
23
  if @field_types.any?
24
24
  @field_types.map { |field_type| field_type.xpath_search }.flatten
25
25
  else
26
- Array(Field.xpath_search)
26
+ Field.xpath_search
27
27
  end
28
28
  end
29
29
 
@@ -10,7 +10,7 @@ module Webrat
10
10
  end
11
11
 
12
12
  def form_element
13
- Webrat::XML.css_at(@dom, "#" + @value)
13
+ @dom.css("#" + @value).first
14
14
  end
15
15
 
16
16
  end
@@ -17,11 +17,11 @@ module Webrat
17
17
  end
18
18
 
19
19
  def label_elements
20
- Webrat::XML.xpath_search(@dom, Label.xpath_search)
20
+ @dom.xpath(*Label.xpath_search)
21
21
  end
22
22
 
23
23
  def text(label_element)
24
- str = Webrat::XML.all_inner_text(label_element)
24
+ str = label_element.inner_text
25
25
  str.gsub!("\n","")
26
26
  str.strip!
27
27
  str.squeeze!(" ")
@@ -10,7 +10,7 @@ module Webrat
10
10
  end
11
11
 
12
12
  def link_element
13
- matching_links.min { |a, b| Webrat::XML.all_inner_text(a).length <=> Webrat::XML.all_inner_text(b).length }
13
+ matching_links.min { |a, b| a.inner_text.length <=> b.inner_text.length }
14
14
  end
15
15
 
16
16
  def matching_links
@@ -27,21 +27,21 @@ module Webrat
27
27
  matcher = /#{Regexp.escape(@value.to_s)}/i
28
28
  end
29
29
 
30
- replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher ||
31
- replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher ||
32
- Webrat::XML.attribute(link, "title")=~ matcher
30
+ replace_nbsp(link.inner_text) =~ matcher ||
31
+ replace_nbsp_ref(link.inner_html) =~ matcher ||
32
+ link["title"] =~ matcher
33
33
  end
34
34
 
35
35
  def matches_id?(link)
36
36
  if @value.is_a?(Regexp)
37
- (Webrat::XML.attribute(link, "id") =~ @value) ? true : false
37
+ link["id"] =~ @value ? true : false
38
38
  else
39
- (Webrat::XML.attribute(link, "id") == @value) ? true : false
39
+ link["id"] == @value ? true : false
40
40
  end
41
41
  end
42
42
 
43
43
  def link_elements
44
- Webrat::XML.xpath_search(@dom, *Link.xpath_search)
44
+ @dom.xpath(*Link.xpath_search)
45
45
  end
46
46
 
47
47
  def replace_nbsp(str)
@@ -19,17 +19,17 @@ module Webrat
19
19
 
20
20
  field.options.detect do |o|
21
21
  if @option_text.is_a?(Regexp)
22
- Webrat::XML.inner_html(o.element) =~ @option_text
22
+ o.element.inner_text =~ @option_text
23
23
  else
24
- Webrat::XML.inner_html(o.element) == @option_text.to_s
24
+ o.inner_text == @option_text.to_s
25
25
  end
26
26
  end
27
27
  else
28
28
  option_element = option_elements.detect do |o|
29
29
  if @option_text.is_a?(Regexp)
30
- Webrat::XML.inner_html(o) =~ @option_text
30
+ o.inner_text =~ @option_text
31
31
  else
32
- Webrat::XML.inner_html(o) == @option_text.to_s
32
+ o.inner_text == @option_text.to_s
33
33
  end
34
34
  end
35
35
 
@@ -38,7 +38,7 @@ module Webrat
38
38
  end
39
39
 
40
40
  def option_elements
41
- Webrat::XML.xpath_search(@dom, *SelectOption.xpath_search)
41
+ @dom.xpath(*SelectOption.xpath_search)
42
42
  end
43
43
 
44
44
  def error_message
@@ -1,3 +1,5 @@
1
+ require "logger"
2
+
1
3
  module Webrat
2
4
  module Logging #:nodoc:
3
5
 
@@ -11,12 +13,9 @@ module Webrat
11
13
  when :rails
12
14
  defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : nil
13
15
  when :merb
14
- Merb.logger
16
+ ::Merb.logger
15
17
  else
16
- @logger ||= begin
17
- require "logger"
18
- ::Logger.new("webrat.log")
19
- end
18
+ @logger ||= ::Logger.new("webrat.log")
20
19
  end
21
20
  end
22
21
 
@@ -7,13 +7,8 @@ module Webrat
7
7
  end
8
8
 
9
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)
10
+ @document = Webrat::XML.document(stringlike)
11
+ @element = @document.inner_text
17
12
 
18
13
  case @content
19
14
  when String
@@ -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)
@@ -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,
@@ -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:
@@ -105,7 +105,7 @@ For example:
105
105
  end
106
106
 
107
107
  def basic_auth(user, pass)
108
- encoded_login = ["#{user}:#{pass}"].pack("m*")
108
+ encoded_login = ["#{user}:#{pass}"].pack("m*").gsub(/\n/, '')
109
109
  header('HTTP_AUTHORIZATION', "Basic #{encoded_login}")
110
110
  end
111
111
 
@@ -260,6 +260,7 @@ For example:
260
260
  def_delegators :current_scope, :uncheck, :unchecks
261
261
  def_delegators :current_scope, :choose, :chooses
262
262
  def_delegators :current_scope, :select, :selects
263
+ def_delegators :current_scope, :unselect, :unselects
263
264
  def_delegators :current_scope, :select_datetime, :selects_datetime
264
265
  def_delegators :current_scope, :select_date, :selects_date
265
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
+