watir 1.6.5 → 1.6.6.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/CHANGES +461 -326
  2. data/VERSION +1 -0
  3. data/bin/watir-console +6 -6
  4. data/lib/changes.rb +3 -3
  5. data/lib/license.rb +38 -38
  6. data/lib/readme.rb +140 -140
  7. data/lib/watir/WindowHelper.rb +49 -49
  8. data/lib/watir/camel_case.rb +66 -66
  9. data/lib/watir/clickJSDialog.rb +19 -19
  10. data/lib/watir/close_all.rb +37 -37
  11. data/lib/watir/collections.rb +392 -344
  12. data/lib/watir/container.rb +857 -815
  13. data/lib/watir/contrib/enabled_popup.rb +20 -20
  14. data/lib/watir/contrib/ie-new-process.rb +27 -27
  15. data/lib/watir/contrib/page_checker.rb +29 -29
  16. data/lib/watir/cookiemanager.rb +55 -55
  17. data/lib/watir/core.rb +28 -0
  18. data/lib/watir/core_ext.rb +17 -17
  19. data/lib/watir/datahandler.rb +107 -107
  20. data/lib/watir/dialog.rb +46 -46
  21. data/lib/watir/element.rb +351 -343
  22. data/lib/watir/element_collections.rb +97 -97
  23. data/lib/watir/form.rb +170 -170
  24. data/lib/watir/frame.rb +59 -59
  25. data/lib/watir/html_element.rb +22 -0
  26. data/lib/watir/ie-class.rb +1006 -1009
  27. data/lib/watir/ie-process.rb +39 -39
  28. data/lib/watir/ie.rb +70 -133
  29. data/lib/watir/image.rb +130 -130
  30. data/lib/watir/input_elements.rb +614 -572
  31. data/lib/watir/irb-history.rb +30 -30
  32. data/lib/watir/link.rb +64 -64
  33. data/lib/watir/locator.rb +200 -176
  34. data/lib/watir/logger.rb +19 -19
  35. data/lib/watir/modal_dialog.rb +122 -122
  36. data/lib/watir/module.rb +37 -0
  37. data/lib/watir/non_control_elements.rb +145 -145
  38. data/lib/watir/page-container.rb +116 -107
  39. data/lib/watir/popup.rb +29 -29
  40. data/lib/watir/process.rb +19 -19
  41. data/lib/watir/screen_capture.rb +115 -115
  42. data/lib/watir/setFileDialog.rb +16 -16
  43. data/lib/watir/table.rb +395 -362
  44. data/lib/watir/watir_simple.rb +475 -475
  45. data/lib/watir/win32.rb +35 -35
  46. data/lib/watir/win32ole.rb +14 -14
  47. data/lib/watir/winClicker.rb +496 -496
  48. data/rakefile.rb +70 -0
  49. data/unittests/all_tests.rb +10 -10
  50. data/unittests/buttons_xpath_test.rb +69 -69
  51. data/unittests/checkbox_test.rb +179 -179
  52. data/unittests/checkbox_xpath_test.rb +107 -107
  53. data/unittests/click_no_wait_test.rb +21 -0
  54. data/unittests/core_tests.rb +17 -17
  55. data/unittests/css_test.rb +42 -35
  56. data/unittests/defer_test.rb +46 -46
  57. data/unittests/dialog_test.rb +77 -77
  58. data/unittests/div2_xpath_test.rb +21 -21
  59. data/unittests/div_test.rb +188 -188
  60. data/unittests/div_xpath_test.rb +96 -96
  61. data/unittests/element_test.rb +49 -0
  62. data/unittests/errorchecker_test.rb +32 -22
  63. data/unittests/filefield_test.rb +39 -39
  64. data/unittests/filefield_xpath_test.rb +33 -33
  65. data/unittests/form_test.rb +280 -280
  66. data/unittests/form_xpath_test.rb +252 -252
  67. data/unittests/frame_test.rb +155 -155
  68. data/unittests/google_form_test.rb +15 -15
  69. data/unittests/html/JavascriptClick.html +39 -39
  70. data/unittests/html/blankpage.html +11 -11
  71. data/unittests/html/buttons1.html +40 -40
  72. data/unittests/html/checkboxes1.html +89 -89
  73. data/unittests/html/click_no_wait.html +14 -0
  74. data/unittests/html/complex_table.html +36 -36
  75. data/unittests/html/cssTest.html +42 -42
  76. data/unittests/html/depot_store.html +59 -59
  77. data/unittests/html/div.html +93 -93
  78. data/unittests/html/div_xml.html +20 -20
  79. data/unittests/html/fileupload.html +45 -45
  80. data/unittests/html/formTest1.html +38 -38
  81. data/unittests/html/forms2.html +44 -44
  82. data/unittests/html/forms3.html +131 -131
  83. data/unittests/html/forms4.html +27 -27
  84. data/unittests/html/frame_buttons.html +3 -3
  85. data/unittests/html/frame_links.html +3 -3
  86. data/unittests/html/frame_multi.html +4 -4
  87. data/unittests/html/google_india.html +119 -119
  88. data/unittests/html/iframeTest.html +12 -12
  89. data/unittests/html/iframeTest1.html +6 -6
  90. data/unittests/html/iframeTest2.html +5 -5
  91. data/unittests/html/images1.html +66 -66
  92. data/unittests/html/javascriptevents.html +35 -35
  93. data/unittests/html/link_pass.html +10 -10
  94. data/unittests/html/links1.html +38 -38
  95. data/unittests/html/links2.html +10 -10
  96. data/unittests/html/links_multi.html +14 -14
  97. data/unittests/html/list_matters.html +720 -720
  98. data/unittests/html/lists.html +17 -17
  99. data/unittests/html/map_test.html +31 -31
  100. data/unittests/html/modal_dialog.html +10 -10
  101. data/unittests/html/modal_dialog_launcher.html +11 -11
  102. data/unittests/html/nestedFrames.html +6 -6
  103. data/unittests/html/new_browser.html +16 -16
  104. data/unittests/html/pass.html +12 -12
  105. data/unittests/html/popups1.html +59 -59
  106. data/unittests/html/pre.html +27 -27
  107. data/unittests/html/radioButtons1.html +71 -71
  108. data/unittests/html/select_tealeaf.html +54 -54
  109. data/unittests/html/selectboxes1.html +52 -52
  110. data/unittests/html/simple_table.html +26 -26
  111. data/unittests/html/simple_table_buttons.html +104 -104
  112. data/unittests/html/simple_table_columns.html +76 -76
  113. data/unittests/html/table1.html +181 -181
  114. data/unittests/html/tableCell_using_xpath.html +19 -19
  115. data/unittests/html/table_and_tablerow_to_a.html +174 -0
  116. data/unittests/html/textarea.html +30 -30
  117. data/unittests/html/textfields1.html +88 -88
  118. data/unittests/html/textsearch.html +44 -44
  119. data/unittests/html/wallofcheckboxes.html +1003 -1003
  120. data/unittests/html/xpath_nbsp.html +12 -12
  121. data/unittests/ie_exists_test.rb +33 -33
  122. data/unittests/ie_mock.rb +94 -94
  123. data/unittests/ie_test.rb +51 -51
  124. data/unittests/images_test.rb +157 -157
  125. data/unittests/images_xpath_test.rb +91 -91
  126. data/unittests/links_multi_test.rb +48 -48
  127. data/unittests/links_test.rb +175 -175
  128. data/unittests/links_xpath_test.rb +39 -39
  129. data/unittests/lists_test.rb +32 -32
  130. data/unittests/map_test.rb +98 -98
  131. data/unittests/minmax_test.rb +31 -31
  132. data/unittests/navigate_test.rb +39 -39
  133. data/unittests/nbsp_xpath_test.rb +16 -16
  134. data/unittests/non_core_tests.rb +12 -12
  135. data/unittests/other/WindowLogonExample.rb +27 -27
  136. data/unittests/other/WindowLogonExtra.rb +6 -6
  137. data/unittests/other/all_tests_concurrent.rb +57 -57
  138. data/unittests/other/jscriptExtraAlert.rb +6 -6
  139. data/unittests/other/jscriptExtraConfirmCancel.rb +6 -6
  140. data/unittests/other/jscriptExtraConfirmOk.rb +6 -6
  141. data/unittests/other/jscriptPushButton.rb +6 -6
  142. data/unittests/other/jscript_test.rb +63 -63
  143. data/unittests/other/navigate_exception_test.rb +24 -24
  144. data/unittests/other/rexml_unit_test.rb +27 -27
  145. data/unittests/other/screen_capture_test.rb +54 -54
  146. data/unittests/other/testcase_method_order_test.rb +35 -35
  147. data/unittests/other/testcase_verify_test.rb +24 -24
  148. data/unittests/other/wait_until_test.rb +99 -99
  149. data/unittests/pagecontainstext_test.rb +69 -69
  150. data/unittests/parent_child_test.rb +43 -43
  151. data/unittests/perf_test.rb +20 -20
  152. data/unittests/popups_test.rb +43 -43
  153. data/unittests/pre_test.rb +53 -53
  154. data/unittests/radios_test.rb +212 -212
  155. data/unittests/radios_xpath_test.rb +101 -101
  156. data/unittests/security_setting_test.rb +23 -23
  157. data/unittests/selectbox_test.rb +148 -148
  158. data/unittests/selectbox_xpath_test.rb +113 -113
  159. data/unittests/setup.rb +87 -77
  160. data/unittests/speed_settings_test.rb +67 -67
  161. data/unittests/table_and_tablerow_to_a_test.rb +117 -0
  162. data/unittests/table_cell_using_xpath_test.rb +35 -35
  163. data/unittests/table_test.rb +376 -376
  164. data/unittests/table_xpath_test.rb +110 -110
  165. data/unittests/test_tests.rb +9 -9
  166. data/unittests/textarea_test.rb +92 -92
  167. data/unittests/textarea_xpath_test.rb +78 -78
  168. data/unittests/textfield_for_ch_char_test.rb +31 -31
  169. data/unittests/textfields_test.rb +218 -218
  170. data/unittests/textfields_xpath_test.rb +111 -111
  171. data/unittests/window_tests.rb +10 -10
  172. data/unittests/windows/attach_to_existing_window_test.rb +53 -53
  173. data/unittests/windows/attach_to_new_window_test.rb +83 -83
  174. data/unittests/windows/close_window_test.rb +20 -20
  175. data/unittests/windows/frame_links_test.rb +25 -25
  176. data/unittests/windows/ie-each_test.rb +47 -47
  177. data/unittests/windows/iedialog_test.rb +54 -54
  178. data/unittests/windows/js_events_test.rb +42 -55
  179. data/unittests/windows/modal_dialog_test.rb +128 -128
  180. data/unittests/windows/new_test.rb +57 -57
  181. data/unittests/windows/open_close_test.rb +18 -18
  182. data/unittests/windows/send_keys_test.rb +33 -33
  183. data/unittests/xpath_tests.rb +10 -10
  184. metadata +85 -31
  185. data/lib/watir/utils.rb +0 -20
  186. data/lib/watir/version.rb +0 -5
