walidhalabi-celerity 0.0.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/History.txt +75 -0
  2. data/License.txt +621 -0
  3. data/README.txt +73 -0
  4. data/Rakefile +12 -0
  5. data/lib/celerity.rb +74 -0
  6. data/lib/celerity/browser.rb +811 -0
  7. data/lib/celerity/clickable_element.rb +69 -0
  8. data/lib/celerity/collections.rb +156 -0
  9. data/lib/celerity/container.rb +788 -0
  10. data/lib/celerity/default_viewer.rb +10 -0
  11. data/lib/celerity/disabled_element.rb +40 -0
  12. data/lib/celerity/element.rb +313 -0
  13. data/lib/celerity/element_collection.rb +107 -0
  14. data/lib/celerity/element_locator.rb +170 -0
  15. data/lib/celerity/elements/button.rb +43 -0
  16. data/lib/celerity/elements/file_field.rb +25 -0
  17. data/lib/celerity/elements/form.rb +23 -0
  18. data/lib/celerity/elements/frame.rb +75 -0
  19. data/lib/celerity/elements/image.rb +76 -0
  20. data/lib/celerity/elements/label.rb +11 -0
  21. data/lib/celerity/elements/link.rb +30 -0
  22. data/lib/celerity/elements/meta.rb +6 -0
  23. data/lib/celerity/elements/non_control_elements.rb +106 -0
  24. data/lib/celerity/elements/option.rb +32 -0
  25. data/lib/celerity/elements/radio_check.rb +115 -0
  26. data/lib/celerity/elements/select_list.rb +121 -0
  27. data/lib/celerity/elements/table.rb +144 -0
  28. data/lib/celerity/elements/table_cell.rb +29 -0
  29. data/lib/celerity/elements/table_elements.rb +42 -0
  30. data/lib/celerity/elements/table_row.rb +48 -0
  31. data/lib/celerity/elements/text_field.rb +169 -0
  32. data/lib/celerity/exception.rb +77 -0
  33. data/lib/celerity/htmlunit.rb +61 -0
  34. data/lib/celerity/htmlunit/commons-codec-1.3.jar +0 -0
  35. data/lib/celerity/htmlunit/commons-collections-3.2.1.jar +0 -0
  36. data/lib/celerity/htmlunit/commons-httpclient-3.1.jar +0 -0
  37. data/lib/celerity/htmlunit/commons-io-1.4.jar +0 -0
  38. data/lib/celerity/htmlunit/commons-lang-2.4.jar +0 -0
  39. data/lib/celerity/htmlunit/commons-logging-1.1.1.jar +0 -0
  40. data/lib/celerity/htmlunit/cssparser-0.9.5.jar +0 -0
  41. data/lib/celerity/htmlunit/htmlunit-2.6-SNAPSHOT.jar +0 -0
  42. data/lib/celerity/htmlunit/htmlunit-core-js-2.5.jar +0 -0
  43. data/lib/celerity/htmlunit/nekohtml-1.9.13-20090507.082850-2.jar +0 -0
  44. data/lib/celerity/htmlunit/sac-1.3.jar +0 -0
  45. data/lib/celerity/htmlunit/serializer-2.7.1.jar +0 -0
  46. data/lib/celerity/htmlunit/xalan-2.7.1.jar +0 -0
  47. data/lib/celerity/htmlunit/xercesImpl-2.8.1.jar +0 -0
  48. data/lib/celerity/htmlunit/xml-apis-1.3.04.jar +0 -0
  49. data/lib/celerity/identifier.rb +11 -0
  50. data/lib/celerity/input_element.rb +25 -0
  51. data/lib/celerity/listener.rb +141 -0
  52. data/lib/celerity/resources/no_viewer.png +0 -0
  53. data/lib/celerity/short_inspect.rb +20 -0
  54. data/lib/celerity/util.rb +91 -0
  55. data/lib/celerity/version.rb +10 -0
  56. data/lib/celerity/watir_compatibility.rb +84 -0
  57. data/tasks/jar.rake +57 -0
  58. data/tasks/rdoc.rake +4 -0
  59. metadata +130 -0
