celerity 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/History.txt +36 -0
  2. data/Manifest.txt +82 -0
  3. data/README.rdoc +78 -0
  4. data/Rakefile +25 -10
  5. data/celerity.gemspec +40 -0
  6. data/lib/celerity.rb +36 -20
  7. data/lib/celerity/browser.rb +396 -189
  8. data/lib/celerity/clickable_element.rb +25 -5
  9. data/lib/celerity/collections.rb +2 -2
  10. data/lib/celerity/container.rb +74 -61
  11. data/lib/celerity/default_viewer.rb +8 -4
  12. data/lib/celerity/disabled_element.rb +5 -5
  13. data/lib/celerity/element.rb +57 -35
  14. data/lib/celerity/element_collection.rb +22 -21
  15. data/lib/celerity/element_locator.rb +25 -18
  16. data/lib/celerity/elements/button.rb +13 -11
  17. data/lib/celerity/elements/file_field.rb +8 -4
  18. data/lib/celerity/elements/form.rb +7 -5
  19. data/lib/celerity/elements/frame.rb +6 -4
  20. data/lib/celerity/elements/image.rb +4 -4
  21. data/lib/celerity/elements/label.rb +1 -1
  22. data/lib/celerity/elements/link.rb +5 -5
  23. data/lib/celerity/elements/meta.rb +2 -1
  24. data/lib/celerity/elements/non_control_elements.rb +3 -3
  25. data/lib/celerity/elements/option.rb +7 -7
  26. data/lib/celerity/elements/radio_check.rb +18 -18
  27. data/lib/celerity/elements/select_list.rb +55 -25
  28. data/lib/celerity/elements/table.rb +21 -18
  29. data/lib/celerity/elements/table_cell.rb +1 -1
  30. data/lib/celerity/elements/table_elements.rb +1 -1
  31. data/lib/celerity/elements/table_row.rb +5 -5
  32. data/lib/celerity/elements/text_field.rb +33 -28
  33. data/lib/celerity/exception.rb +11 -5
  34. data/lib/celerity/htmlunit.rb +24 -8
  35. data/lib/celerity/htmlunit/commons-codec-1.4.jar +0 -0
  36. data/lib/celerity/htmlunit/htmlunit-2.6.jar +0 -0
  37. data/lib/celerity/htmlunit/htmlunit-core-js-2.6.jar +0 -0
  38. data/lib/celerity/htmlunit/nekohtml-1.9.13.jar +0 -0
  39. data/lib/celerity/htmlunit/{xercesImpl-2.8.1.jar → xercesImpl-2.9.1.jar} +0 -0
  40. data/lib/celerity/ignoring_web_connection.rb +15 -0
  41. data/lib/celerity/input_element.rb +1 -1
  42. data/lib/celerity/listener.rb +23 -17
  43. data/lib/celerity/short_inspect.rb +20 -0
  44. data/lib/celerity/util.rb +5 -2
  45. data/lib/celerity/version.rb +3 -10
  46. data/lib/celerity/viewer_connection.rb +89 -0
  47. data/lib/celerity/watir_compatibility.rb +2 -5
  48. data/lib/celerity/xpath_support.rb +48 -0
  49. data/spec/browser_authentication_spec.rb +16 -0
  50. data/spec/browser_spec.rb +300 -0
  51. data/spec/clickable_element_spec.rb +39 -0
  52. data/spec/default_viewer_spec.rb +23 -0
  53. data/spec/element_spec.rb +51 -0
  54. data/spec/filefield_spec.rb +18 -0
  55. data/spec/htmlunit_spec.rb +56 -0
  56. data/spec/index_offset_spec.rb +24 -0
  57. data/spec/listener_spec.rb +142 -0
  58. data/spec/spec_helper.rb +8 -0
  59. data/tasks/benchmark.rake +4 -0
  60. data/tasks/deployment.rake +43 -0
  61. data/tasks/environment.rake +7 -0
  62. data/tasks/fix.rake +25 -0
  63. data/tasks/jar.rake +4 -6
  64. data/tasks/rspec.rake +43 -0
  65. data/tasks/simple_ci.rake +94 -0
  66. data/tasks/snapshot.rake +22 -0
  67. data/tasks/website.rake +17 -0
  68. data/tasks/yard.rake +9 -0
  69. metadata +59 -26
  70. data/README.txt +0 -69
  71. data/lib/celerity/extra/method_generator.rb +0 -170
  72. data/lib/celerity/htmlunit/commons-codec-1.3.jar +0 -0
  73. data/lib/celerity/htmlunit/htmlunit-2.5-SNAPSHOT.jar +0 -0
  74. data/lib/celerity/htmlunit/htmlunit-core-js-2.5-SNAPSHOT.jar +0 -0
  75. data/lib/celerity/htmlunit/nekohtml-1.9.12-20090308.130127-11.jar +0 -0
