jwilger-webrat 0.4.3.4 → 0.4.4.2

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 (62) hide show
  1. data/History.txt +34 -0
  2. data/Rakefile +16 -3
  3. data/lib/webrat/core/configuration.rb +9 -16
  4. data/lib/webrat/core/elements/area.rb +7 -7
  5. data/lib/webrat/core/elements/element.rb +11 -11
  6. data/lib/webrat/core/elements/field.rb +50 -50
  7. data/lib/webrat/core/elements/form.rb +17 -17
  8. data/lib/webrat/core/elements/label.rb +6 -6
  9. data/lib/webrat/core/elements/link.rb +13 -11
  10. data/lib/webrat/core/elements/select_option.rb +9 -9
  11. data/lib/webrat/core/locators/area_locator.rb +10 -10
  12. data/lib/webrat/core/locators/button_locator.rb +13 -13
  13. data/lib/webrat/core/locators/field_by_id_locator.rb +8 -8
  14. data/lib/webrat/core/locators/field_labeled_locator.rb +11 -11
  15. data/lib/webrat/core/locators/field_locator.rb +7 -7
  16. data/lib/webrat/core/locators/field_named_locator.rb +10 -10
  17. data/lib/webrat/core/locators/form_locator.rb +6 -6
  18. data/lib/webrat/core/locators/label_locator.rb +9 -9
  19. data/lib/webrat/core/locators/link_locator.rb +12 -12
  20. data/lib/webrat/core/locators/locator.rb +5 -5
  21. data/lib/webrat/core/locators/select_option_locator.rb +11 -11
  22. data/lib/webrat/core/locators.rb +2 -2
  23. data/lib/webrat/core/logging.rb +7 -4
  24. data/lib/webrat/core/matchers/have_content.rb +12 -12
  25. data/lib/webrat/core/matchers/have_selector.rb +9 -9
  26. data/lib/webrat/core/matchers/have_tag.rb +4 -4
  27. data/lib/webrat/core/matchers/have_xpath.rb +24 -24
  28. data/lib/webrat/core/methods.rb +14 -10
  29. data/lib/webrat/core/mime.rb +3 -3
  30. data/lib/webrat/core/save_and_open_page.rb +9 -9
  31. data/lib/webrat/core/scope.rb +54 -52
  32. data/lib/webrat/core/session.rb +20 -13
  33. data/lib/webrat/core/xml/hpricot.rb +3 -3
  34. data/lib/webrat/core/xml/nokogiri.rb +11 -11
  35. data/lib/webrat/core/xml/rexml.rb +3 -3
  36. data/lib/webrat/core/xml.rb +16 -16
  37. data/lib/webrat/core_extensions/blank.rb +1 -1
  38. data/lib/webrat/core_extensions/deprecate.rb +1 -1
  39. data/lib/webrat/core_extensions/detect_mapped.rb +4 -4
  40. data/lib/webrat/core_extensions/meta_class.rb +1 -1
  41. data/lib/webrat/core_extensions/tcp_socket.rb +27 -0
  42. data/lib/webrat/mechanize.rb +9 -9
  43. data/lib/webrat/merb.rb +1 -1
  44. data/lib/webrat/merb_session.rb +10 -10
  45. data/lib/webrat/rack_test.rb +32 -0
  46. data/lib/webrat/rails.rb +2 -2
  47. data/lib/webrat/rspec-rails.rb +2 -2
  48. data/lib/webrat/selenium/application_server.rb +75 -0
  49. data/lib/webrat/selenium/matchers/have_content.rb +4 -4
  50. data/lib/webrat/selenium/matchers/have_selector.rb +4 -4
  51. data/lib/webrat/selenium/matchers/have_tag.rb +16 -16
  52. data/lib/webrat/selenium/matchers/have_xpath.rb +4 -4
  53. data/lib/webrat/selenium/matchers.rb +1 -1
  54. data/lib/webrat/selenium/merb_application_server.rb +50 -0
  55. data/lib/webrat/selenium/rails_application_server.rb +44 -0
  56. data/lib/webrat/selenium/selenium_rc_server.rb +90 -0
  57. data/lib/webrat/selenium/selenium_session.rb +22 -31
  58. data/lib/webrat/selenium/silence_stream.rb +14 -0
  59. data/lib/webrat/selenium/sinatra_application_server.rb +37 -0
  60. data/lib/webrat/selenium.rb +5 -81
  61. data/lib/webrat.rb +8 -11
  62. metadata +10 -2
@@ -4,30 +4,30 @@ require "webrat/core/elements/element"
4
4
 
