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
@@ -2,9 +2,9 @@ require "webrat/core/elements/element"
2
2
 
3
3
  module Webrat
4
4
  class Label < Element #:nodoc:
5
-
5
+
6
6
  attr_reader :element
7
-
7
+
8
8
  def self.xpath_search
9
9
  ".//label"
10
10
  end
@@ -12,20 +12,20 @@ module Webrat
12
12
  def for_id
13
13
  Webrat::XML.attribute(@element, "for")
14
14
  end
15
-
15
+
16
16
  def field
17
17
  Field.load(@session, field_element)
18
18
  end
19
-
19
+
20
20
  protected
21
-
21
+
22
22
  def field_element
23
23
  if for_id.blank?
24
- Webrat::XML.xpath_at(@element, *Field.xpath_search)
24
+ Webrat::XML.xpath_at(@element, *Field.xpath_search_excluding_hidden)
25
25
  else
26
- Webrat::XML.css_search(@session.dom, "#" + for_id).first
26
+ Webrat::XML.css_search(@session.current_dom, "#" + for_id).first
27
27
  end
28
28
  end
29
-
29
+
30
30
  end
31
31
  end
@@ -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
@@ -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
@@ -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
- matching_labels.any? && matching_labels.first.field
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,12 +39,18 @@ module Webrat
39
39
  str.squeeze!(" ")
40
40
  str
41
41
  end
42
-
42
+
43
43
  end
44
-
44
+
45
+ # Locates a form field based on a <tt>label</tt> element in the HTML source.
46
+ # This can be useful in order to verify that a field is pre-filled with the
47
+ # correct value.
48
+ #
49
+ # Example:
50
+ # field_labeled("First name").value.should == "Bryan"
45
51
  def field_labeled(label, *field_types)
46
52
  FieldLabeledLocator.new(@session, dom, label, *field_types).locate!
47
53
  end
48
-
54
+
49
55
  end
50
- end
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