@@ -1,5 +1,5 @@
1
1
  module Celerity
2
-
2
+
3
3
  #
4
4
  # For fields that accept file uploads
5
5
  #
@@ -10,16 +10,20 @@ module Celerity
10
10
 
11
11
  #
12
12
  # Set the file field to the given path
13
- #
13
+ #
14
14
 
15
15
  def set(path)
16
16
  assert_exists
17
17
  path = path.to_s
18
- @container.update_page @object.setValueAttribute(path)
18
+
19
+ @object.setValueAttribute path
20
+
19
21
  unless @object.getContentType
20
22
  @object.setContentType(Celerity::Util.content_type_for(path))
21
23
  end
24
+
25
+ path
22
26
  end
23
-
27
+
24
28
  end # FileField
25
29
  end # Celerity
@@ -1,20 +1,22 @@
1
1
  module Celerity
2
2
  class Form < Element
3
3
  include Container
4
-
4
+
5
5
  TAGS = [Identifier.new('form')]
6
-
6
+
7
7
  # HTML 4.01 Transitional DTD
8
8
  ATTRIBUTES = BASE_ATTRIBUTES | [:action, :method, :enctype, :accept, :name, :onsubmit, :onreset, :target, :'accept-charset']
9
9
  DEFAULT_HOW = :name
10
10
 
11
11
  #
12
12
  # Submits the form.
13
- #
14
-
13
+ #
14
+ # This method should be avoided - invoke the user interface element that triggers the submit instead.
15
+ #
16
+
15
17
  def submit
16
18
  assert_exists
17
- @container.update_page @object.submit(nil)
19
+ @object.submit(nil)
18
20
  end
19
21
 
20
22
  end # Form
@@ -1,6 +1,8 @@
1
1
  module Celerity
2
2
  class Frame < Element
3
3
  include Container
4
+ include XpathSupport
5
+
4
6
  attr_accessor :page
5
7
 
6
8
  TAGS = [Identifier.new('frame'), Identifier.new('iframe')]
@@ -10,7 +12,7 @@ module Celerity
10
12
  #
11
13
  # Override the default locate to handle frame and inline frames.
12
14
  # @api private
13
- #
15
+ #
14
16
 
15
17
  def locate
16
18
  super
@@ -22,7 +24,7 @@ module Celerity
22
24
  end
23
25
  end
24
26
  end
25
-
27
+
26
28
  #
27
29
  # Override assert_exists to raise UnknownFrameException (for Watir compatibility)
28
30
  # @api private
@@ -64,7 +66,7 @@ module Celerity
64
66
  meth = selector_to_attribute(meth)
65
67
  if self.class::ATTRIBUTES.include?(meth)
66
68
  assert_exists
67
- @inline_frame_object.getAttributeValue(meth.to_s)
69
+ @inline_frame_object.getAttribute(meth.to_s)
68
70
  else
69
71
  Log.warn "Element\#method_missing calling super with #{meth.inspect}"
70
72
  super
@@ -72,4 +74,4 @@ module Celerity
72
74
  end
73
75
 
74
76
  end # Frame
75
- end # Celerity
77
+ end # Celerity
@@ -4,7 +4,7 @@ module Celerity
4
4
  include ClickableElement
5
5
 
6
6
  TAGS = [ Identifier.new('img') ]
7
-
7
+
8
8
  ATTRIBUTES = BASE_ATTRIBUTES | [:src, :alt, :longdesc, :name, :height, :width, :usemap, :ismap, :align, :border, :hspace, :vspace]
9
9
  DEFAULT_HOW = :src
10
10
 
@@ -19,13 +19,13 @@ module Celerity
19
19
  end