@@ -0,0 +1,29 @@
1
+ module Celerity
2
+
3
+ class TableCell < Element
4
+ include Celerity::Exception
5
+ include ClickableElement
6
+ include Container
7
+
8
+ TAGS = [ Identifier.new('td') ]
9
+ ATTRIBUTES = BASE_ATTRIBUTES | CELLHALIGN_ATTRIBUTES |
10
+ CELLVALIGN_ATTRIBUTES | [:abbr, :axis, :headers, :scope, :rowspan, :colspan]
11
+
12
+ DEFAULT_HOW = :id
13
+
14
+ alias_method :to_s, :text # why?
15
+
16
+ def colspan
17
+ assert_exists
18
+ attribute_value = @object.getAttribute('colspan').to_i
19
+ attribute_value > 0 ? attribute_value : 1
20
+ end
21
+
22
+ end
23
+
24
+ #-- needs code review regarding attributes/correctness of this
25
+ class Th < TableCell
26
+ TAGS = [ Identifier.new('th')]
27
+ end
28
+
29
+ end
@@ -0,0 +1,42 @@
1
+ module Celerity
2
+ class TableElement < Element
3
+ include Enumerable
4
+ include ClickableElement
5
+
6
+ ATTRIBUTES = BASE_ATTRIBUTES | CELLHALIGN_ATTRIBUTES | CELLVALIGN_ATTRIBUTES
7
+ DEFAULT_HOW = :id
8
+
9
+ def locate
10
+ super
11
+ @rows = @object.getRows if @object
12
+ end
13
+
14
+ def [](index)
15
+ assert_exists
16
+ TableRow.new(self, :object, @rows[index - Celerity.index_offset])
17
+ end
18
+
19
+ def length
20
+ assert_exists
21
+ @object.getRows.length
22
+ end
23
+
24
+ def each
25
+ assert_exists
26
+ @rows.each { |row| yield TableRow.new(self, :object, row) }
27
+ end
28
+ end
29
+
30
+ class TableBody < TableElement
31
+ TAGS = [ Identifier.new('tbody') ]
32
+ end
33
+
34
+ class TableFooter < TableElement
35
+ TAGS = [ Identifier.new('tfoot') ]
36
+ end
37
+
38
+ class TableHeader < TableElement
39
+ TAGS = [ Identifier.new('thead') ]
40
+ end
41
+
42
+ end
@@ -0,0 +1,48 @@
1
+ module Celerity
2
+ class TableRow < Element
3
+ include Enumerable
4
+ include ClickableElement
5
+
6
+ TAGS = [ Identifier.new('tr') ]
7
+ DEFAULT_HOW = :id
8
+
9
+ def locate
10
+ super
11
+ @cells = @object.getCells if @object
12
+ end
13
+
14
+ #
15
+ # Yields each TableCell in this row to the given block.
16
+ #
17
+
18
+ def each
19
+ assert_exists
20
+ @cells.each { |cell| yield TableCell.new(self, :object, cell) }
21
+ end
22
+
23
+ #
24
+ # Get the child cell at the given index
25
+ #
26
+
27
+ def child_cell(index)
28
+ assert_exists
29
+
30
+ if (index - Celerity.index_offset) >= @cells.length
31
+ raise UnknownCellException, "Unable to locate a cell at index #{index}"
32
+ end
33
+
34
+ TableCell.new(self, :object, @cells[index - Celerity.index_offset])
35
+ end
36
+ alias_method :[], :child_cell
37
+
38
+ #
39
+ # Number of cells in this row.
40
+ #
41
+
42
+ def column_count
43
+ assert_exists
44
+ @cells.length
45
+ end
46
+
47
+ end # TableRow
48
+ end # Celerity
@@ -0,0 +1,169 @@
1
+ module Celerity
2
+
3
+ #
4
+ # Class representing text field elements
5
+ #
6
+ # This class is the main class for Text Fields
7
+ # Normally a user would not need to create this object as it is returned by the Watir::Container#text_field method
8
+ #
9
+
10
+ class TextField < InputElement
11
+ NON_TEXT_TYPES = %w[file radio checkbox submit reset image button hidden]
12
+ TAGS = [ Identifier.new('textarea'),
13
+ Identifier.new('input', :type => ["text", "password", /^(?!(#{ Regexp.union(*NON_TEXT_TYPES) })$)/]) ]
14
+ DEFAULT_HOW = :name
15
+
16
+ def visible?
17
+ assert_exists
18
+ type == 'hidden' ? false : super
19
+ end
20
+
21
+ #
22
+ # Clear the text field.
23
+ #
24
+
25
+ def clear
26
+ assert_exists
27
+ insert_string ''
28
+ end
29
+
30
+ #
31
+ # Set the text field to the given value.
32
+ # This ensures execution of JavaScript events (onkeypress etc.), but is slower than +value=+
33
+ #
34
+
35
+ def set(value)
36
+ assert_enabled
37
+ assert_not_readonly
38
+ clear
39
+ type_string(value.to_s)
40
+
41
+ value
42
+ end
43
+
44
+ #
45
+ # This directly sets the text field to the given value, skipping exectuion of JavaScript events.
46
+ # Use +set+ if you want to run events on text fields.
47
+ #
48
+
49
+ def value=(value)
50
+ assert_enabled
51
+ assert_not_readonly
52
+ clear
53
+
54
+ insert_string value.to_s
55
+
56
+ value
57
+ end
58
+
59
+ #
60
+ # Returns the text in the text field.
61
+ #
62
+
63
+ def value
64
+ assert_exists
65
+ case @object.getTagName
66
+ when 'textarea'
67
+ @object.getText
68
+ when 'input'
69
+ @object.getValueAttribute
70
+ end
71
+ end
72
+ alias_method :get_contents, :value
73
+
74
+ #
75
+ # Append the given value to the text in the text field.
76
+ #
77
+
78
+ def append(value)
79
+ assert_enabled
80
+ assert_not_readonly
81
+ type_string value
82
+ end
83
+
84
+
85
+ def type
86
+ assert_exists
87
+ type = @object.getAttribute 'type'
88
+
89
+ NON_TEXT_TYPES.include?(type) ? type : 'text'
90
+ end
91
+
92
+ #
93
+ # This bascially just moves the text to the other text field using TextField#append
94
+ # TODO: check if HtmlUnit supports some kind of dragging.
95
+ #
96
+
97
+ def drag_contents_to(how, what)
98
+ assert_exists # assert_enabled?
99
+ val = self.value
100
+ self.value = ''
101
+ @container.text_field(how, what).append(val)
102
+ end
103
+
104
+ #
105
+ # Check if the given text fields contains the given String or Regexp.
106
+ #
107
+
108
+ def contains_text(expected_text)
109
+ assert_exists
110
+
111
+ case expected_text
112
+ when Regexp
113
+ value() =~ expected_text
114
+ when String
115
+ value().index(expected_text)
116
+ else
117
+ raise TypeError, "expected String or Regexp, got #{expected_text.inspect}:#{expected_text.class}"
118
+ end
119
+ end
120
+
121
+ #
122
+ # A boolean version of TextField#contains_text
123
+ #
124
+ # @param [String, Regexp] expected_text The text to look for.
125
+ # @return [boolean]
126
+ #
127
+
128
+ def verify_contains(expected)
129
+ # assert_exists called by contains_text
130
+ !!contains_text(expected)
131
+ end
132
+
133
+ private
134
+
135
+ def type_string(value)
136
+ java.lang.String.new(value.to_java_bytes, @browser.page.getPageEncoding).toCharArray.each do |char|
137
+ @container.update_page @object.type(char)
138
+ end
139
+ end
140
+
141
+ def insert_string(value)
142
+ case @object.getTagName
143
+ when 'textarea'
144
+ @object.setText(value)
145
+ when 'input'
146
+ @object.setValueAttribute(value)
147
+ else
148
+ raise "unknown tag name #{@object.getTagName.inspect} for #{self.class}"
149
+ end
150
+ end
151
+
152
+ end # TextField
153
+
154
+ #
155
+ # This class can be used to access hidden field objects
156
+ # Normally a user would not need to create this object as it is returned by the Celerity::Container#hidden method
157
+ #
158
+
159
+ class Hidden < TextField
160
+ TAGS = [ Identifier.new('input', :type => %w[hidden]) ]
161
+ DEFAULT_HOW = :name
162
+
163
+ def visible?
164
+ assert_exists
165
+ false
166
+ end
167
+ end
168
+
169
+ end # Celerity
@@ -0,0 +1,77 @@
1
+ module Celerity
2
+ module Exception
3
+
4
+ #
5
+ # Superclass for all Celerity exceptions.
6
+ #
7
+
8
+ class CelerityException < StandardError; end
9
+
10
+ #
11
+ # This exception is thrown if an attempt is made to access an object that doesn't exist
12
+ #
13
+
14
+ class UnknownObjectException < CelerityException; end
15
+
16
+ #
17
+ # This exception is thrown if an attempt is made to access an object that is in a disabled state
18
+ #
19
+
20
+ class ObjectDisabledException < CelerityException; end
21
+
22
+ #
23
+ # This exception is thrown if an attempt is made to access a frame that cannot be found
24
+ #
25
+
26
+ class UnknownFrameException < CelerityException; end
27
+
28
+ #
29
+ # This exception is thrown if an attempt is made to access a form that cannot be found
30
+ #
31
+
32
+ class UnknownFormException < CelerityException; end
33
+
34
+ #
35
+ # This exception is thrown if an attempt is made to access an object that is in a read-only state
36
+ #
37
+
38
+ class ObjectReadOnlyException < CelerityException; end
39
+
40
+ #
41
+ # This exception is thrown if an attempt is made to access an object when the specified value cannot be found
42
+ #
43
+
44
+ class NoValueFoundException < CelerityException; end
45
+
46
+ #
47
+ # This exception gets raised if the how argument is wrong.
48
+ #
49
+
50
+ class MissingWayOfFindingObjectException < CelerityException; end
51
+
52
+ #
53
+ # This exception is raised if an attempt is made to access a table row that doesn't exist
54
+ #
55
+
56
+ class UnknownRowException < CelerityException; end
57
+
58
+ #
59
+ # This exception is raised if an attempt is made to access a table cell that doesn't exist
60
+ #
61
+
62
+ class UnknownCellException < CelerityException; end
63
+
64
+ #
65
+ # This exception is thrown if an http error, such as a 404, 500 etc is encountered while navigating
66
+ #
67
+
68
+ class NavigationException < CelerityException; end
69
+
70
+ #
71
+ # This exception is thrown if an unexpected content type is returned by the server.
72
+ #
73
+
74
+ class UnexpectedPageException < CelerityException; end
75
+
76
+ end # Exception
77
+ end # Celerity
@@ -0,0 +1,61 @@
1
+ module Celerity
2
+ Jars = Dir[File.dirname(__FILE__) + '/htmlunit/*.jar']
3
+ Jars.each { |jar| require(jar) }
4
+
5
+ include_class org.apache.commons.httpclient.Cookie
6
+ end
7
+
8
+ module HtmlUnit
9
+ include_package 'com.gargoylesoftware.htmlunit'
10
+
11
+ module Html
12
+ include_package 'com.gargoylesoftware.htmlunit.html'
13
+ end
14
+
15
+ module Util
16
+ include_package 'com.gargoylesoftware.htmlunit.util'
17
+ end
18
+ end
19
+
20
+ module Java::OrgW3cDom::NamedNodeMap
21
+ include Enumerable
22
+
23
+ def each
24
+ 0.upto(getLength - 1) do |idx|
25
+ yield item(idx)
26
+ end
27
+ end
28
+ end
29
+
30
+ module Java::JavaLang::Iterable
31
+ include Enumerable
32
+
33
+ def each
34
+ it = iterator
35
+ yield it.next while it.hasNext
36
+ end
37
+
38
+ end unless Java::JavaLang::Iterable < Enumerable # depends on JRuby version
39
+
40
+ class Java::ComGargoylesoftwareHtmlunitHtml::HtmlPage
41
+ def inspect
42
+ '#<HtmlPage:0x%s(%s)>' % [self.hash.to_s(16), getWebResponse.getRequestUrl.toString]
43
+ end
44
+ end
45
+
46
+ class Java::ComGargoylesoftwareHtmlunitHtml::HtmlElement
47
+ def inspect
48
+ '#<%s:0x%s>' % [self.class.name.split("::").last, self.hash.to_s(16)]
49
+ end
50
+ end
51
+
52
+ class Java::ComGargoylesoftwareHtmlunitJavascriptHostHtml::HTMLElement
53
+ def method_missing(meth, *args, &blk)
54
+ m = ["jsxGet_#{meth}", "jsx_get_#{meth}"].find { |m| respond_to?(m) }
55
+ if m
56
+ __send__ m
57
+ else
58
+ super
59
+ end
60
+ end
61
+ end