@@ -1,46 +1,46 @@
1
- require 'watir/ie'
2
- # TODO: move this file to watir/contrib
3
-
4
- module Watir
5
-
6
- class Dialog
7
- WindowName = 'Windows Internet Explorer'
8
- def button(name)
9
- DialogButton.new(name)
10
- end
11
- def close
12
- # TODO: register autoit before use
13
- autoit = WIN32OLE.new('AutoItX3.Control')
14
- autoit.WinClose WindowName, ""
15
- end
16
- def exists?
17
- # TODO: register autoit before use
18
- autoit = WIN32OLE.new('AutoItX3.Control')
19
- found = autoit.WinWait(WindowName, "", 1)
20
- return found == 1
21
- end
22
- end
23
-
24
- def dialog
25
- Dialog.new
26
- end
27
-
28
- class DialogButton
29
- def initialize(name)
30
- @name = name
31
- end
32
- def click
33
- # TODO: register autoit before use
34
- autoit = WIN32OLE.new('AutoItX3.Control')
35
- autoit.WinWait Dialog::WindowName, "", 1
36
- name_pattern = Regexp.new "^#{@name}$"
37
- unless name_pattern =~ autoit.WinGetText(Dialog::WindowName, "")
38
- raise Watir::Exception::UnknownObjectException
39
- end
40
- autoit.Send "{ENTER}"
41
- end
42
- end
43
-
44
- end
45
-
46
-
1
+ require 'watir/ie'
2
+ # TODO: move this file to watir/contrib
3
+
4
+ module Watir
5
+
6
+ class Dialog
7
+ WindowName = 'Windows Internet Explorer'
8
+ def button(name)
9
+ DialogButton.new(name)
10
+ end
11
+ def close
12
+ # TODO: register autoit before use
13
+ autoit = WIN32OLE.new('AutoItX3.Control')
14
+ autoit.WinClose WindowName, ""
15
+ end
16
+ def exists?
17
+ # TODO: register autoit before use
18
+ autoit = WIN32OLE.new('AutoItX3.Control')
19
+ found = autoit.WinWait(WindowName, "", 1)
20
+ return found == 1
21
+ end
22
+ end
23
+
24
+ def dialog
25
+ Dialog.new
26
+ end
27
+
28
+ class DialogButton
29
+ def initialize(name)
30
+ @name = name
31
+ end
32
+ def click
33
+ # TODO: register autoit before use
34
+ autoit = WIN32OLE.new('AutoItX3.Control')
35
+ autoit.WinWait Dialog::WindowName, "", 1
36
+ name_pattern = Regexp.new "^#{@name}$"
37
+ unless name_pattern =~ autoit.WinGetText(Dialog::WindowName, "")
38
+ raise Watir::Exception::UnknownObjectException
39
+ end
40
+ autoit.Send "{ENTER}"
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+
@@ -1,343 +1,351 @@
1
- module Watir
2
- # Base class for html elements.
3
- # This is not a class that users would normally access.
4
- class Element # Wrapper
5
- include Watir::Exception
6
- include Container # presumes @container is defined
7
- attr_accessor :container
8
-
9
- # number of spaces that separate the property from the value in the to_s method
10
- TO_S_SIZE = 14
11
-
12
- # ole_object - the ole object for the element being wrapped
13
- def initialize(ole_object)
14
- @o = ole_object
15
- @original_color = nil
16
- end
17
-
18
- # Return the ole object, allowing any methods of the DOM that Watir doesn't support to be used.
19
- def ole_object # BUG: should use an attribute reader and rename the instance variable
20
- return @o
21
- end
22
- def ole_object=(o)
23
- @o = o
24
- end
25
-
26
- def inspect
27
- '#<%s:0x%x located=%s how=%s what=%s>' % [self.class, hash*2, !!ole_object, @how.inspect, @what.inspect]
28
- end
29
-
30
- private
31
- def self.def_wrap(ruby_method_name, ole_method_name=nil)
32
- ole_method_name = ruby_method_name unless ole_method_name
33
- class_eval "def #{ruby_method_name}
34
- assert_exists
35
- ole_object.invoke('#{ole_method_name}')
36
- end"
37
- end
38
- def self.def_wrap_guard(method_name)
39
- class_eval "def #{method_name}
40
- assert_exists
41
- begin
42
- ole_object.invoke('#{method_name}')
43
- rescue
44
- ''
45
- end
46
- end"
47
- end
48
-
49
-
50
-
51
- public
52
- def assert_exists
53
- locate if respond_to?(:locate)
54
- unless ole_object
55
- raise UnknownObjectException.new(
56
- Watir::Exception.message_for_unable_to_locate(@how, @what))
57
- end
58
- end
59
- def assert_enabled
60
- unless enabled?
61
- raise ObjectDisabledException, "object #{@how} and #{@what} is disabled"
62
- end
63
- end
64
-
65
- # return the name of the element (as defined in html)
66
- def_wrap_guard :name
67
- # return the id of the element
68
- def_wrap_guard :id
69
- # return whether the element is disabled
70
- def_wrap :disabled
71
- alias disabled? disabled
72
- # return the value of the element
73
- def_wrap_guard :value
74
- # return the title of the element
75
- def_wrap_guard :title
76
- # return the style of the element
77
- def_wrap_guard :style
78
-
79
- def_wrap_guard :alt
80
- def_wrap_guard :src
81
-
82
- # return the type of the element
83
- def_wrap_guard :type # input elements only
84
- # return the url the link points to
85
- def_wrap :href # link only
86
- # return the ID of the control that this label is associated with
87
- def_wrap :for, :htmlFor # label only
88
- # return the class name of the element
89
- # raise an ObjectNotFound exception if the object cannot be found
90
- def_wrap :class_name, :className
91
- # return the unique COM number for the element
92
- def_wrap :unique_number, :uniqueNumber
93
- # Return the outer html of the object - see http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/outerhtml.asp?frame=true
94
- def_wrap :html, :outerHTML
95
-
96
- # return the text before the element
97
- def before_text # label only
98
- assert_exists
99
- begin
100
- ole_object.getAdjacentText("afterEnd").strip
101
- rescue
102
- ''
103
- end
104
- end
105
-
106
- # return the text after the element
107
- def after_text # label only
108
- assert_exists
109
- begin
110
- ole_object.getAdjacentText("beforeBegin").strip
111
- rescue
112
- ''
113
- end
114
- end
115
-
116
- # Return the innerText of the object
117
- # Raise an ObjectNotFound exception if the object cannot be found
118
- def text
119
- assert_exists
120
- return ole_object.innerText.strip
121
- end
122
-
123
- def ole_inner_elements
124
- assert_exists
125
- return ole_object.all
126
- end
127
- private :ole_inner_elements
128
-
129
- def document
130
- assert_exists
131
- return ole_object
132
- end
133
-
134
- # Return the element immediately containing self.
135
- def parent
136
- assert_exists
137
- result = Element.new(ole_object.parentelement)
138
- result.set_container self
139
- result
140
- end
141
-
142
- include Comparable
143
- def <=> other
144
- assert_exists
145
- other.assert_exists
146
- ole_object.sourceindex <=> other.ole_object.sourceindex
147
- end
148
-
149
- # Return true if self is contained earlier in the html than other.
150
- alias :before? :<
151
- # Return true if self is contained later in the html than other.
152
- alias :after? :>
153
-
154
- def typingspeed
155
- @container.typingspeed
156
- end
157
- def type_keys
158
- return @container.type_keys if @type_keys.nil?
159
- @type_keys
160
- end
161
- def activeObjectHighLightColor
162
- @container.activeObjectHighLightColor
163
- end
164
-
165
- # Return an array with many of the properties, in a format to be used by the to_s method
166
- def string_creator
167
- n = []
168
- n << "type:".ljust(TO_S_SIZE) + self.type
169
- n << "id:".ljust(TO_S_SIZE) + self.id.to_s
170
- n << "name:".ljust(TO_S_SIZE) + self.name.to_s
171
- n << "value:".ljust(TO_S_SIZE) + self.value.to_s
172
- n << "disabled:".ljust(TO_S_SIZE) + self.disabled.to_s
173
- return n
174
- end
175
- private :string_creator
176
-
177
- # Display basic details about the object. Sample output for a button is shown.
178
- # Raises UnknownObjectException if the object is not found.
179
- # name b4
180
- # type button
181
- # id b5
182
- # value Disabled Button
183
- # disabled true
184
- def to_s
185
- assert_exists
186
- return string_creator.join("\n")
187
- end
188
-
189
- # This method is responsible for setting and clearing the colored highlighting on the currently active element.
190
- # use :set to set the highlight
191
- # :clear to clear the highlight
192
- # TODO: Make this two methods: set_highlight & clear_highlight
193
- # TODO: Remove begin/rescue blocks
194
- def highlight(set_or_clear)
195
- if set_or_clear == :set
196
- begin
197
- @original_color ||= style.backgroundColor
198
- style.backgroundColor = @container.activeObjectHighLightColor
199
- rescue
200
- @original_color = nil
201
- end
202
- else # BUG: assumes is :clear, but could actually be anything
203
- begin
204
- style.backgroundColor = @original_color unless @original_color == nil
205
- rescue
206
- # we could be here for a number of reasons...
207
- # e.g. page may have reloaded and the reference is no longer valid
208
- ensure
209
- @original_color = nil
210
- end
211
- end
212
- end
213
- private :highlight
214
-
215
- # This method clicks the active element.
216
- # raises: UnknownObjectException if the object is not found
217
- # ObjectDisabledException if the object is currently disabled
218
- def click
219
- click!
220
- @container.wait
221
- end
222
-
223
- def click_no_wait
224
- assert_enabled
225
-
226
- highlight(:set)
227
- object = "#{self.class}.new(self, :unique_number, #{self.unique_number})"
228
- @page_container.eval_in_spawned_process(object + ".click!")
229
- highlight(:clear)
230
- end
231
-
232
- def click!
233
- assert_enabled
234
-
235
- highlight(:set)
236
- ole_object.click
237
- highlight(:clear)
238
- end
239
-
240
- # Flash the element the specified number of times.
241
- # Defaults to 10 flashes.
242
- def flash number=10
243
- assert_exists
244
- number.times do
245
- highlight(:set)
246
- sleep 0.05
247
- highlight(:clear)
248
- sleep 0.05
249
- end
250
- nil
251
- end
252
-
253
- # Executes a user defined "fireEvent" for objects with JavaScript events tied to them such as DHTML menus.
254
- # usage: allows a generic way to fire javascript events on page objects such as "onMouseOver", "onClick", etc.
255
- # raises: UnknownObjectException if the object is not found
256
- # ObjectDisabledException if the object is currently disabled
257
- def fire_event(event)
258
- assert_enabled
259
-
260
- highlight(:set)
261
- ole_object.fireEvent(event)
262
- @container.wait
263
- highlight(:clear)
264
- end
265
-
266
- # This method sets focus on the active element.
267
- # raises: UnknownObjectException if the object is not found
268
- # ObjectDisabledException if the object is currently disabled
269
- def focus
270
- assert_enabled
271
- ole_object.focus
272
- end
273
-
274
- # Returns whether this element actually exists.
275
- def exists?
276
- begin
277
- locate if defined?(locate)
278
- rescue WIN32OLERuntimeError
279
- @o = nil
280
- end
281
- @o ? true: false
282
- end
283
- alias :exist? :exists?
284
-
285
- # Returns true if the element is enabled, false if it isn't.
286
- # raises: UnknownObjectException if the object is not found
287
- def enabled?
288
- assert_exists
289
- return ! disabled
290
- end
291
-
292
- # If any parent element isn't visible then we cannot write to the
293
- # element. The only realiable way to determine this is to iterate
294
- # up the DOM element tree checking every element to make sure it's
295
- # visible.
296
- def visible?
297
- # Now iterate up the DOM element tree and return false if any
298
- # parent element isn't visible or is disabled.
299
- assert_exists
300
- object = @o
301
- while object
302
- begin
303
- if object.currentstyle.invoke('visibility') =~ /^hidden$/i
304
- return false
305
- end
306
- if object.currentstyle.invoke('display') =~ /^none$/i
307
- return false
308
- end
309
- if object.invoke('isDisabled')
310
- return false
311
- end
312
- rescue WIN32OLERuntimeError
313
- end
314
- object = object.parentElement
315
- end
316
- true
317
- end
318
-
319
- # Get attribute value for any attribute of the element.
320
- # Returns null if attribute doesn't exist.
321
- def attribute_value(attribute_name)
322
- assert_exists
323
- return ole_object.getAttribute(attribute_name)
324
- end
325
-
326
- end
327
-
328
- class ElementMapper # Still to be used
329
- include Container
330
-
331
- def initialize wrapper_class, container, how, what
332
- @wrapper_class = wrapper_class
333
- set_container
334
- @how = how
335
- @what = what
336
- end
337
-
338
- def method_missing method, *args
339
- locate
340
- @wrapper_class.new(@o).send(method, *args)
341
- end
342
- end
343
- end
1
+ module Watir
2
+ # Base class for html elements.
3
+ # This is not a class that users would normally access.
4
+ class Element # Wrapper
5
+ include Watir::Exception
6
+ include Container # presumes @container is defined
7
+ attr_accessor :container
8
+
9
+ # number of spaces that separate the property from the value in the to_s method
10
+ TO_S_SIZE = 14
11
+
12
+ # ole_object - the ole object for the element being wrapped
13
+ def initialize(ole_object)
14
+ @o = ole_object
15
+ @original_color = nil
16
+ end
17
+
18
+ # Return the ole object, allowing any methods of the DOM that Watir doesn't support to be used.
19
+ def ole_object # BUG: should use an attribute reader and rename the instance variable
20
+ return @o
21
+ end
22
+
23
+ def ole_object=(o)
24
+ @o = o
25
+ end
26
+
27
+ def inspect
28
+ '#<%s:0x%x located=%s how=%s what=%s>' % [self.class, hash*2, !!ole_object, @how.inspect, @what.inspect]
29
+ end
30
+
31
+ private
32
+ def self.def_wrap(ruby_method_name, ole_method_name=nil)
33
+ ole_method_name = ruby_method_name unless ole_method_name
34
+ class_eval "def #{ruby_method_name}
35
+ assert_exists
36
+ ole_object.invoke('#{ole_method_name}')
37
+ end"
38
+ end
39
+
40
+ def self.def_wrap_guard(method_name)
41
+ class_eval "def #{method_name}
42
+ assert_exists
43
+ begin
44
+ ole_object.invoke('#{method_name}')
45
+ rescue
46
+ ''
47
+ end
48
+ end"
49
+ end
50
+
51
+
52
+ public
53
+ def assert_exists
54
+ locate if respond_to?(:locate)
55
+ unless ole_object
56
+ raise UnknownObjectException.new(
57
+ Watir::Exception.message_for_unable_to_locate(@how, @what))
58
+ end
59
+ end
60
+
61
+ def assert_enabled
62
+ unless enabled?
63
+ raise ObjectDisabledException, "object #{@how} and #{@what} is disabled"
64
+ end
65
+ end
66
+
67
+ # return the name of the element (as defined in html)
68
+ def_wrap_guard :name
69
+ # return the id of the element
70
+ def_wrap_guard :id
71
+ # return whether the element is disabled
72
+ def_wrap :disabled
73
+ alias disabled? disabled
74
+ # return the value of the element
75
+ def_wrap_guard :value
76
+ # return the title of the element
77
+ def_wrap_guard :title
78
+
79
+ def_wrap_guard :currentstyle
80
+ # return current style instead of the inline style of the element
81
+ alias style currentstyle
82
+
83
+ def_wrap_guard :alt
84
+ def_wrap_guard :src
85
+
86
+ # return the type of the element
87
+ def_wrap_guard :type # input elements only
88
+ # return the url the link points to
89
+ def_wrap :href # link only
90
+ # return the ID of the control that this label is associated with
91
+ def_wrap :for, :htmlFor # label only
92
+ # return the class name of the element
93
+ # raise an ObjectNotFound exception if the object cannot be found
94
+ def_wrap :class_name, :className
95
+ # return the unique COM number for the element
96
+ def_wrap :unique_number, :uniqueNumber
97
+ # Return the outer html of the object - see http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/outerhtml.asp?frame=true
98
+ def_wrap :html, :outerHTML
99
+
100
+ # return the text before the element
101
+ def before_text # label only
102
+ assert_exists
103
+ begin
104
+ ole_object.getAdjacentText("afterEnd").strip
105
+ rescue
106
+ ''
107
+ end
108
+ end
109
+
110
+ # return the text after the element
111
+ def after_text # label only
112
+ assert_exists
113
+ begin
114
+ ole_object.getAdjacentText("beforeBegin").strip
115
+ rescue
116
+ ''
117
+ end
118
+ end
119
+
120
+ # Return the innerText of the object
121
+ # Raise an ObjectNotFound exception if the object cannot be found
122
+ def text
123
+ assert_exists
124
+ return ole_object.innerText.strip
125
+ end
126
+
127
+ def ole_inner_elements
128
+ assert_exists
129
+ return ole_object.all
130
+ end
131
+
132
+ private :ole_inner_elements
133
+
134
+ def document
135
+ assert_exists
136
+ return ole_object
137
+ end
138
+
139
+ # Return the element immediately containing self.
140
+ def parent
141
+ assert_exists
142
+ result = Element.new(ole_object.parentelement)
143
+ result.set_container self
144
+ result
145
+ end
146
+
147
+ include Comparable
148
+
149
+ def <=> other
150
+ assert_exists
151
+ other.assert_exists
152
+ ole_object.sourceindex <=> other.ole_object.sourceindex
153
+ end
154
+
155
+ # Return true if self is contained earlier in the html than other.
156
+ alias :before? :<
157
+ # Return true if self is contained later in the html than other.
158
+ alias :after? :>
159
+
160
+ def typingspeed
161
+ @container.typingspeed
162
+ end
163
+
164
+ def type_keys
165
+ return @container.type_keys if @type_keys.nil?
166
+ @type_keys
167
+ end
168
+
169
+ def activeObjectHighLightColor
170
+ @container.activeObjectHighLightColor
171
+ end
172
+
173
+ # Return an array with many of the properties, in a format to be used by the to_s method
174
+ def string_creator
175
+ n = []
176
+ n << "type:".ljust(TO_S_SIZE) + self.type
177
+ n << "id:".ljust(TO_S_SIZE) + self.id.to_s
178
+ n << "name:".ljust(TO_S_SIZE) + self.name.to_s
179
+ n << "value:".ljust(TO_S_SIZE) + self.value.to_s
180
+ n << "disabled:".ljust(TO_S_SIZE) + self.disabled.to_s
181
+ return n
182
+ end
183
+
184
+ private :string_creator
185
+
186
+ # Display basic details about the object. Sample output for a button is shown.
187
+ # Raises UnknownObjectException if the object is not found.
188
+ # name b4
189
+ # type button
190
+ # id b5
191
+ # value Disabled Button
192
+ # disabled true
193
+ def to_s
194
+ assert_exists
195
+ return string_creator.join("\n")
196
+ end
197
+
198
+ # This method is responsible for setting and clearing the colored highlighting on the currently active element.
199
+ # use :set to set the highlight
200
+ # :clear to clear the highlight
201
+ # TODO: Make this two methods: set_highlight & clear_highlight
202
+ # TODO: Remove begin/rescue blocks
203
+ def highlight(set_or_clear)
204
+ if set_or_clear == :set
205
+ begin
206
+ @original_color ||= style.backgroundColor
207
+ style.backgroundColor = @container.activeObjectHighLightColor
208
+ rescue
209
+ @original_color = nil
210
+ end
211
+ else # BUG: assumes is :clear, but could actually be anything
212
+ begin
213
+ style.backgroundColor = @original_color unless @original_color == nil
214
+ rescue
215
+ # we could be here for a number of reasons...
216
+ # e.g. page may have reloaded and the reference is no longer valid
217
+ ensure
218
+ @original_color = nil
219
+ end
220
+ end
221
+ end
222
+
223
+ private :highlight
224
+
225
+ # This method clicks the active element.
226
+ # raises: UnknownObjectException if the object is not found
227
+ # ObjectDisabledException if the object is currently disabled
228
+ def click
229
+ click!
230
+ @container.wait
231
+ end
232
+
233
+ def click_no_wait
234
+ assert_enabled
235
+
236
+ highlight(:set)
237
+ element = "#{self.class}.new(#{@page_container.attach_command}, :unique_number, #{self.unique_number})"
238
+ @page_container.click_no_wait(element)
239
+ highlight(:clear)
240
+ end
241
+
242
+ def click!
243
+ assert_enabled
244
+
245
+ highlight(:set)
246
+ ole_object.click
247
+ highlight(:clear)
248
+ end
249
+
250
+ # Flash the element the specified number of times.
251
+ # Defaults to 10 flashes.
252
+ def flash number=10
253
+ assert_exists
254
+ number.times do
255
+ highlight(:set)
256
+ sleep 0.05
257
+ highlight(:clear)
258
+ sleep 0.05
259
+ end
260
+ nil
261
+ end
262
+
263
+ # Executes a user defined "fireEvent" for objects with JavaScript events tied to them such as DHTML menus.
264
+ # usage: allows a generic way to fire javascript events on page objects such as "onMouseOver", "onClick", etc.
265
+ # raises: UnknownObjectException if the object is not found
266
+ # ObjectDisabledException if the object is currently disabled
267
+ def fire_event(event)
268
+ assert_enabled
269
+
270
+ highlight(:set)
271
+ ole_object.fireEvent(event)
272
+ @container.wait
273
+ highlight(:clear)
274
+ end
275
+
276
+ # This method sets focus on the active element.
277
+ # raises: UnknownObjectException if the object is not found
278
+ # ObjectDisabledException if the object is currently disabled
279
+ def focus
280
+ assert_enabled
281
+ ole_object.focus
282
+ end
283
+
284
+ # Returns whether this element actually exists.
285
+ def exists?
286
+ begin
287
+ locate if defined?(locate)
288
+ rescue WIN32OLERuntimeError
289
+ @o = nil
290
+ end
291
+ @o ? true: false
292
+ end
293
+
294
+ alias :exist? :exists?
295
+
296
+ # Returns true if the element is enabled, false if it isn't.
297
+ # raises: UnknownObjectException if the object is not found
298
+ def enabled?
299
+ assert_exists
300
+ return ! disabled
301
+ end
302
+
303
+ # If any parent element isn't visible then we cannot write to the
304
+ # element. The only realiable way to determine this is to iterate
305
+ # up the DOM element tree checking every element to make sure it's
306
+ # visible.
307
+ def visible?
308
+ # Now iterate up the DOM element tree and return false if any
309
+ # parent element isn't visible
310
+ assert_exists
311
+ object = @o
312
+ while object
313
+ begin
314
+ if object.currentstyle.invoke('visibility') =~ /^hidden$/i
315
+ return false
316
+ end
317
+ if object.currentstyle.invoke('display') =~ /^none$/i
318
+ return false
319
+ end
320
+ rescue WIN32OLERuntimeError
321
+ end
322
+ object = object.parentElement
323
+ end
324
+ true
325
+ end
326
+
327
+ # Get attribute value for any attribute of the element.
328
+ # Returns null if attribute doesn't exist.
329
+ def attribute_value(attribute_name)
330
+ assert_exists
331
+ return ole_object.getAttribute(attribute_name)
332
+ end
333
+
334
+ end
335
+
336
+ class ElementMapper # Still to be used
337
+ include Container
338
+
339
+ def initialize wrapper_class, container, how, what
340
+ @wrapper_class = wrapper_class
341
+ set_container
342
+ @how = how
343
+ @what = what
344
+ end
345
+
346
+ def method_missing method, *args
347
+ locate
348
+ @wrapper_class.new(@o).send(method, *args)
349
+ end
350
+ end
351
+ end