20
20
 
21
21
  #
22
- # returns the filesize of the image
22
+ # returns the file size of the image in bytes
23
23
  #
24
24
 
25
25
  def file_size
26
26
  assert_exists
27
27
  web_response = @object.getWebResponse(true)
28
- web_response.getResponseBody.length
28
+ web_response.getContentAsBytes.length
29
29
  end
30
30
 
31
31
  #
@@ -71,6 +71,6 @@ module Celerity
71
71
  buffered_image = image_reader.read(0);
72
72
  javax.imageio.ImageIO.write(buffered_image, image_reader.getFormatName(), file);
73
73
  end
74
-
74
+
75
75
  end # Image
76
76
  end # Celerity
@@ -2,7 +2,7 @@ module Celerity
2
2
 
3
3
  class Label < Element
4
4
  include ClickableElement
5
-
5
+
6
6
  TAGS = [ Identifier.new('label') ]
7
7
  ATTRIBUTES = BASE_ATTRIBUTES | [:for, :accesskey, :onfocus, :onblur]
8
8
  DEFAULT_HOW = :text
@@ -7,16 +7,16 @@ module Celerity
7
7
  :target, :rel, :rev, :accesskey, :shape,
8
8
  :coords, :tabindex, :onfocus, :onblur]
9
9
  DEFAULT_HOW = :href
10
-
10
+
11
11
  #
12
12
  # Returns the absolute URL for this link (Celerity-specific)
13
- #
13
+ #
14
14
  # (Watir/IE does this for href(), but we don't want that.)
15
- #
15
+ #
16
16
 
17
17
  def absolute_url
18
18
  assert_exists
19
- href = @object.getAttributeValue('href')
19
+ href = @object.getAttribute('href')
20
20
 
21
21
  unless href.empty? || URI.parse(href).absolute?
22
22
  href = URI.join(browser.url, href).to_s
@@ -25,6 +25,6 @@ module Celerity
25
25
  href
26
26
  end
27
27
 
28
-
28
+
29
29
  end # Link
30
30
  end # Celerity
@@ -1,6 +1,7 @@
1
1
  module Celerity
2
2
  class Meta < Element
3
- ATTRIBUTES = [:name, :id, :'http-equiv', :content, :scheme] | HTML_401_TRANSITIONAL[:i18n]
3
+ ATTRIBUTES = [:name, :id, :'http-equiv', :content, :scheme] | HTML_401_TRANSITIONAL[:i18n]
4
+ DEFAULT_HOW = :id
4
5
  TAGS = [ Identifier.new('meta') ]
5
6
  end
6
7
  end
@@ -1,9 +1,9 @@
1
1
  module Celerity
2
-
2
+
3
3
  #
4
4
  # Superclass for for Span, Pre, Div, H1, ...
5
5
  #
6
-
6
+
7
7
  class NonControlElement < Element
8
8
  include Exception
9
9
  include ClickableElement
@@ -90,7 +90,7 @@ module Celerity
90
90
  class Span < NonControlElement
91
91
  TAGS = [ Identifier.new('span') ]
92
92
  end
93
-
93
+
94
94
  class Strong < NonControlElement
95
95
  TAGS = [ Identifier.new('strong') ]
96
96
  end
@@ -1,5 +1,5 @@
1
1
  module Celerity
2
-
2
+
3
3
  #
4
4
  # Represents an option in a select list.
5
5
  #
@@ -8,25 +8,25 @@ module Celerity
8
8
  include ClickableElement
9
9
  include DisabledElement
10
10
 
11
- TAGS = [ Identifier.new('option')]
11
+ TAGS = [ Identifier.new('option') ]
12
12
  ATTRIBUTES = BASE_ATTRIBUTES | [:selected, :disabled, :label, :value]
13
13
  DEFAULT_HOW = :text
14
14
 
15
15
  alias_method :select, :click
16
16
 
17
- #
17
+ #
18
18
  # Is this option selected?
19
- #
20
-
19
+ #
20
+
21
21
  def selected?
22
22
  assert_exists
23
23
  @object.isSelected
24
24
  end
25
-
25
+
26
26
  def label
27
27
  # overrides Container#label
28
28
  assert_exists
29
- @object.getAttribute("label")
29
+ @object.getLabelAttribute
30
30
  end