5
5
  module Webrat
6
6
  class Link < Element #:nodoc:
7
-
7
+
8
8
  def self.xpath_search
9
9
  ".//a[@href]"
10
10
  end
11
-
11
+
12
12
  def click(options = {})
13
13
  method = options[:method] || http_method
14
14
  return if href =~ /^#/ && method == :get
15
-
15
+
16
16
  options[:javascript] = true if options[:javascript].nil?
17
-
17
+
18
18
  if options[:javascript]
19
19
  @session.request_page(absolute_href, method, data)
20
20
  else
21
21
  @session.request_page(absolute_href, :get, {})
22
22
  end
23
23
  end
24
-
24
+
25
25
  protected
26
-
26
+
27
27
  def id
28
28
  Webrat::XML.attribute(@element, "id")
29
29
  end
30
-
30
+
31
31
  def data
32
32
  authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
33
33
  end
@@ -49,17 +49,17 @@ module Webrat
49
49
  href
50
50
  end
51
51
  end
52
-
52
+
53
53
  def authenticity_token
54
54
  return unless onclick && onclick.include?("s.setAttribute('name', 'authenticity_token');") &&
55
55
  onclick =~ /s\.setAttribute\('value', '([a-f0-9]{40})'\);/
56
56
  $LAST_MATCH_INFO.captures.first
57
57
  end
58
-
58
+
59
59
  def onclick
60
60
  Webrat::XML.attribute(@element, "onclick")
61
61
  end
62
-
62
+
63
63
  def http_method
64
64
  if !onclick.blank? && onclick.include?("f.submit()")
65
65
  http_method_from_js_form
@@ -81,10 +81,12 @@ module Webrat
81
81
  :delete
82
82
  elsif onclick.include?("m.setAttribute('value', 'put')")
83
83
  :put
84
+ elsif onclick.include?("m.setAttribute('value', 'post')")
85
+ :post
84
86
  else
85
87
  raise Webrat::WebratError.new("No HTTP method for _method param in #{onclick.inspect}")
86
88
  end
87
89
  end
88
-
90
+
89
91
  end
90
92
  end
@@ -2,34 +2,34 @@ require "webrat/core/elements/element"
2
2
 
3
3
  module Webrat
4
4
  class SelectOption < Element #:nodoc:
5
-
5
+
6
6
  def self.xpath_search
7
7
  ".//option"
8
8
  end
9
-
9
+
10
10
  def choose
11
11
  select.raise_error_if_disabled
12
12
  select.set(value)
13
13
  end
14
-
14
+
15
15
  protected
16
-
16
+
17
17
  def select
18
18
  SelectField.load(@session, select_element)
19
19
  end
20
-
20
+
21
21
  def select_element
22
22
  parent = @element.parent
23
-
23
+
24
24
  while parent.respond_to?(:parent)
25
25
  return parent if parent.name == 'select'
26
26
  parent = parent.parent
27
27
  end
28
28
  end
29
-
29
+
30
30
  def value
31
31
  Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element)
32
32
  end
33
-
33
+
34
34
  end
35
- end
35
+ end
@@ -2,37 +2,37 @@ require "webrat/core/locators/locator"
2
2
 
3
3
  module Webrat
4
4
  module Locators
5
-
5
+
6
6
  class AreaLocator < Locator # :nodoc:
7
-
7
+
8
8
  def locate
9
9
  Area.load(@session, area_element)
10
10
  end
11
-
11
+
12
12
  def area_element
13
13
  area_elements.detect do |area_element|
14
14
  Webrat::XML.attribute(area_element, "title") =~ matcher ||
15
15
  Webrat::XML.attribute(area_element, "id") =~ matcher
16
16
  end
17
17
  end
18
-
18
+
19
19
  def matcher
20
20
  /#{Regexp.escape(@value.to_s)}/i
21
21
  end
22
-
22
+
23
23
  def area_elements
24
24
  Webrat::XML.xpath_search(@dom, Area.xpath_search)
25
25
  end
26
-
26
+
27
27
  def error_message
28
28
  "Could not find area with name #{@value}"
29
29
  end
30
-
30
+
31
31
  end
32
-
32
+
33
33
  def find_area(id_or_title) #:nodoc:
34
34
  AreaLocator.new(@session, dom, id_or_title).locate!
35
35
  end
36
-
36
+
37
37
  end
38
- end
38
+ end
@@ -2,13 +2,13 @@ require "webrat/core/locators/locator"
2
2
 
3
3
  module Webrat
4
4
  module Locators
5
-
5
+
6
6
  class ButtonLocator < Locator # :nodoc:
7
-
7
+
8
8
  def locate
9
9
  ButtonField.load(@session, button_element)
10
10
  end
11
-
11
+
12
12
  def button_element
13
13
  button_elements.detect do |element|
14
14
  @value.nil? ||
@@ -18,37 +18,37 @@ module Webrat
18
18
  matches_alt?(element)
19
19
  end
20
20
  end
21
-
21
+
22
22
  def matches_id?(element)
23
23
  (@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) ||
24
24
  (!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s)
25
25
  end
26
-
26
+
27
27
  def matches_value?(element)
28
28
  Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
29
29
  end
30
-
30
+
31
31
  def matches_html?(element)
32
32
  Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i
33
33
  end
34
-
34
+
35
35
  def matches_alt?(element)
36
36
  Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
37
37
  end
38
-
38
+
39
39
  def button_elements
40
40
  Webrat::XML.xpath_search(@dom, *ButtonField.xpath_search)
41
41
  end
42
-
42
+
43
43
  def error_message
44
44
  "Could not find button #{@value.inspect}"
45
45
  end
46
-
46
+
47
47
  end
48
-
48
+
49
49
  def find_button(value) #:nodoc:
50
50
  ButtonLocator.new(@session, dom, value).locate!
51
51
  end
52
-
52
+
53
53
  end
54
- end
54
+ end
@@ -2,13 +2,13 @@ require "webrat/core/locators/locator"
2
2
 
3
3
  module Webrat
4
4
  module Locators
5
-
5
+
6
6
  class FieldByIdLocator < Locator # :nodoc:
7
-
7
+
8
8
  def locate
9
9
  Field.load(@session, field_element)
10
10
  end
11
-
11
+
12
12
  def field_element
13
13
  field_elements.detect do |field_element|
14
14
  if @value.is_a?(Regexp)
@@ -18,7 +18,7 @@ module Webrat
18
18
  end
19
19
  end
20
20
  end
21
-
21
+
22
22
  def field_elements
23
23
  Webrat::XML.xpath_search(@dom, *Field.xpath_search)
24
24
  end
@@ -26,12 +26,12 @@ module Webrat
26
26
  def error_message
27
27
  "Could not find field with id #{@value.inspect}"
28
28
  end
29
-
29
+
30
30
  end
31
-
31
+
32
32
  def field_with_id(id, *field_types)
33
33
  FieldByIdLocator.new(@session, dom, id, *field_types).locate!
34
34
  end
35
-
35
+
36
36
  end
37
- end
37
+ end
@@ -3,13 +3,13 @@ require "webrat/core/locators/locator"
3
3
 
4
4
  module Webrat
5
5
  module Locators
6
-
6
+
7
7
  class FieldLabeledLocator < Locator # :nodoc:
8
-
8
+
9
9
  def locate
10
10
  matching_labels.any? && matching_labels.detect_mapped { |label| label.field }
11
11
  end
12
-
12
+
13
13
  def matching_labels
14
14
  matching_label_elements.sort_by do |label_element|
15
15
  text(label_element).length
@@ -17,21 +17,21 @@ module Webrat
17
17
  Label.load(@session, label_element)
18
18
  end
19
19
  end
20
-
20
+
21
21
  def matching_label_elements
22
22
  label_elements.select do |label_element|
23
- text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i
23
+ text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}(\b|\Z)/i
24
24
  end
25
25
  end
26
-
26
+
27
27
  def label_elements
28
28
  Webrat::XML.xpath_search(@dom, Label.xpath_search)
29
29
  end
30
-
30
+
31
31
  def error_message
32
32
  "Could not find field labeled #{@value.inspect}"
33
33
  end
34
-
34
+
35
35
  def text(element)
36
36
  str = Webrat::XML.all_inner_text(element)
37
37
  str.gsub!("\n","")
@@ -39,9 +39,9 @@ module Webrat
39
39
  str.squeeze!(" ")
40
40
  str
41
41
  end
42
-
42
+
43
43
  end
44
-
44
+
45
45
  # Locates a form field based on a <tt>label</tt> element in the HTML source.
46
46
  # This can be useful in order to verify that a field is pre-filled with the
47
47
  # correct value.
@@ -51,6 +51,6 @@ module Webrat
51
51
  def field_labeled(label, *field_types)
52
52
  FieldLabeledLocator.new(@session, dom, label, *field_types).locate!
53
53
  end
54
-
54
+
55
55
  end
56
56
  end
@@ -2,24 +2,24 @@ require "webrat/core/locators/locator"
2
2
 
3
3
  module Webrat
4
4
  module Locators