31
31
  end
32
32
  end
@@ -1,16 +1,16 @@
1
1
  module Celerity
2
-
2
+
3
3
  #
4
4
  # Common superclass for radios and check boxes.
5
5
  #
6
-
6
+
7
7
  class RadioCheckCommon < InputElement
8
8
  DEFAULT_HOW = :name
9
-
9
+
10
10
  #
11
- # Can optionally take a value parameter as a third arg, so we override initialize
11
+ # Can optionally take a value parameter as a third arg, so we override initialize
12
12
  #
13
-
13
+
14
14
  def initialize(container, type, *args)
15
15
  @type = type
16
16
  case args.size
@@ -22,12 +22,12 @@ module Celerity
22
22
  super(container, *args)
23
23
  end
24
24
  end
25
-
25
+
26
26
  #
27
27
  # returns true if the element is checked
28
28
  # @return [true, false]
29
29
  #
30
-
30
+
31
31
  def set?
32
32
  assert_exists
33
33
  @object.isChecked
@@ -37,7 +37,7 @@ module Celerity
37
37
  #
38
38
  # Unset this element.
39
39
  #
40
-
40
+
41
41
  def clear
42
42
  set(false)
43
43
  end
@@ -46,14 +46,14 @@ module Celerity
46
46
  #
47
47
  # This class is the representation of a radio button.
48
48
  #
49
-
49
+
50
50
  class Radio < RadioCheckCommon
51
51
  TAGS = [Identifier.new('input', :type => %w[radio])]
52
-
52
+
53
53
  #
54
54
  # @api private
55
55
  #
56
-
56
+
57
57
  def initialize(container, *args)
58
58
  super(container, %w[radio], *args)
59
59
  end
@@ -67,26 +67,26 @@ module Celerity
67
67
  # radio.set(false)
68
68
  # radio.set? #=> false
69
69
  #
70
-
70
+
71
71
  def set(value = true)
72
72
  assert_exists
73
73
  assert_enabled
74
- @container.update_page(value ? @object.click : @object.setChecked(value))
74
+ value ? @object.click : @object.setChecked(value)
75
75
  end
76
76
 
77
77
  end
78
-
78
+
79
79
  #
80
80
  # This class is the representation of a check box.
81
81
  #
82
-
82
+
83
83
  class CheckBox < RadioCheckCommon
84
84
  TAGS = [Identifier.new('input', :type => %w[checkbox])]
85
85
 
86
86
  #
87
87
  # @api private
88
88
  #
89
-
89
+
90
90
  def initialize(container, *args)
91
91
  super(container, %w[checkbox], *args)
92
92
  end
@@ -100,14 +100,14 @@ module Celerity
100
100
  # checkbox.set(false)
101
101
  # checkbox.set? #=> false
102
102
  #
103
-
103
+
104
104
  def set(value = true)
105
105
  assert_exists
106
106
  assert_enabled
107
107
 
108
108
  if (value && !set?) || (!value && set?)
109
109
  Log.debug(@object.inspect)
110
- @container.update_page(@object.click)
110
+ @object.click
111
111
  end
112
112
  end
113
113
  end
@@ -6,25 +6,27 @@ module Celerity
6
6
  #
7
7
  # @return [Array<String>] An array of strings representing the text value of the select list's options.
8
8
  #
9
-
9
+
10
10
  def options
11
11
  assert_exists
12
- @object.getOptions.map { |e| e.asText }
12
+ @object.getOptions.map do |e|
13
+ e.asText.empty? ? e.getLabelAttribute : e.asText
14
+ end
13
15
  end
14
-
16
+
15
17
  #
16
18
  # @return [Array<String>] An array of strings representing the text value of the currently selected options.
17
19
  #
18
-
20
+
19
21
  def selected_options
20
22
  assert_exists
21
- @object.getSelectedOptions.map { |e| e.asText }
23
+ @object.getSelectedOptions.map { |e| e.asText.empty? ? e.getLabelAttribute : e.asText }
22
24
  end
23
-
25
+
24
26
  #
25
27
  # Clear all selected options
26
28
  #
27
-
29
+
28
30
  def clear
29
31
  # assert_exists called by SelectList#type here
30
32
  # TODO: should update page for each option changed?
@@ -32,39 +34,62 @@ module Celerity
32
34
  end
33
35
 
34
36
  #
35
- # Select the option(s) matching the given value.
37
+ # Select the option(s) whose text or label matches the given string.
36
38
  # If several options match the value given, all will be selected.
37
39
  #
38
40
  # @param [String, Regexp] value A value.
39
41
  # @raise [Celerity::Exception::NoValueFoundException] if the value does not exist.
40
- # @return [String, nil] The option selected. If multiple options match, returns the first match
41
- #
42
+ # @return [String] The option selected. If multiple options match, returns the first match
42
43
  #
43
-
44
+ #
45
+
44
46
  def select(value)
45
47
  assert_exists
46
- raise NoValueFoundException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}" unless include?(value)
47
-
48
+
48
49
  selected = nil
49
- @object.getOptions.select { |e| matches?(e.asText, value) }.each do |option|
50
+ @object.getOptions.select do |option|
51
+ next unless matches_option?(option, value)
52
+
50
53
  selected ||= option.asText
51
- @container.update_page option.click
54
+ option.click
55
+ end
56
+
57
+ unless selected
58
+ raise NoValueFoundException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}"
52
59
  end
53
-
60
+
54
61
  selected
55
62
  end
56
63
  alias_method :set, :select
57
64
 
65
+ #
66
+ # Selects the option(s) whose value attribute matches the given string.
67
+ # @param [String, Regexp] value A value.
68
+ # @raise [Celerity::Exception::NoValueFoundException] if the value does not exist.
69
+ # @return [String] The option selected. If multiple options match, returns the first match
70
+ #
71
+
72
+ def select_value(value)
73
+ assert_exists
74
+ selected = @object.getOptions.map { |e| e.click if matches?(e.getValueAttribute, value) }.compact.first
75
+
76
+ unless selected
77
+ raise NoValueFoundException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}"
78
+ end
79
+
80
+ selected.asText
81
+ end
82
+
58
83
  #
59
84
  # Returns true if the select list has one or more options matching the given value.
60
85
  #
61
86
  # @param [String, Regexp] value A value.
62
87
  # @return [true, false]
63
88
  #
64
-
89
+
65
90
  def include?(value)
66
91
  assert_exists
67
- !!@object.getOptions.find { |e| matches?(e.asText, value) }
92
+ !!@object.getOptions.find { |e| matches_option?(e, value) }
68
93
  end
69
94
 
70
95
  #
@@ -74,11 +99,11 @@ module Celerity
74
99
  # @raise [Celerity::Exception::UnknownObjectException] if the value does not exist.
75
100
  # @return [true, false]
76
101
  #
77
-
102
+
78
103
  def selected?(value)
79
104
  assert_exists
80
105
  raise UnknownObjectException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}" unless include?(value)
81
- !!@object.getOptions.find { |e| matches?(e.asText, value) && e.isSelected }
106
+ !!@object.getOptions.find { |e| matches_option?(e, value) && e.isSelected }
82
107
  end
83
108
 
84
109
  #
@@ -86,12 +111,11 @@ module Celerity
86
111
  # defined, otherwise 'select-one'.
87
112
  #
88
113
  # @return [String]
89
- # TODO: Move to watir_compatibility or delete it 2008-05-23 Alexander
90
114
  #
91
-
115
+
92
116
  def type
93
117
  assert_exists
94
- 'select-' + (@object.isAttributeDefined('multiple') ? 'multiple' : 'one')
118
+ 'select-' + (@object.hasAttribute('multiple') ? 'multiple' : 'one')
95
119
  end
96
120
 
97
121
  #
@@ -103,10 +127,16 @@ module Celerity
103
127
 
104
128
  def value
105
129
  assert_exists
106
- if (optn = @object.getSelectedOptions.to_a.first)
107
- optn.getValueAttribute
130
+ if (option = @object.getSelectedOptions.to_a.first)
131
+ option.getValueAttribute
108
132
  end
109
133
  end
110
134
 
135
+ private
136
+
137
+ def matches_option?(option, value)
138
+ matches?(option.asText, value) || (option.hasAttribute("label") && matches?(option.getLabelAttribute, value))
139
+ end
140
+
111
141
  end # SelectList
112
142
  end # Celerity