5
-
5
+
6
6
  class FieldLocator < Locator # :nodoc:
7
-
7
+
8
8
  def locate
9
9
  FieldByIdLocator.new(@session, @dom, @value).locate ||
10
10
  FieldNamedLocator.new(@session, @dom, @value, *@field_types).locate ||
11
11
  FieldLabeledLocator.new(@session, @dom, @value, *@field_types).locate
12
12
  end
13
-
13
+
14
14
  def error_message
15
15
  "Could not find field: #{@value.inspect}"
16
16
  end
17
-
17
+
18
18
  end
19
-
19
+
20
20
  def field(*args) # :nodoc:
21
21
  FieldLocator.new(@session, dom, *args).locate!
22
22
  end
23
-
23
+
24
24
  end
25
- end
25
+ end
@@ -2,23 +2,23 @@ require "webrat/core/locators/locator"
2
2
 
3
3
  module Webrat
4
4
  module Locators
5
-
5
+
6
6
  class FieldNamedLocator < Locator # :nodoc:
7
-
7
+
8
8
  def locate
9
9
  Field.load(@session, field_element)
10
10
  end
11
-
11
+
12
12
  def field_element
13
13
  field_elements.detect do |field_element|
14
14
  Webrat::XML.attribute(field_element, "name") == @value.to_s
15
15
  end
16
16
  end
17
-
17
+
18
18
  def field_elements
19
19
  Webrat::XML.xpath_search(@dom, *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
@@ -26,16 +26,16 @@ module Webrat
26
26
  Array(Field.xpath_search)
27
27
  end
28
28
  end
29
-
29
+
30
30
  def error_message
31
31
  "Could not find field named #{@value.inspect}"
32
32
  end
33
-
33
+
34
34
  end
35
-
35
+
36
36
  def field_named(name, *field_types)
37
37
  FieldNamedLocator.new(@session, dom, name, *field_types).locate!
38
38
  end
39
-
39
+
40
40
  end
41
- end
41
+ end
@@ -2,18 +2,18 @@ require "webrat/core/locators/locator"
2
2
 
3
3
  module Webrat
4
4
  module Locators
5
-
5
+
6
6
  class FormLocator < Locator # :nodoc:
7
-
7
+
8
8
  def locate
9
9
  Form.load(@session, form_element)
10
10
  end
11
-
11
+
12
12
  def form_element
13
13
  Webrat::XML.css_at(@dom, "#" + @value)
14
14
  end
15
-
15
+
16
16
  end
17
-
17
+
18
18
  end
19
- end
19
+ end
@@ -3,23 +3,23 @@ require "webrat/core/locators/locator"
3
3
 
4
4
  module Webrat
5
5
  module Locators
6
-
6
+
7
7
  class LabelLocator < Locator # :nodoc:
8
-
8
+
9
9
  def locate
10
10
  Label.load(@session, label_element)
11
11
  end
12
-
12
+
13
13
  def label_element
14
14
  label_elements.detect do |label_element|
15
- text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i
15
+ text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}(\b|\Z)/i
16
16
  end
17
17
  end
18
-
18
+
19
19
  def label_elements
20
20
  Webrat::XML.xpath_search(@dom, Label.xpath_search)
21
21
  end
22
-
22
+
23
23
  def text(label_element)
24
24
  str = Webrat::XML.all_inner_text(label_element)
25
25
  str.gsub!("\n","")
@@ -27,8 +27,8 @@ module Webrat
27
27
  str.squeeze!(" ")
28
28
  str
29
29
  end
30
-
30
+
31
31
  end
32
-
32
+
33
33
  end
34
- end
34
+ end
@@ -2,24 +2,24 @@ require "webrat/core/locators/locator"
2
2
 
3
3
  module Webrat
4
4
  module Locators
5
-
5
+
6
6
  class LinkLocator < Locator # :nodoc:
7
-
7
+
8
8
  def locate
9
9
  Link.load(@session, link_element)
10
10
  end
11
-
11
+
12
12
  def link_element
13
13
  matching_links.min { |a, b| Webrat::XML.all_inner_text(a).length <=> Webrat::XML.all_inner_text(b).length }
14
14
  end
15
-
15
+
16
16
  def matching_links
17
17
  @matching_links ||= link_elements.select do |link_element|
18
18
  matches_text?(link_element) ||
19
19
  matches_id?(link_element)
20
20
  end
21
21
  end
22
-
22
+
23
23
  def matches_text?(link)
24
24
  if @value.is_a?(Regexp)
25
25
  matcher = @value
@@ -39,11 +39,11 @@ module Webrat
39
39
  (Webrat::XML.attribute(link, "id") == @value) ? true : false
40
40
  end
41
41
  end
42
-
42
+
43
43
  def link_elements
44
44
  Webrat::XML.xpath_search(@dom, *Link.xpath_search)
45
45
  end
46
-
46
+
47
47
  def replace_nbsp(str)
48
48
  str.gsub([0xA0].pack('U'), ' ')
49
49
  end
@@ -51,16 +51,16 @@ module Webrat
51
51
  def replace_nbsp_ref(str)
52
52
  str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
53
53
  end
54
-
54
+
55
55
  def error_message
56
56
  "Could not find link with text or title or id #{@value.inspect}"
57
57
  end
58
-
58
+
59
59
  end
60
-
60
+
61
61
  def find_link(text_or_title_or_id) #:nodoc:
62
62
  LinkLocator.new(@session, dom, text_or_title_or_id).locate!
63
63
  end
64
-
64
+
65
65
  end
66
- end
66
+ end
@@ -1,6 +1,6 @@
1
1
  module Webrat
2
2
  module Locators
3
-
3
+
4
4
  class Locator # :nodoc:
5
5
 
6
6
  def initialize(session, dom, value, *field_types)
@@ -9,12 +9,12 @@ module Webrat
9
9
  @value = value
10
10
  @field_types = field_types
11
11
  end
12
-
12
+
13
13
  def locate!
14
14
  locate || raise(NotFoundError.new(error_message))
15
15
  end
16
-
16
+
17
17
  end
18
-
18
+
19
19
  end
20
- end
20
+ end
@@ -3,20 +3,20 @@ require "webrat/core/locators/locator"
3
3
 
4
4
  module Webrat
5
5
  module Locators
6
-
6
+
7
7
  class SelectOptionLocator < Locator # :nodoc:
8
-
8
+
9
9
  def initialize(session, dom, option_text, id_or_name_or_label)
10
10
  @session = session
11
11
  @dom = dom
12
12
  @option_text = option_text
13
13
  @id_or_name_or_label = id_or_name_or_label
14
14
  end
15
-
15
+
16
16
  def locate
17
17
  if @id_or_name_or_label
18
18
  field = FieldLocator.new(@session, @dom, @id_or_name_or_label, SelectField).locate!
19
-
19
+
20
20
  field.options.detect do |o|
21
21
  if @option_text.is_a?(Regexp)
22
22
  Webrat::XML.inner_html(o.element) =~ @option_text
@@ -32,15 +32,15 @@ module Webrat
32
32
  Webrat::XML.inner_html(o) == @option_text.to_s
33
33
  end
34
34
  end
35
-
35
+
36
36
  SelectOption.load(@session, option_element)
37
37
  end
38
38
  end
39
-
39
+
40
40
  def option_elements
41
41
  Webrat::XML.xpath_search(@dom, *SelectOption.xpath_search)
42
42
  end
43
-
43
+
44
44
  def error_message
45
45
  if @id_or_name_or_label
46
46
  "The '#{@option_text}' option was not found in the #{@id_or_name_or_label.inspect} select box"
@@ -48,12 +48,12 @@ module Webrat
48
48
  "Could not find option #{@option_text.inspect}"
49
49
  end
50
50
  end
51
-
51
+
52
52
  end
53
-
53
+
54
54
  def select_option(option_text, id_or_name_or_label = nil) #:nodoc:
55
55
  SelectOptionLocator.new(@session, dom, option_text, id_or_name_or_label).locate!
56
56
  end
57
-
57
+
58
58
  end
59
- end
59
+ end
@@ -11,10 +11,10 @@ require "webrat/core/locators/form_locator"
11
11
 
12
12
  module Webrat
13
13
  module Locators
14
-
14
+
15
15
  def field_by_xpath(xpath)
16
16
  Field.load(@session, Webrat::XML.xpath_at(dom, xpath))
17
17
  end
18
-
18
+
19
19
  end
20
20
  end
@@ -1,6 +1,6 @@
1
1
  module Webrat
2
2
  module Logging #:nodoc:
3
-
3
+
4
4
  def debug_log(message) # :nodoc:
5
5
  return unless logger
6
6
  logger.debug message
@@ -13,9 +13,12 @@ module Webrat
13
13
  when :merb
14
14
  Merb.logger
15
15
  else
16
- nil
16
+ @logger ||= begin
17
+ require "logger"
18
+ ::Logger.new("webrat.log")
19
+ end
17
20
  end
18
21
  end
19
-
22
+
20
23
  end
21
- end
24
+ end