firewatir 1.2.1 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/lib/firewatir.rb +50 -0
  2. data/lib/firewatir/MozillaBaseElement.rb +1863 -0
  3. data/lib/firewatir/container.rb +534 -0
  4. data/lib/firewatir/exceptions.rb +10 -0
  5. data/lib/firewatir/firefox.rb +1127 -0
  6. data/lib/firewatir/htmlelements.rb +1911 -0
  7. data/lib/firewatir/version.rb +5 -0
  8. data/{firewatir → lib/firewatir}/winClicker.rb +0 -0
  9. data/{firewatir → lib/firewatir}/x11.rb +0 -0
  10. data/unittests/attach_to_new_window_test.rb +20 -12
  11. data/unittests/bug_fixes_test.rb +79 -69
  12. data/unittests/buttons_xpath_test.rb +45 -44
  13. data/unittests/checkbox_test.rb +86 -85
  14. data/unittests/checkbox_xpath_test.rb +58 -58
  15. data/unittests/div_test.rb +117 -115
  16. data/unittests/filefield_test.rb +12 -12
  17. data/unittests/filefield_xpath_test.rb +11 -11
  18. data/unittests/form_test.rb +134 -133
  19. data/unittests/frame_test.rb +42 -41
  20. data/unittests/hidden_test.rb +40 -40
  21. data/unittests/hidden_xpath_test.rb +32 -32
  22. data/unittests/images_test.rb +85 -84
  23. data/unittests/images_xpath_test.rb +57 -56
  24. data/unittests/iostring_test.rb +1 -1
  25. data/unittests/javascript_test.rb +42 -38
  26. data/unittests/links_test.rb +88 -86
  27. data/unittests/links_xpath_test.rb +38 -38
  28. data/unittests/mozilla_all_tests.rb +2 -14
  29. data/unittests/pre_test.rb +27 -25
  30. data/unittests/radios_test.rb +86 -86
  31. data/unittests/radios_xpath_test.rb +48 -48
  32. data/unittests/redirect_test.rb +20 -19
  33. data/unittests/selectbox_test.rb +72 -71
  34. data/unittests/selectbox_xpath_test.rb +58 -56
  35. data/unittests/setup.rb +22 -27
  36. data/unittests/table_test.rb +89 -88
  37. data/unittests/table_xpath_test.rb +37 -36
  38. data/unittests/textfields_test.rb +105 -102
  39. data/unittests/textfields_xpath_test.rb +53 -52
  40. metadata +37 -18
  41. data/MozillaBaseElement.rb +0 -1780
  42. data/container.rb +0 -900
  43. data/firewatir.rb +0 -1195
  44. data/firewatir/exceptions.rb +0 -44
  45. data/firewatir/testUnitAddons.rb +0 -8
  46. data/htmlelements.rb +0 -2281
  47. data/unittests/buttons_test.rb +0 -215
data/lib/firewatir.rb ADDED
@@ -0,0 +1,50 @@
1
+ =begin
2
+ license
3
+ ---------------------------------------------------------------------------
4
+ Copyright (c) 2004 - 2005, Paul Rogers and Bret Pettichord
5
+ Copyright (c) 2006 - 2007, Angrez Singh
6
+ Copyright (c) 2008, Bret Pettichord
7
+
8
+ Redistribution and use in source and binary forms, with or without
9
+ modification, are permitted provided that the following conditions are met:
10
+
11
+ 1. Redistributions of source code must retain the above copyright notice,
12
+ this list of conditions and the following disclaimer.
13
+
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimer in the
16
+ documentation and/or other materials provided with the distribution.
17
+
18
+ 3. Neither the names Angrez Singh nor the names of contributors to
19
+ this software may be used to endorse or promote products derived from this
20
+ software without specific prior written permission.
21
+
22
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
23
+ IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
26
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ --------------------------------------------------------------------------
34
+ (based on BSD Open Source License)
35
+ =end
36
+
37
+ require 'socket'
38
+
39
+ require 'firewatir/exceptions'
40
+ require 'firewatir/container'
41
+ require 'firewatir/MozillaBaseElement'
42
+ require 'firewatir/htmlelements'
43
+ require 'watir/matches'
44
+ require 'firewatir/firefox'
45
+ require 'firewatir/version'
46
+ require 'watir'
47
+
48
+ # this only has an effect if firewatir is required before anyone invokes
49
+ # Browser.new. Thus it has no effect when Browser.new itself autoloads this library.
50
+ Watir::Browser.default = 'firefox'
@@ -0,0 +1,1863 @@
1
+ require 'active_support'
2
+
3
+ =begin
4
+ license
5
+ ---------------------------------------------------------------------------
6
+ Copyright (c) 2006-2007, Angrez Singh
7
+
8
+ Redistribution and use in source and binary forms, with or without
9
+ modification, are permitted provided that the following conditions are met:
10
+
11
+ 1. Redistributions of source code must retain the above copyright notice,
12
+ this list of conditions and the following disclaimer.
13
+
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimer in the
16
+ documentation and/or other materials provided with the distribution.
17
+
18
+ 3. Neither the names Angrez Singh nor the names of contributors to
19
+ this software may be used to endorse or promote products derived from this
20
+ software without specific prior written permission.
21
+
22
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
23
+ IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
26
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ --------------------------------------------------------------------------
34
+ (based on BSD Open Source License)
35
+ =end
36
+
37
+ # Base class for html elements.
38
+ # This is not a class that users would normally access.
39
+ class Element
40
+ include FireWatir::Container
41
+ # Number of spaces that separate the property from the value in the to_s method
42
+ TO_S_SIZE = 14
43
+
44
+ # How to get the nodes using XPath in mozilla.
45
+ ORDERED_NODE_ITERATOR_TYPE = 5
46
+ # To get the number of nodes returned by the xpath expression
47
+ NUMBER_TYPE = 1
48
+ # To get single node value
49
+ FIRST_ORDERED_NODE_TYPE = 9
50
+ # This stores the level to which we have gone finding element inside another element.
51
+ # This is just to make sure that every element has unique name in JSSH.
52
+ @@current_level = 0
53
+ # This stores the name of the element that is about to trigger an Javascript pop up.
54
+ #@@current_js_object = nil
55
+
56
+ attr_accessor :element_name
57
+ #
58
+ # Description:
59
+ # Creates new instance of element. If argument is not nil and is of type string this
60
+ # sets the element_name and element_type property of the object. These properties can
61
+ # be accessed using element_object and element_type methods respectively.
62
+ #
63
+ # Used internally by Firewatir.
64
+ #
65
+ # Input:
66
+ # element - Name of the variable with which the element is referenced in JSSh.
67
+ #
68
+ def initialize(element, container=nil)
69
+ @container = container
70
+ @element_name = element
71
+ @element_type = element_type
72
+ #puts "in initialize "
73
+ #puts caller(0)
74
+ #if(element != nil && element.class == String)
75
+ #@element_name = element
76
+ #elsif(element != nil && element.class == Element)
77
+ # @o = element
78
+ #end
79
+
80
+ #puts "@element_name is #{@element_name}"
81
+ #puts "@element_type is #{@element_type}"
82
+ end
83
+
84
+ private
85
+ def self.def_wrap(ruby_method_name, ole_method_name = nil)
86
+ ole_method_name = ruby_method_name unless ole_method_name
87
+ class_eval "def #{ruby_method_name}
88
+ assert_exists
89
+ # Every element has its name starting from element. If yes then
90
+ # use element_name to send the command to jssh. Else its a number
91
+ # and we are still searching for element, in this case use doc.all
92
+ # array with element_name as index to send command to jssh
93
+ #puts element_object.to_s
94
+ #if(@element_type == 'HTMLDivElement')
95
+ # ole_method_name = 'innerHTML'
96
+ #end
97
+ jssh_socket.send('typeof(' + element_object + '.#{ole_method_name});\n', 0)
98
+ return_type = read_socket()
99
+
100
+ return_value = get_attribute_value(\"#{ole_method_name}\")
101
+
102
+ #if(return_value == '' || return_value == \"null\")
103
+ # return_value = \"\"
104
+ #end
105
+
106
+ if(return_type == \"boolean\")
107
+ return_value = false if return_value == \"false\"
108
+ return_value = true if return_value == \"true\"
109
+ end
110
+ #puts return_value
111
+ @@current_level = 0
112
+ return return_value
113
+ end"
114
+ end
115
+
116
+ def get_attribute_value(attribute_name)
117
+ #if the attribut name is columnLength get number of cells in first row if rows exist.
118
+ case attribute_name
119
+ when "columnLength"
120
+ rowsLength = js_eval_method "columns"
121
+ if (rowsLength != 0 || rowsLength != "")
122
+ return js_eval_method("rows[0].cells.length")
123
+ end
124
+ when "text"
125
+ return text
126
+ when "url", "href", "src", "action", "name"
127
+ return_value = js_eval_method("getAttribute(\"#{attribute_name}\")")
128
+ else
129
+ jssh_command = "var attribute = '';
130
+ if(#{element_object}.#{attribute_name} != undefined)
131
+ attribute = #{element_object}.#{attribute_name};
132
+ else
133
+ attribute = #{element_object}.getAttribute(\"#{attribute_name}\");
134
+ attribute;"
135
+ return_value = js_eval(jssh_command)
136
+ end
137
+ if attribute_name == "value"
138
+ tagName = js_eval_method("tagName").downcase
139
+ type = js_eval_method("type").downcase
140
+
141
+ if tagName == "button" || ["image", "submit", "reset", "button"].include?(type)
142
+ if return_value == "" || return_value == "null"
143
+ return_value = js_eval_method "innerHTML"
144
+ end
145
+ end
146
+ end
147
+
148
+ if return_value == "null" || return_value =~ /\[object\s.*\]/
149
+ return_value = ""
150
+ end
151
+ return return_value
152
+ end
153
+ private:get_attribute_value
154
+
155
+
156
+ #
157
+ # Description:
158
+ # Returns an array of the properties of an element, in a format to be used by the to_s method.
159
+ # additional attributes are returned based on the supplied atributes hash.
160
+ # name, type, id, value and disabled attributes are common to all the elements.
161
+ # This method is used internally by to_s method.
162
+ #
163
+ # Output:
164
+ # Array with values of the following properties:
165
+ # name, type, id, value disabled and the supplied attribues list.
166
+ #
167
+ def string_creator(attributes = nil)
168
+ n = []
169
+ n << "name:".ljust(TO_S_SIZE) + get_attribute_value("name")
170
+ n << "type:".ljust(TO_S_SIZE) + get_attribute_value("type")
171
+ n << "id:".ljust(TO_S_SIZE) + get_attribute_value("id")
172
+ n << "value:".ljust(TO_S_SIZE) + get_attribute_value("value")
173
+ n << "disabled:".ljust(TO_S_SIZE) + get_attribute_value("disabled")
174
+ #n << "style:".ljust(TO_S_SIZE) + get_attribute_value("style")
175
+ #n << "class:".ljust(TO_S_SIZE) + get_attribute_value("className")
176
+
177
+ if(attributes != nil)
178
+ attributes.each do |key,value|
179
+ n << "#{key}:".ljust(TO_S_SIZE) + get_attribute_value(value)
180
+ end
181
+ end
182
+ return n
183
+ end
184
+
185
+ #
186
+ # Description:
187
+ # Sets and clears the colored highlighting on the currently active element.
188
+ #
189
+ # Input:
190
+ # set_or_clear - this can have following two values
191
+ # :set - To set the color of the element.
192
+ # :clear - To clear the color of the element.
193
+ #
194
+ def highlight(set_or_clear)
195
+ if set_or_clear == :set
196
+ #puts "element_name is : #{element_object}"
197
+ jssh_command = " var original_color = #{element_object}.style.background;"
198
+ jssh_command += " #{element_object}.style.background = \"#{DEFAULT_HIGHLIGHT_COLOR}\"; original_color;"
199
+
200
+ # TODO: Need to change this so that it would work if user sets any other color.
201
+ #puts "color is : #{DEFAULT_HIGHLIGHT_COLOR}"
202
+ jssh_socket.send("#{jssh_command}\n", 0)
203
+ @original_color = read_socket()
204
+
205
+ else # BUG: assumes is :clear, but could actually be anything
206
+ begin
207
+ jssh_socket.send("#{element_object}.style.background = \"#{@original_color}\";\n", 0)
208
+ read_socket()
209
+ rescue
210
+ # we could be here for a number of reasons...
211
+ # e.g. page may have reloaded and the reference is no longer valid
212
+ ensure
213
+ @original_color = nil
214
+ end
215
+ end
216
+ end
217
+ protected :highlight
218
+
219
+ #
220
+ # Description:
221
+ # Returns array of rows for a given table. Returns nil if calling element is not of table type.
222
+ #
223
+ # Output:
224
+ # Array of row elements in an table or nil
225
+ #
226
+ def get_rows()
227
+ #puts "#{element_object} and #{element_type}"
228
+ if(element_type == "HTMLTableElement")
229
+ jssh_socket.send("#{element_object}.rows.length;\n", 0)
230
+ length = read_socket().to_i
231
+ #puts "The number of rows in the table are : #{no_of_rows}"
232
+ return_array = Array.new(length)
233
+ for i in 0..length - 1 do
234
+ return_array[i] = "#{element_object}.rows[#{i}]"
235
+ end
236
+ return return_array
237
+ else
238
+ puts "Trying to access rows for Element of type #{element_type}. Element must be of table type to execute this function."
239
+ return nil
240
+ end
241
+ end
242
+ private :get_rows
243
+
244
+ def set_specifier(how, what)
245
+ if how.class == Hash and what.nil?
246
+ specifiers = how
247
+ else
248
+ specifiers = {how => what}
249
+ end
250
+
251
+ @specifiers = {:index => 1} # default if not specified
252
+
253
+ specifiers.each do |how, what|
254
+ what = what.to_i if how == :index
255
+ how = :href if how == :url
256
+ how = :value if how == :caption
257
+ how = :class if how == :class_name
258
+
259
+ @specifiers[how] = what
260
+ end
261
+ end
262
+
263
+ #
264
+ # Description:
265
+ # Locates the element on the page depending upon the parameters passed. Logic for locating the element is written
266
+ # in JavaScript and then send to JSSh; so that we don't make small round-trips via socket to JSSh. This is done to
267
+ # improve the performance for locating the element.
268
+ #
269
+ # Input:
270
+ # tag - Tag name of the element to be located like "input", "a" etc. This is case insensitive.
271
+ # how - The attribute by which you want to locate the element like id, name etc. You can use any attribute-value pair
272
+ # that uniquely identifies that element on the page. If there are more that one element that have identical
273
+ # attribute-value pair then first element that is found while traversing the DOM will be returned.
274
+ # what - The value of the attribute specified by how.
275
+ # types - Used if that HTML element to be located has different type like input can be of type image, button etc.
276
+ # Default value is nil
277
+ # value - This is used only in case of radio buttons where they have same name but different value.
278
+ #
279
+ # Output:
280
+ # Returns nil if unable to locate the element, else return the element.
281
+ #
282
+ def locate_tagged_element(tag, how, what, types = nil, value = nil)
283
+ #puts caller(0)
284
+ # how = :value if how == :caption
285
+ # how = :href if how == :url
286
+ set_specifier(how, what)
287
+ #puts "(locate_tagged_element)current element is : #{@container.class} and tag is #{tag}"
288
+ # If there is no current element i.e. element in current context we are searching the whole DOM tree.
289
+ # So get all the elements.
290
+
291
+ if(types != nil and types.include?("button"))
292
+ jssh_command = "var isButtonElement = true;"
293
+ else
294
+ jssh_command = "var isButtonElement = false;"
295
+ end
296
+
297
+ # Because in both the below cases we need to get element with respect to document.
298
+ # when we locate a frame document is automatically adjusted to point to HTML inside the frame
299
+ if(@container.class == FireWatir::Firefox || @container.class == Frame)
300
+ #end
301
+ #if(@@current_element_object == "")
302
+ jssh_command += "var elements_#{tag} = null; elements_#{tag} = #{DOCUMENT_VAR}.getElementsByTagName(\"#{tag}\");"
303
+ if(types != nil and (types.include?("textarea") or types.include?("button")) )
304
+ jssh_command += "elements_#{tag} = #{DOCUMENT_VAR}.body.getElementsByTagName(\"*\");"
305
+ end
306
+ # @@has_changed = true
307
+ else
308
+ #puts "container name is: " + @container.element_name
309
+ #locate if defined? locate
310
+ #@container.locate
311
+ jssh_command += "var elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"#{tag}\");"
312
+ if(types != nil and (types.include?("textarea") or types.include?("button") ) )
313
+ jssh_command += "elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"*\");"
314
+ end
315
+ # @@has_changed = false
316
+ end
317
+
318
+
319
+ if(types != nil)
320
+ jssh_command += "var types = new Array("
321
+ count = 0
322
+ types.each do |type|
323
+ if count == 0
324
+ jssh_command += "\"#{type}\""
325
+ count += 1
326
+ else
327
+ jssh_command += ",\"#{type}\""
328
+ end
329
+ end
330
+ jssh_command += ");"
331
+ else
332
+ jssh_command += "var types = null;"
333
+ end
334
+ #jssh_command += "var elements = #{element_object}.getElementsByTagName('*');"
335
+ jssh_command += "var object_index = 1; var o = null; var element_name = \"\";"
336
+
337
+ if(value == nil)
338
+ jssh_command += "var value = null;"
339
+ else
340
+ jssh_command += "var value = \"#{value}\";"
341
+ end
342
+
343
+
344
+ #add hash arrays
345
+ sKey = "var hashKeys = new Array("
346
+ sVal = "var hashValues = new Array("
347
+ @specifiers.each do |k,v|
348
+ sKey += "\"#{k}\","
349
+ if v.class == Regexp
350
+ oldRegExp = v.to_s
351
+ newRegExp = v.inspect
352
+ flags = oldRegExp.slice(2, oldRegExp.index(':') - 2)
353
+
354
+ for i in 0..flags.length do
355
+ flag = flags[i, 1]
356
+ if(flag == '-')
357
+ break;
358
+ else
359
+ newRegExp << flag
360
+ end
361
+ end
362
+ sVal += "#{newRegExp},"
363
+ else
364
+ sVal += "\"#{v}\","
365
+ end
366
+ end
367
+ sKey = sKey[0..sKey.length-2]
368
+ sVal = sVal[0..sVal.length-2]
369
+ jssh_command += sKey + ");"
370
+ jssh_command += sVal + ");"
371
+
372
+ #index
373
+ jssh_command += "var target_index = 1;
374
+ for(var k=0; k<hashKeys.length; k++)
375
+ {
376
+ if(hashKeys[k] == \"index\")
377
+ {
378
+ target_index = parseInt(hashValues[k]);
379
+ break;
380
+ }
381
+ }"
382
+
383
+ #jssh_command += "elements.length;"
384
+ if(@container.class == FireWatir::Firefox || @container.class == Frame)
385
+
386
+ jssh_command += "for(var i=0; i<elements_#{tag}.length; i++)
387
+ {
388
+ if(element_name != \"\") break;
389
+ var element = elements_#{tag}[i];"
390
+ else
391
+ jssh_command += "for(var i=0; i<elements_#{@@current_level}_#{tag}.length; i++)
392
+ {
393
+ if(element_name != \"\") break;
394
+ var element = elements_#{@@current_level}_#{tag}[i];"
395
+ end
396
+
397
+ # Because in IE for button the value of "value" attribute also corresponds to the innerHTML if value attribute
398
+ # is not supplied. For e.g.: <button>Sign In</button>, in this case value of "value" attribute is "Sign In"
399
+ # though value attribute is not supplied. But for Firefox value of "value" attribute is null. So to make sure
400
+ # script runs on both IE and Watir we are also considering innerHTML if element is of button type.
401
+ jssh_command += " var attribute = \"\";
402
+ var same_type = false;
403
+ if(types)
404
+ {
405
+ for(var j=0; j<types.length; j++)
406
+ {
407
+ if(types[j] == element.type || types[j] == element.tagName)
408
+ {
409
+ same_type = true;
410
+ break;
411
+ }
412
+ }
413
+ }
414
+ else
415
+ {
416
+ same_type = true;
417
+ }
418
+ if(same_type == true)
419
+ {
420
+ var how = \"\";
421
+ var what = null;
422
+ attribute = \"\";
423
+ for(var k=0; k<hashKeys.length; k++)
424
+ {
425
+ how = hashKeys[k];
426
+ what = hashValues[k];
427
+
428
+ if(how == \"index\")
429
+ {
430
+ attribute = parseInt(what);
431
+ what = parseInt(what);
432
+ }
433
+ else
434
+ {
435
+ if(how == \"text\")
436
+ {
437
+ attribute = element.textContent.replace(/\\xA0/g,' ').replace(/^\\s+|\\s+$/g, '').replace(/\\s+/g, ' ')
438
+ }
439
+ else
440
+ {
441
+ if(how == \"href\" || how == \"src\" || how == \"action\" || how == \"name\")
442
+ {
443
+ attribute = element.getAttribute(how);
444
+ }
445
+ else
446
+ {
447
+ if(eval(\"element.\"+how) != undefined)
448
+ attribute = eval(\"element.\"+how);
449
+ else
450
+ attribute = element.getAttribute(how);
451
+ }
452
+ }
453
+ if(\"value\" == how && isButtonElement && (attribute == null || attribute == \"\"))
454
+ {
455
+ attribute = element.innerHTML;
456
+ }
457
+ }
458
+ if(attribute == \"\") o = 'NoMethodError';
459
+ var found = false;
460
+ if (typeof what == \"object\" || typeof what == \"function\")
461
+ {
462
+ var regExp = new RegExp(what);
463
+ found = regExp.test(attribute);
464
+ }
465
+ else
466
+ {
467
+ found = (attribute == what);
468
+ }"
469
+
470
+ # if(what.class == Regexp)
471
+ # # Construct the regular expression because we can't use it directly by converting it to string.
472
+ # # If reg ex is /Google/i then its string conversion will be (?i-mx:Google) so we can't use it.
473
+ # # Construct the regular expression again from the string conversion.
474
+ # oldRegExp = what.to_s
475
+ # newRegExp = what.inspect
476
+ # flags = oldRegExp.slice(2, oldRegExp.index(':') - 2)
477
+
478
+ # for i in 0..flags.length do
479
+ # flag = flags[i, 1]
480
+ # if(flag == '-')
481
+ # break;
482
+ # else
483
+ # newRegExp << flag
484
+ # end
485
+ # end
486
+ # #puts "old reg ex is #{what} new reg ex is #{newRegExp}"
487
+ # jssh_command += " var regExp = new RegExp(#{newRegExp});
488
+ # found = regExp.test(attribute);"
489
+ # elsif(how == :index)
490
+ # jssh_command += " found = (attribute == #{what});"
491
+ # else
492
+ # jssh_command += " found = (attribute == \"#{what}\");"
493
+ # end
494
+
495
+
496
+ #jssh_command += " found;"
497
+ if(@container.class == FireWatir::Firefox || @container.class == Frame)
498
+ jssh_command += " if(found)
499
+ {
500
+ if(value)
501
+ {
502
+ if(element.value == \"#{value}\")
503
+ {
504
+ o = element;
505
+ element_name = \"elements_#{tag}[\" + i + \"]\";
506
+ }
507
+ else
508
+ break;
509
+ }
510
+ else
511
+ {
512
+ o = element;
513
+ element_name = \"elements_#{tag}[\" + i + \"]\";
514
+ }
515
+ }"
516
+ else
517
+ jssh_command += " if(found)
518
+ {
519
+ if(value)
520
+ {
521
+ if(element.value == \"#{value}\")
522
+ {
523
+ o = element;
524
+ element_name = \"elements_#{@@current_level}_#{tag}[\" + i + \"]\";
525
+ }
526
+ else
527
+ break;
528
+ }
529
+ else
530
+ {
531
+ o = element;
532
+ element_name = \"elements_#{@@current_level}_#{tag}[\" + i + \"]\";
533
+ }
534
+ }"
535
+ end
536
+
537
+ jssh_command += "
538
+ else {
539
+ o = null;
540
+ element_name = \"\";
541
+ break;
542
+ }
543
+ }
544
+ if(element_name != \"\")
545
+ {
546
+ if(target_index == object_index)
547
+ {
548
+ break;
549
+ }
550
+ else if(target_index < object_index)
551
+ {
552
+ element_name = \"\";
553
+ o = null;
554
+ break;
555
+ }
556
+ else
557
+ {
558
+ object_index += 1;
559
+ element_name = \"\";
560
+ o = null;
561
+ }
562
+ }
563
+ }
564
+ }
565
+ element_name;"
566
+
567
+ # Remove \n that are there in the string as a result of pressing enter while formatting.
568
+ jssh_command.gsub!(/\n/, "")
569
+ #puts jssh_command
570
+ #out = File.new("c:\\result.log", "w")
571
+ #out << jssh_command
572
+ #out.close
573
+ jssh_socket.send("#{jssh_command};\n", 0)
574
+ element_name = read_socket();
575
+ #puts "element name in find control is : #{element_name}"
576
+ @@current_level = @@current_level + 1
577
+ #puts @container
578
+ #puts element_name
579
+ if(element_name != "")
580
+ return element_name #Element.new(element_name, @container)
581
+ else
582
+ return nil
583
+ end
584
+ end
585
+
586
+ #
587
+ # Description:
588
+ # Locates frame element. Logic for locating the frame is written in JavaScript so that we don't make small
589
+ # round trips to JSSh using socket. This is done to improve the performance for locating the element.
590
+ #
591
+ # Input:
592
+ # how - The attribute for locating the frame. You can use any attribute-value pair that uniquely identifies
593
+ # the frame on the page. If there are more than one frames that have identical attribute-value pair
594
+ # then first frame that is found while traversing the DOM will be returned.
595
+ # what - Value of the attribute specified by how
596
+ #
597
+ # Output:
598
+ # Nil if unable to locate frame, else return the Frame element.
599
+ #
600
+ def locate_frame(how, what)
601
+ # Get all the frames the are there on the page.
602
+ #puts "how is #{how} and what is #{what}"
603
+ jssh_command = ""
604
+ if(@container.class == FireWatir::Firefox)
605
+ # In firefox 3 if you write Frame Name then it will not show anything. So we add .toString function to every element.
606
+ jssh_command = "var frameset = #{WINDOW_VAR}.frames;
607
+ var elements_frames = new Array();
608
+ for(var i = 0; i < frameset.length; i++)
609
+ {
610
+ var frames = frameset[i].frames;
611
+ for(var j = 0; j < frames.length; j++)
612
+ {
613
+ frames[j].frameElement.toString = function() { return '[object HTMLFrameElement]'; };
614
+ elements_frames.push(frames[j].frameElement);
615
+
616
+ }
617
+ }"
618
+ else
619
+ jssh_command = "var frames = #{@container.element_name}.contentWindow.frames;
620
+ var elements_frames_#{@@current_level} = new Array();
621
+ for(var i = 0; i < frames.length; i++)
622
+ {
623
+ elements_frames_#{@@current_level}.push(frames[i].frameElement);
624
+ }"
625
+ end
626
+
627
+ jssh_command +=" var element_name = ''; var object_index = 1;var attribute = '';
628
+ var element = '';"
629
+ if(@container.class == FireWatir::Firefox)
630
+ jssh_command += "for(var i = 0; i < elements_frames.length; i++)
631
+ {
632
+ element = elements_frames[i];"
633
+ else
634
+ jssh_command += "for(var i = 0; i < elements_frames_#{@@current_level}.length; i++)
635
+ {
636
+ element = elements_frames_#{@@current_level}[i];"
637
+ end
638
+ jssh_command += " if(\"index\" == \"#{how}\")
639
+ {
640
+ attribute = object_index; object_index += 1;
641
+ }
642
+ else
643
+ {
644
+ attribute = element.getAttribute(\"#{how}\");
645
+ if(attribute == \"\" || attribute == null)
646
+ {
647
+ attribute = element.#{how};
648
+ }
649
+ }
650
+ var found = false;"
651
+ if(what.class == Regexp)
652
+ oldRegExp = what.to_s
653
+ newRegExp = "/" + what.source + "/"
654
+ flags = oldRegExp.slice(2, oldRegExp.index(':') - 2)
655
+
656
+ for i in 0..flags.length do
657
+ flag = flags[i, 1]
658
+ if(flag == '-')
659
+ break;
660
+ else
661
+ newRegExp << flag
662
+ end
663
+ end
664
+ #puts "old reg ex is #{what} new reg ex is #{newRegExp}"
665
+ jssh_command += " var regExp = new RegExp(#{newRegExp});
666
+ found = regExp.test(attribute);"
667
+ elsif(how == :index)
668
+ jssh_command += " found = (attribute == #{what});"
669
+ else
670
+ jssh_command += " found = (attribute == \"#{what}\");"
671
+ end
672
+
673
+ jssh_command += " if(found)
674
+ {"
675
+ if(@container.class == FireWatir::Firefox)
676
+ jssh_command += " element_name = \"elements_frames[\" + i + \"]\";
677
+ #{DOCUMENT_VAR} = elements_frames[i].contentDocument;
678
+ #{BODY_VAR} = #{DOCUMENT_VAR}.body;"
679
+ else
680
+ jssh_command += " element_name = \"elements_frames_#{@@current_level}[\" + i + \"]\";
681
+ #{DOCUMENT_VAR} = elements_frames_#{@@current_level}[i].contentDocument;
682
+ #{BODY_VAR} = #{DOCUMENT_VAR}.body;"
683
+ end
684
+ jssh_command += " break;
685
+ }
686
+ }
687
+ element_name;"
688
+
689
+ jssh_command.gsub!("\n", "")
690
+ #puts "jssh_command for finding frame is : #{jssh_command}"
691
+
692
+ jssh_socket.send("#{jssh_command};\n", 0)
693
+ element_name = read_socket()
694
+ @@current_level = @@current_level + 1
695
+ #puts "element_name for frame is : #{element_name}"
696
+
697
+ if(element_name != "")
698
+ return element_name
699
+ else
700
+ return nil
701
+ end
702
+ end
703
+
704
+ def get_frame_html
705
+ jssh_socket.send("var htmlelem = #{DOCUMENT_VAR}.getElementsByTagName('html')[0]; htmlelem.innerHTML;\n", 0)
706
+ #jssh_socket.send("#{BODY_VAR}.innerHTML;\n", 0)
707
+ result = read_socket()
708
+ return "<html>" + result + "</html>"
709
+ end
710
+
711
+ def submit_form
712
+ #puts "form name is : #{element_object}"
713
+ jssh_socket.send("#{element_object}.submit();\n" , 0)
714
+ read_socket()
715
+ end
716
+
717
+ public
718
+
719
+ #
720
+ #
721
+ # Description:
722
+ # Matches the given text with the current text shown in the browser for that particular element.
723
+ #
724
+ # Input:
725
+ # target - Text to match. Can be a string or regex
726
+ #
727
+ # Output:
728
+ # Returns the index if the specified text was found.
729
+ # Returns matchdata object if the specified regexp was found.
730
+ #
731
+ def contains_text(target)
732
+ #puts "Text to match is : #{match_text}"
733
+ #puts "Html is : #{self.text}"
734
+ if target.kind_of? Regexp
735
+ self.text.match(target)
736
+ elsif target.kind_of? String
737
+ self.text.index(target)
738
+ else
739
+ raise ArgumentError, "Argument #{target} should be a string or regexp."
740
+ end
741
+ end
742
+ #
743
+ # Description:
744
+ # Method for inspecting the object. Defined here because IRB was not able to locate the object.
745
+ # TODO: Need to find out why IRB is unable to find object though both (this and IRB) are executing same statements
746
+ #
747
+ def inspect
748
+ assert_exists
749
+ puts self.to_s
750
+ end
751
+
752
+ #
753
+ # Description:
754
+ # Returns array of elements that matches a given XPath query.
755
+ # Mozilla browser directly supports XPath query on its DOM. So no need to create the DOM tree as WATiR does for IE.
756
+ # Refer: http://developer.mozilla.org/en/docs/DOM:document.evaluate
757
+ # Used internally by Firewatir use ff.elements_by_xpath instead.
758
+ #
759
+ # Input:
760
+ # xpath - The xpath expression or query.
761
+ #
762
+ # Output:
763
+ # Array of elements that matched the xpath expression provided as parameter.
764
+ #
765
+ def elements_by_xpath(container, xpath)
766
+ rand_no = rand(1000)
767
+ #jssh_command = "var xpathResult = #{DOCUMENT_VAR}.evaluate(\"count(#{xpath})\", #{DOCUMENT_VAR}, null, #{NUMBER_TYPE}, null); xpathResult.numberValue;"
768
+ #jssh_socket.send("#{jssh_command}\n", 0);
769
+ #node_count = read_socket()
770
+
771
+ jssh_command = "var element_xpath_#{rand_no} = new Array();"
772
+
773
+ jssh_command += "var result = #{DOCUMENT_VAR}.evaluate(\"#{xpath}\", #{DOCUMENT_VAR}, null, #{ORDERED_NODE_ITERATOR_TYPE}, null);
774
+ var iterate = result.iterateNext();
775
+ while(iterate)
776
+ {
777
+ element_xpath_#{rand_no}.push(iterate);
778
+ iterate = result.iterateNext();
779
+ }
780
+ element_xpath_#{rand_no}.length;
781
+ "
782
+
783
+ # Remove \n that are there in the string as a result of pressing enter while formatting.
784
+ jssh_command.gsub!(/\n/, "")
785
+ #puts jssh_command
786
+ jssh_socket.send("#{jssh_command};\n", 0)
787
+ node_count = read_socket()
788
+ #puts "value of count is : #{node_count}"
789
+
790
+ elements = Array.new(node_count.to_i)
791
+
792
+ for i in 0..elements.length - 1 do
793
+ elements[i] = "element_xpath_#{rand_no}[#{i}]"
794
+ end
795
+
796
+ return elements;
797
+ end
798
+
799
+ #
800
+ # Description:
801
+ # Returns first element found while traversing the DOM; that matches an given XPath query.
802
+ # Mozilla browser directly supports XPath query on its DOM. So no need to create the DOM tree as WATiR does for IE.
803
+ # Refer: http://developer.mozilla.org/en/docs/DOM:document.evaluate
804
+ # Used internally by Firewatir use ff.element_by_xpath instead.
805
+ #
806
+ # Input:
807
+ # xpath - The xpath expression or query.
808
+ #
809
+ # Output:
810
+ # First element in DOM that matched the XPath expression or query.
811
+ #
812
+ def element_by_xpath(container, xpath)
813
+ #puts "here locating element by xpath"
814
+ rand_no = rand(1000)
815
+ jssh_command = "var element_xpath_#{rand_no} = null; element_xpath_#{rand_no} = #{DOCUMENT_VAR}.evaluate(\"#{xpath}\", #{DOCUMENT_VAR}, null, #{FIRST_ORDERED_NODE_TYPE}, null).singleNodeValue; element_xpath_#{rand_no};"
816
+
817
+ jssh_socket.send("#{jssh_command}\n", 0)
818
+ result = read_socket()
819
+ #puts "command send to jssh is : #{jssh_command}"
820
+ #puts "result is : #{result}"
821
+ if(result == "null" || result == "" || result.include?("exception"))
822
+ @@current_level = 0
823
+ return nil
824
+ else
825
+ @@current_level += 1
826
+ return "element_xpath_#{rand_no}"
827
+ end
828
+ end
829
+
830
+ #
831
+ # Description:
832
+ # Returns the name of the element with which we can access it in JSSh.
833
+ # Used internally by Firewatir to execute methods, set properties or return property value for the element.
834
+ #
835
+ # Output:
836
+ # Name of the variable with which element is referenced in JSSh
837
+ #
838
+ def element_object
839
+ #puts caller.join("\n")
840
+ #puts "In element_object element name is : #{@element_name}"
841
+ #puts "in element_object : #{@container.class}"
842
+ #if(@container.class == FireWatir::Firefox)
843
+ return @element_name #if @element_name != nil
844
+ #else
845
+ # return @container.element_name
846
+ #end
847
+ #return @o.element_name if @o != nil
848
+ end
849
+ private :element_object
850
+
851
+ #
852
+ # Description:
853
+ # Returns the type of element. For e.g.: HTMLAnchorElement. used internally by Firewatir
854
+ #
855
+ # Output:
856
+ # Type of the element.
857
+ #
858
+ def element_type
859
+ #puts "in element_type object is : #{element_object}"
860
+ # Get the type of the element.
861
+ jssh_socket.send("#{element_object};\n", 0)
862
+ temp = read_socket()
863
+
864
+ if temp == ""
865
+ return nil
866
+ end
867
+
868
+ #puts "#{element_object} and type is #{temp}"
869
+ temp =~ /\[object\s(.*)\]/
870
+ if $1
871
+ return $1
872
+ else
873
+ # This is done because in JSSh if you write element name of anchor type
874
+ # then it displays the link to which it navigates instead of displaying
875
+ # object type. So above regex match will return nil
876
+ return "HTMLAnchorElement"
877
+ end
878
+ end
879
+ #private :element_type
880
+
881
+ #
882
+ # Description:
883
+ # Fires the provided event for an element and by default waits for the action to get completed.
884
+ #
885
+ # Input:
886
+ # event - Event to be fired like "onclick", "onchange" etc.
887
+ # wait - Whether to wait for the action to get completed or not. By default its true.
888
+ #
889
+ # TODO: Provide ability to specify event parameters like keycode for key events, and click screen
890
+ # coordinates for mouse events.
891
+ def fire_event(event, wait = true)
892
+ assert_exists()
893
+ event = event.to_s # in case event was given as a symbol
894
+
895
+ event = event.downcase
896
+
897
+ event =~ /on(.*)/i
898
+ event = $1 if $1
899
+
900
+ # check if we've got an old-school on-event
901
+ #jssh_socket.send("typeof(#{element_object}.#{event});\n", 0)
902
+ #is_defined = read_socket()
903
+
904
+ # info about event types harvested from:
905
+ # http://www.howtocreate.co.uk/tutorials/javascript/domevents
906
+ case event
907
+ when 'abort', 'blur', 'change', 'error', 'focus', 'load', 'reset', 'resize',
908
+ 'scroll', 'select', 'submit', 'unload'
909
+ dom_event_type = 'HTMLEvents'
910
+ dom_event_init = "initEvent(\"#{event}\", true, true)"
911
+ when 'keydown', 'keypress', 'keyup'
912
+ dom_event_type = 'KeyEvents'
913
+ # Firefox has a proprietary initializer for keydown/keypress/keyup.
914
+ # Args are as follows:
915
+ # 'type', bubbles, cancelable, windowObject, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode
916
+ dom_event_init = "initKeyEvent(\"#{event}\", true, true, #{WINDOW_VAR}, false, false, false, false, 0, 0)"
917
+ when 'click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover',
918
+ 'mouseup'
919
+ dom_event_type = 'MouseEvents'
920
+ # Args are as follows:
921
+ # 'type', bubbles, cancelable, windowObject, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget
922
+ dom_event_init = "initMouseEvent(\"#{event}\", true, true, #{WINDOW_VAR}, 1, 0, 0, 0, 0, false, false, false, false, 0, null)"
923
+ else
924
+ dom_event_type = 'HTMLEvents'
925
+ dom_event_init = "initEvents(\"#{event}\", true, true)"
926
+ end
927
+
928
+ if(element_type == "HTMLSelectElement")
929
+ dom_event_type = 'HTMLEvents'
930
+ dom_event_init = "initEvent(\"#{event}\", true, true)"
931
+ end
932
+
933
+
934
+ jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"#{dom_event_type}\"); "
935
+ jssh_command << "event.#{dom_event_init}; "
936
+ jssh_command << "#{element_object}.dispatchEvent(event);"
937
+
938
+ #puts "JSSH COMMAND:\n#{jssh_command}\n"
939
+
940
+ jssh_socket.send("#{jssh_command}\n", 0)
941
+ read_socket() if wait
942
+ wait() if wait
943
+
944
+ @@current_level = 0
945
+ end
946
+ alias fireEvent fire_event
947
+
948
+ #
949
+ # Description:
950
+ # Returns the value of the specified attribute of an element.
951
+ #
952
+ def attribute_value(attribute_name)
953
+ #puts attribute_name
954
+ assert_exists()
955
+ return_value = get_attribute_value(attribute_name)
956
+ @@current_level = 0
957
+ return return_value
958
+ end
959
+
960
+ #
961
+ # Description:
962
+ # Checks if element exists or not. Raises UnknownObjectException if element doesn't exists.
963
+ #
964
+ def assert_exists
965
+ unless exists?
966
+ raise UnknownObjectException.new(
967
+ Watir::Exception.message_for_unable_to_locate(@how, @what))
968
+ end
969
+ end
970
+
971
+ #
972
+ # Description:
973
+ # Checks if element is enabled or not. Raises ObjectDisabledException if object is disabled and
974
+ # you are trying to use the object.
975
+ #
976
+ def assert_enabled
977
+ unless enabled?
978
+ raise ObjectDisabledException, "object #{@how} and #{@what} is disabled"
979
+ end
980
+ end
981
+
982
+ #
983
+ # Description:
984
+ # First checks if element exists or not. Then checks if element is enabled or not.
985
+ #
986
+ # Output:
987
+ # Returns true if element exists and is enabled, else returns false.
988
+ #
989
+ def enabled?
990
+ assert_exists
991
+ value = js_eval_method "disabled"
992
+ @@current_level = 0
993
+ return true if(value == "false")
994
+ return false if(value == "true")
995
+ return value
996
+ end
997
+
998
+ #
999
+ # Description:
1000
+ # Checks if element exists or not. If element is not located yet then first locates the element.
1001
+ #
1002
+ # Output:
1003
+ # True if element exists, false otherwise.
1004
+ #
1005
+ def exists?
1006
+ #puts "element is : #{element_object}"
1007
+ #puts caller(0)
1008
+ # If elements array has changed locate the element again. So that the element name points to correct element.
1009
+ if(element_object == nil || element_object == "")
1010
+ @@current_level = 0
1011
+ #puts "locating element"
1012
+ locate if defined?(locate)
1013
+ if(@element_name == nil || @element_name == "")
1014
+ return false
1015
+ else
1016
+ #puts caller(0)
1017
+ #puts "element name is : #{@element_name}"
1018
+ return true
1019
+ end
1020
+ else
1021
+ #puts "not locating the element again"
1022
+ return true
1023
+ end
1024
+ #@@current_level = 0
1025
+ #if(element_object == nil || element_object == "")
1026
+ # return false
1027
+ #else
1028
+ # return true
1029
+ #end
1030
+ end
1031
+ alias exist? exists?
1032
+
1033
+ #
1034
+ # Description:
1035
+ # Returns the text of the element.
1036
+ #
1037
+ # Output:
1038
+ # Text of the element.
1039
+ #
1040
+ def text()
1041
+ assert_exists
1042
+ element = (element_type == "HTMLFrameElement") ? BODY_VAR : element_object
1043
+ return_value = js_eval("#{element}.textContent.replace(/\\xA0/g, ' ').replace(/\\s+/g, ' ')").strip
1044
+ @@current_level = 0
1045
+ return return_value
1046
+ end
1047
+ alias innerText text
1048
+
1049
+ # Returns the name of the element (as defined in html)
1050
+ def_wrap :name
1051
+ # Returns the id of the element
1052
+ def_wrap :id
1053
+ # Returns whether the element is disabled
1054
+ def disabled
1055
+ ! enabled?
1056
+ end
1057
+ alias disabled? disabled
1058
+ # Returns the state of the element
1059
+ def_wrap :checked
1060
+ # Returns the value of the element
1061
+ def_wrap :value
1062
+ # Returns the title of the element
1063
+ def_wrap :title
1064
+ # Returns the value of 'alt' attribute in case of Image element.
1065
+ def_wrap :alt
1066
+ # Returns the value of 'href' attribute in case of Anchor element.
1067
+ def_wrap :src
1068
+ # Returns the type of the element. Use in case of Input element only.
1069
+ def_wrap :type
1070
+ # Returns the url the Anchor element points to.
1071
+ def_wrap :href
1072
+ # Return the ID of the control that this label is associated with
1073
+ def_wrap :for, :htmlFor
1074
+ # Returns the class name of the element
1075
+ def_wrap :class_name, :className
1076
+ # Return the html of the object
1077
+ def_wrap :html, :innerHTML
1078
+ # Return the action of form
1079
+ def_wrap :action
1080
+
1081
+ #
1082
+ # Description:
1083
+ # Display basic details about the object. Sample output for a button is shown.
1084
+ # Raises UnknownObjectException if the object is not found.
1085
+ # name b4
1086
+ # type button
1087
+ # id b5
1088
+ # value Disabled Button
1089
+ # disabled true
1090
+ #
1091
+ # Output:
1092
+ # Array with value of properties shown above.
1093
+ #
1094
+ def to_s(attributes=nil)
1095
+ #puts "here in to_s"
1096
+ #puts caller(0)
1097
+ assert_exists
1098
+ if(element_type == "HTMLTableCellElement")
1099
+ return text()
1100
+ else
1101
+ result = string_creator(attributes) #.join("\n")
1102
+ @@current_level = 0
1103
+ return result
1104
+ end
1105
+ end
1106
+
1107
+ #
1108
+ # Description:
1109
+ # Function to fire click event on elements.
1110
+ #
1111
+ def click
1112
+ assert_exists
1113
+ assert_enabled
1114
+
1115
+ highlight(:set)
1116
+ #puts "#{element_object} and #{element_type}"
1117
+ case element_type
1118
+
1119
+ when "HTMLAnchorElement", "HTMLImageElement"
1120
+ # Special check for link or anchor tag. Because click() doesn't work on links.
1121
+ # More info: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-48250443
1122
+ # https://bugzilla.mozilla.org/show_bug.cgi?id=148585
1123
+
1124
+ jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"MouseEvents\");"
1125
+
1126
+ # Info about initMouseEvent at: http://www.xulplanet.com/references/objref/MouseEvent.html
1127
+ jssh_command += "event.initMouseEvent('click',true,true,null,1,0,0,0,0,false,false,false,false,0,null);"
1128
+ jssh_command += "#{element_object}.dispatchEvent(event);\n"
1129
+
1130
+ #puts "jssh_command is: #{jssh_command}"
1131
+ jssh_socket.send("#{jssh_command}", 0)
1132
+ read_socket()
1133
+ else
1134
+ jssh_socket.send("typeof(#{element_object}.click);\n", 0)
1135
+ isDefined = read_socket()
1136
+ if(isDefined == "undefined")
1137
+ fire_event("onclick")
1138
+ else
1139
+ jssh_socket.send("#{element_object}.click();\n" , 0)
1140
+ read_socket()
1141
+ end
1142
+ end
1143
+ highlight(:clear)
1144
+ # Wait for firefox to reload.
1145
+ wait()
1146
+ end
1147
+
1148
+ #
1149
+ # Description:
1150
+ # Wait for the browser to get loaded, after the event is being fired.
1151
+ #
1152
+ def wait
1153
+ #ff = FireWatir::Firefox.new
1154
+ #ff.wait()
1155
+ #puts @container
1156
+ @container.wait()
1157
+ @@current_level = 0
1158
+ end
1159
+
1160
+ #
1161
+ # Description:
1162
+ # Function is used for click events that generates javascript pop up.
1163
+ # Doesn't fire the click event immediately instead, it stores the state of the object. User then tells which button
1164
+ # is to be clicked in case a javascript pop up comes after clicking the element. Depending upon the button to be clicked
1165
+ # the functions 'alert' and 'confirm' are re-defined in JavaScript to return appropriate values either true or false. Then the
1166
+ # re-defined functions are send to jssh which then fires the click event of the element using the state
1167
+ # stored above. So the click event is fired in the second statement. Therefore, if you are using this function you
1168
+ # need to call 'click_js_popup_button()' function in the next statement to actually trigger the click event.
1169
+ #
1170
+ # Typical Usage:
1171
+ # ff.button(:id, "button").click_no_wait()
1172
+ # ff.click_js_popup_button("OK")
1173
+ #
1174
+ #def click_no_wait
1175
+ # assert_exists
1176
+ # assert_enabled
1177
+ #
1178
+ # highlight(:set)
1179
+ # @@current_js_object = Element.new("#{element_object}", @container)
1180
+ #end
1181
+
1182
+ #
1183
+ # Description:
1184
+ # Function to click specified button on the javascript pop up. Currently you can only click
1185
+ # either OK or Cancel button.
1186
+ # Functions alert and confirm are redefined so that it doesn't causes the JSSH to get blocked. Also this
1187
+ # will make Firewatir cross platform.
1188
+ #
1189
+ # Input:
1190
+ # button to be clicked
1191
+ #
1192
+ #def click_js_popup(button = "OK")
1193
+ # jssh_command = "var win = #{BROWSER_VAR}.contentWindow;"
1194
+ # if(button =~ /ok/i)
1195
+ # jssh_command += "var popuptext = '';win.alert = function(param) {popuptext = param; return true; };
1196
+ # win.confirm = function(param) {popuptext = param; return true; };"
1197
+ # elsif(button =~ /cancel/i)
1198
+ # jssh_command += "var popuptext = '';win.alert = function(param) {popuptext = param; return false; };
1199
+ # win.confirm = function(param) {popuptext = param; return false; };"
1200
+ # end
1201
+ # jssh_command.gsub!(/\n/, "")
1202
+ # jssh_socket.send("#{jssh_command}\n", 0)
1203
+ # read_socket()
1204
+ # click_js_popup_creator_button()
1205
+ # #jssh_socket.send("popuptext_alert;\n", 0)
1206
+ # #read_socket()
1207
+ # jssh_socket.send("\n", 0)
1208
+ # read_socket()
1209
+ #end
1210
+
1211
+ #
1212
+ # Description:
1213
+ # Clicks on button or link or any element that triggers a javascript pop up.
1214
+ # Used internally by function click_js_popup.
1215
+ #
1216
+ #def click_js_popup_creator_button
1217
+ # #puts @@current_js_object.element_name
1218
+ # jssh_socket.send("#{@@current_js_object.element_name}\n;", 0)
1219
+ # temp = read_socket()
1220
+ # temp =~ /\[object\s(.*)\]/
1221
+ # if $1
1222
+ # type = $1
1223
+ # else
1224
+ # # This is done because in JSSh if you write element name of anchor type
1225
+ # # then it displays the link to which it navigates instead of displaying
1226
+ # # object type. So above regex match will return nil
1227
+ # type = "HTMLAnchorElement"
1228
+ # end
1229
+ # #puts type
1230
+ # case type
1231
+ # when "HTMLAnchorElement", "HTMLImageElement"
1232
+ # jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"MouseEvents\");"
1233
+ # # Info about initMouseEvent at: http://www.xulplanet.com/references/objref/MouseEvent.html
1234
+ # jssh_command += "event.initMouseEvent('click',true,true,null,1,0,0,0,0,false,false,false,false,0,null);"
1235
+ # jssh_command += "#{@@current_js_object.element_name}.dispatchEvent(event);\n"
1236
+ #
1237
+ # jssh_socket.send("#{jssh_command}", 0)
1238
+ # read_socket()
1239
+ # when "HTMLDivElement", "HTMLSpanElement"
1240
+ # jssh_socket.send("typeof(#{element_object}.#{event.downcase});\n", 0)
1241
+ # isDefined = read_socket()
1242
+ # #puts "is method there : #{isDefined}"
1243
+ # if(isDefined != "undefined")
1244
+ # if(element_type == "HTMLSelectElement")
1245
+ # jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"HTMLEvents\");
1246
+ # event.initEvent(\"click\", true, true);
1247
+ # #{element_object}.dispatchEvent(event);"
1248
+ # jssh_command.gsub!(/\n/, "")
1249
+ # jssh_socket.send("#{jssh_command}\n", 0)
1250
+ # read_socket()
1251
+ # else
1252
+ # jssh_socket.send("#{element_object}.#{event.downcase}();\n", 0)
1253
+ # read_socket()
1254
+ # end
1255
+ # end
1256
+ # else
1257
+ # jssh_command = "#{@@current_js_object.element_name}.click();\n";
1258
+ # jssh_socket.send("#{jssh_command}", 0)
1259
+ # read_socket()
1260
+ # end
1261
+ # @@current_level = 0
1262
+ # @@current_js_object = nil
1263
+ #end
1264
+ #private :click_js_popup_creator_button
1265
+
1266
+ #
1267
+ # Description:
1268
+ # Gets all the options of the select list element.
1269
+ #
1270
+ # Output:
1271
+ # Array of option elements.
1272
+ #
1273
+ def options
1274
+ jssh_socket.send("#{element_object}.options.length;\n", 0)
1275
+ length = read_socket().to_i
1276
+ #puts "options length is : #{length}"
1277
+ arr_options = Array.new(length)
1278
+ for i in 0..length - 1
1279
+ arr_options[i] = "#{element_object}.options[#{i}]"
1280
+ end
1281
+ return arr_options
1282
+ end
1283
+ private :options
1284
+
1285
+ #
1286
+ # Description:
1287
+ # Used to get class name for option element. Used internally by Firewatir use ff.select_list(...).option(..).class
1288
+ #
1289
+ # Output:
1290
+ # Class name of option element.
1291
+ #
1292
+ def option_class_name
1293
+ jssh_socket.send("#{element_object}.className;\n", 0)
1294
+ return read_socket()
1295
+ end
1296
+ private :option_class_name
1297
+
1298
+ #
1299
+ # Description:
1300
+ # Used to get text for option element. Used internally by Firewatir use ff.select_list(...).option(..).text
1301
+ #
1302
+ # Output:
1303
+ # Text of option element.
1304
+ #
1305
+ def option_text
1306
+ jssh_socket.send("#{element_object}.text;\n", 0)
1307
+ return read_socket()
1308
+ end
1309
+ private :option_text
1310
+
1311
+ #
1312
+ # Description:
1313
+ # Used to get value for option element. Used internally by Firewatir use ff.select_list(...).option(..).value
1314
+ #
1315
+ # Output:
1316
+ # Value of option element.
1317
+ #
1318
+ def option_value
1319
+ jssh_socket.send("#{element_object}.value;\n", 0)
1320
+ return read_socket()
1321
+ end
1322
+ private :option_value
1323
+
1324
+ #
1325
+ # Description:
1326
+ # Used to check if option is selected or not. Used internally by Firewatir use ff.select_list(...).option(..).selected
1327
+ #
1328
+ # Output:
1329
+ # True if option is selected, false otherwise.
1330
+ #
1331
+ def option_selected
1332
+ jssh_socket.send("#{element_object}.selected;\n", 0)
1333
+ value = read_socket()
1334
+ return true if value == "true"
1335
+ return false if value == "false"
1336
+ end
1337
+ private :option_selected
1338
+
1339
+ #
1340
+ # Description:
1341
+ # Gets all the cells of the row of a table.
1342
+ #
1343
+ # Output:
1344
+ # Array of table cell elements.
1345
+ #
1346
+ def get_cells
1347
+ assert_exists
1348
+ #puts "element name in cells is : #{element_object}"
1349
+ if(element_type == "HTMLTableRowElement")
1350
+ jssh_socket.send("#{element_object}.cells.length;\n", 0)
1351
+ length = read_socket.to_i
1352
+ return_array = Array.new(length)
1353
+ for i in 0..length - 1 do
1354
+ return_array[i] = "#{element_object}.cells[#{i}]"
1355
+ end
1356
+ return return_array
1357
+ else
1358
+ puts "The element must be of table row type to execute this function."
1359
+ return nil
1360
+ end
1361
+ end
1362
+ private :get_cells
1363
+
1364
+ #
1365
+ # Description:
1366
+ # Sets the value of file in HTMLInput file field control.
1367
+ #
1368
+ # Input:
1369
+ # setPath - location of the file to be uploaded. '|' should not be part of filename.
1370
+ #
1371
+ def setFileFieldValue(setPath)
1372
+ # As ruby converts \\ to \ and while sending name to jssh \ is ignored.
1373
+ # So replace \ with \\. Now this is even trickier you can't replace \ with \\
1374
+ # directly like this string.gsub!("\\", "\\\\") this doesn't work.
1375
+ # Work around is replace '\' with two special character and then replace
1376
+ # special character with \.
1377
+ setPath.gsub!("\\", "||")
1378
+ setPath.gsub!("|", "\\")
1379
+
1380
+ #jssh_command = "var textBox = #{DOCUMENT_VAR}.getBoxObjectFor(#{element_object}).firstChild;"
1381
+ #jssh_command += "textBox.value = \"#{setPath}\";\n";
1382
+
1383
+ #puts jssh_command
1384
+ jssh_socket.send("#{element_object}.value = \"#{setPath}\";\n", 0)
1385
+ read_socket()
1386
+ @@current_level = 0
1387
+ end
1388
+ private :setFileFieldValue
1389
+
1390
+ #
1391
+ # Description:
1392
+ # Traps all the function calls for an element that is not defined and fires them again
1393
+ # as it is to the jssh. This can be used in case the element supports properties or methods
1394
+ # that are not defined in the corresponding element class or in the base class(Element).
1395
+ #
1396
+ # Input:
1397
+ # methodId - Id of the method that is called.
1398
+ # *args - arguments sent to the methods.
1399
+ #
1400
+ def method_missing(methId, *args)
1401
+ methodName = methId.id2name
1402
+ #puts "method name is : #{methodName}"
1403
+ assert_exists
1404
+ #assert_enabled
1405
+ methodName = "colSpan" if methodName == "colspan"
1406
+ if(methodName =~ /invoke/)
1407
+ jssh_command = "#{element_object}."
1408
+ for i in args do
1409
+ jssh_command += i;
1410
+ end
1411
+ #puts "#{jssh_command}"
1412
+ jssh_socket.send("#{jssh_command};\n", 0)
1413
+ return_value = read_socket()
1414
+ #puts "return value is : #{return_value}"
1415
+ return return_value
1416
+ else
1417
+ #assert_exists
1418
+ #puts "element name is #{element_object}"
1419
+
1420
+ # We get method name with trailing '=' when we try to assign a value to a
1421
+ # property. So just remove the '=' to get the type
1422
+ temp = ""
1423
+ assigning_value = false
1424
+ if(methodName =~ /(.*)=$/)
1425
+ temp = "#{element_object}.#{$1}"
1426
+ assigning_value = true
1427
+ else
1428
+ temp = "#{element_object}.#{methodName}"
1429
+ end
1430
+ #puts "temp is : #{temp}"
1431
+
1432
+ jssh_socket.send("typeof(#{temp});\n", 0)
1433
+ method_type = read_socket()
1434
+ #puts "method_type is : #{method_type}"
1435
+
1436
+ if(assigning_value)
1437
+ if(method_type != "boolean" && args[0].class != Fixnum)
1438
+ args[0].gsub!("\\", "\\"*4)
1439
+ args[0].gsub!("\"", "\\\"")
1440
+ args[0].gsub!("\n","\\n")
1441
+ jssh_command = "#{element_object}.#{methodName}\"#{args[0]}\""
1442
+ else
1443
+ jssh_command = "#{element_object}.#{methodName}#{args[0]}"
1444
+ end
1445
+ #puts "jssh_command is : #{jssh_command}"
1446
+ jssh_socket.send("#{jssh_command};\n", 0)
1447
+ read_socket()
1448
+ return
1449
+ end
1450
+
1451
+ methodName = "#{element_object}.#{methodName}"
1452
+ if(args.length == 0)
1453
+ #puts "In if loop #{methodName}"
1454
+ if(method_type == "function")
1455
+ jssh_command = "#{methodName}();\n"
1456
+ else
1457
+ jssh_command = "#{methodName};\n"
1458
+ end
1459
+ else
1460
+ #puts "In else loop : #{methodName}"
1461
+ jssh_command = "#{methodName}("
1462
+
1463
+ count = 0
1464
+ if args != nil
1465
+ for i in args
1466
+ jssh_command += "," if count != 0
1467
+ if i.kind_of? Numeric
1468
+ jssh_command += i.to_s
1469
+ else
1470
+ jssh_command += "\"#{i.to_s.gsub(/"/,"\\\"")}\""
1471
+ end
1472
+ count = count + 1
1473
+ end
1474
+ end
1475
+
1476
+ jssh_command += ");\n"
1477
+ end
1478
+
1479
+ if(method_type == "boolean")
1480
+ jssh_command = jssh_command.gsub("\"false\"", "false")
1481
+ jssh_command = jssh_command.gsub("\"true\"", "true")
1482
+ end
1483
+ #puts "jssh_command is #{jssh_command}"
1484
+ jssh_socket.send("#{jssh_command}", 0)
1485
+ returnValue = read_socket()
1486
+ #puts "return value is : #{returnValue}"
1487
+
1488
+ @@current_level = 0
1489
+
1490
+ if(method_type == "boolean")
1491
+ return false if(returnValue == "false")
1492
+ return true if(returnValue == "true")
1493
+ elsif(method_type == "number")
1494
+ return returnValue.to_i
1495
+ else
1496
+ return returnValue
1497
+ end
1498
+ end
1499
+ end
1500
+ end
1501
+
1502
+ #
1503
+ # Description:
1504
+ # Class for returning the document element.
1505
+ #
1506
+ class Document
1507
+ include FireWatir::Container
1508
+ @@current_level = 0
1509
+
1510
+ #
1511
+ # Description:
1512
+ # Creates new instance of Document class.
1513
+ #
1514
+ def initialize(container)
1515
+ @length = 0
1516
+ @elements = nil
1517
+ @arr_elements = ""
1518
+ @container = container
1519
+ end
1520
+
1521
+ #
1522
+ # Description:
1523
+ # Find all the elements in the document by querying DOM.
1524
+ # Set the class variables like length and the variable name of array storing the elements in JSSH.
1525
+ #
1526
+ # Output:
1527
+ # Array of elements.
1528
+ #
1529
+ def all
1530
+ @arr_elements = "arr_coll_#{@@current_level}"
1531
+ jssh_command = "var arr_coll_#{@@current_level}=new Array(); "
1532
+
1533
+ if(@container.class == FireWatir::Firefox || @container.class == Frame)
1534
+ jssh_command +="var element_collection = null; element_collection = #{DOCUMENT_VAR}.getElementsByTagName(\"*\");
1535
+ if(element_collection != null && typeof(element_collection) != 'undefined')
1536
+ {
1537
+ for (var i = 0; i < element_collection.length; i++)
1538
+ {
1539
+ if((element_collection[i].tagName != 'BR') && (element_collection[i].tagName != 'HR') && (element_collection[i].tagName != 'DOCTYPE') && (element_collection[i].tagName != 'META') && (typeof(element_collection[i].tagName) != 'undefined'))
1540
+ arr_coll_#{@@current_level}.push(element_collection[i]);
1541
+ }
1542
+ }
1543
+ arr_coll_#{@@current_level}.length;"
1544
+ else
1545
+ jssh_command +="var element_collection = null; element_collection = #{@container.element_name}.getElementsByTagName(\"*\");
1546
+ if(element_collection!= null && typeof(element_collection) != 'undefined')
1547
+ {
1548
+ for (var i = 0; i < element_collection.length; i++)
1549
+ {
1550
+ if((element_collection[i].tagName != 'BR') && (element_collection[i].tagName != 'HR') && (element_collection[i].tagName != 'DOCTYPE') && (element_collection[i].tagName != 'META') && (typeof(element_collection[i].tagName) != 'undefined'))
1551
+ arr_coll_#{@@current_level}.push(element_collection[i]);
1552
+ }
1553
+ }
1554
+ arr_coll_#{@@current_level}.length;"
1555
+ end
1556
+
1557
+ # Remove \n that are there in the string as a result of pressing enter while formatting.
1558
+ jssh_command.gsub!(/\n/, "")
1559
+ #puts jssh_command
1560
+ jssh_socket.send("#{jssh_command};\n", 0)
1561
+ @length = read_socket().to_i;
1562
+ #puts "elements length is in locate_tagged_elements is : #{@length}"
1563
+
1564
+ elements = nil
1565
+ elements = Array.new(@length)
1566
+ for i in 0..@length - 1 do
1567
+ temp = Element.new("arr_coll_#{@@current_level}[#{i}]", @container)
1568
+ elements[i] = temp
1569
+ end
1570
+ @@current_level += 1
1571
+ return elements
1572
+
1573
+ end
1574
+
1575
+ #
1576
+ # Description:
1577
+ # Returns the count of elements in the document.
1578
+ #
1579
+ # Output:
1580
+ # Count of elements found in the document.
1581
+ #
1582
+ def length
1583
+ return @length
1584
+ end
1585
+
1586
+ #
1587
+ # Description:
1588
+ # Iterates over elements in the document.
1589
+ #
1590
+ def each
1591
+ for i in 0..@length - 1
1592
+ yield Element.new("#{@arr_elements}[#{i}]", @container)
1593
+ end
1594
+ end
1595
+
1596
+ #
1597
+ # Description:
1598
+ # Gets the element at the nth index in the array of the elements.
1599
+ #
1600
+ # Input:
1601
+ # n - Index of element you want to access. Index is 1 based.
1602
+ #
1603
+ # Output:
1604
+ # Element at the nth index.
1605
+ #
1606
+ def [](n)
1607
+ return Element.new("#{@arr_elements}[#{n-1}]", @container)
1608
+ end
1609
+
1610
+ #
1611
+ # Description:
1612
+ # Get all forms available on the page.
1613
+ # Used internally by Firewatir use ff.show_forms instead.
1614
+ #
1615
+ # Output:
1616
+ # Array containing Form elements
1617
+ #
1618
+ def get_forms()
1619
+ jssh_socket.send("var element_forms = #{DOCUMENT_VAR}.forms; element_forms.length;\n", 0)
1620
+ length = read_socket().to_i
1621
+ forms = Array.new(length)
1622
+
1623
+ for i in 0..length - 1 do
1624
+ forms[i] = Form.new(@container, :jssh_name, "element_forms[#{i}]")
1625
+ end
1626
+ return forms
1627
+ end
1628
+
1629
+ #
1630
+ # Description:
1631
+ # Get all images available on the page.
1632
+ # Used internally by Firewatir use ff.show_images instead.
1633
+ #
1634
+ # Output:
1635
+ # Array containing Image elements
1636
+ #
1637
+ def get_images
1638
+ return Images.new(@container)
1639
+ end
1640
+
1641
+ #
1642
+ # Description:
1643
+ # Get all links available on the page.
1644
+ # Used internally by Firewatir use ff.show_links instead.
1645
+ #
1646
+ # Output:
1647
+ # Array containing Link elements
1648
+ #
1649
+ def get_links
1650
+ return Links.new(@container)
1651
+ end
1652
+
1653
+ #
1654
+ # Description:
1655
+ # Get all divs available on the page.
1656
+ # Used internally by Firewatir use ff.show_divs instead.
1657
+ #
1658
+ # Output:
1659
+ # Array containing Div elements
1660
+ #
1661
+ def get_divs
1662
+ return Divs.new(@container)
1663
+ end
1664
+
1665
+ #
1666
+ # Description:
1667
+ # Get all tables available on the page.
1668
+ # Used internally by Firewatir use ff.show_tables instead.
1669
+ #
1670
+ # Output:
1671
+ # Array containing Table elements
1672
+ #
1673
+ def get_tables
1674
+ return Tables.new(@container)
1675
+ end
1676
+
1677
+ #
1678
+ # Description:
1679
+ # Get all pres available on the page.
1680
+ # Used internally by Firewatir use ff.show_pres instead.
1681
+ #
1682
+ # Output:
1683
+ # Array containing Pre elements
1684
+ #
1685
+ def get_pres
1686
+ return Pres.new(@container)
1687
+ end
1688
+
1689
+ #
1690
+ # Description:
1691
+ # Get all spans available on the page.
1692
+ # Used internally by Firewatir use ff.show_spans instead.
1693
+ #
1694
+ # Output:
1695
+ # Array containing Span elements
1696
+ #
1697
+ def get_spans
1698
+ return Spans.new(@container)
1699
+ end
1700
+
1701
+ #
1702
+ # Description:
1703
+ # Get all labels available on the page.
1704
+ # Used internally by Firewatir use ff.show_labels instead.
1705
+ #
1706
+ # Output:
1707
+ # Array containing Label elements
1708
+ #
1709
+ def get_labels
1710
+ return Labels.new(@container)
1711
+ end
1712
+ end
1713
+
1714
+ #
1715
+ # Description:
1716
+ # Class for iterating over elements of common type like links, images, divs etc.
1717
+ #
1718
+ class ElementCollections
1719
+ def self.inherited subclass
1720
+ class_name = subclass.to_s.demodulize
1721
+ method_name = class_name.underscore
1722
+ element_class_name = class_name.singularize
1723
+
1724
+ FireWatir::Container.module_eval "def #{method_name}
1725
+ locate if defined?(locate)
1726
+ return #{class_name}.new(self); end"
1727
+
1728
+ subclass.class_eval "def element_class; #{element_class_name}; end"
1729
+ end
1730
+
1731
+ include FireWatir::Container # XXX not sure if this is right
1732
+ @@current_level = 0
1733
+
1734
+ def initialize(container)
1735
+ @container = container
1736
+ elements = locate_elements
1737
+ length = elements.length
1738
+ #puts "length is : #{length}"
1739
+ @element_objects = Array.new(length)
1740
+ for i in 0..length - 1 do
1741
+ @element_objects[i] = element_class.new(container, :jssh_name, elements[i])
1742
+ end
1743
+ end
1744
+
1745
+ # default implementation. overridden by some subclasses.
1746
+ def locate_elements
1747
+ locate_tagged_elements(element_class::TAG)
1748
+ end
1749
+
1750
+
1751
+ # Return all the elements of give tag and type inside the container.
1752
+ #
1753
+ # Input:
1754
+ # tag - tag name of the elements
1755
+ # types - array of element type names. used in case where same
1756
+ # element tag has different types like input has type image, button etc.
1757
+ #
1758
+ def locate_tagged_elements(tag, types = [])
1759
+
1760
+ # generate array to hold results
1761
+ result_name = "arr_coll_#{tag}_#{@@current_level}"
1762
+ jssh_command = "var #{result_name}=new Array();"
1763
+
1764
+ # generate array of elements matching the tag
1765
+ case @container
1766
+ when FireWatir::Firefox, FireWatir::Frame
1767
+ elements_tag = "elements_#{tag}"
1768
+ container_name = DOCUMENT_VAR
1769
+ else
1770
+ elements_tag = "elements_#{@@current_level}_#{tag}"
1771
+ container_name = @container.element_name
1772
+ end
1773
+ if types.include?("textarea") || types.include?("button")
1774
+ search_tag = '*'
1775
+ else
1776
+ search_tag = tag
1777
+ end
1778
+ jssh_command += "var #{elements_tag} = null; "
1779
+ jssh_command += "#{elements_tag} = #{container_name}.getElementsByTagName(\"#{search_tag}\");"
1780
+
1781
+
1782
+ # generate array containing results
1783
+ if types.empty?
1784
+ jssh_command += "#{result_name} = #{elements_tag};"
1785
+ else
1786
+ # generate types array
1787
+ jssh_command += "var types = new Array("
1788
+ types.each_with_index do |type, count|
1789
+ jssh_command += "," unless count == 0
1790
+ jssh_command += "\"#{type}\""
1791
+ end
1792
+ jssh_command += ");"
1793
+
1794
+ # check the type of each element
1795
+ jssh_command += "for(var i=0; i<#{elements_tag}.length; i++)
1796
+ {
1797
+ var element = #{elements_tag}[i];
1798
+ var same_type = false;
1799
+
1800
+ for(var j=0; j<types.length; j++)
1801
+ {
1802
+ if(types[j] == element.type || types[j] == element.tagName)
1803
+ {
1804
+ same_type = true;
1805
+ break;
1806
+ }
1807
+ }
1808
+
1809
+ if(same_type)
1810
+ {
1811
+ #{result_name}.push(element);
1812
+ }
1813
+ };"
1814
+ end
1815
+ jssh_command += "#{result_name}.length;"
1816
+
1817
+ # Remove \n that are there in the string as a result of pressing enter while formatting.
1818
+ jssh_command.gsub!(/\n/, "")
1819
+ #puts jssh_command
1820
+ jssh_socket.send("#{jssh_command};\n", 0)
1821
+ length = read_socket().to_i;
1822
+ #puts "elements length is in locate_tagged_elements is : #{length}"
1823
+
1824
+ elements = (0...length).collect {|i| "#{result_name}[#{i}]"}
1825
+
1826
+ @@current_level = @@current_level + 1
1827
+ return elements
1828
+ end
1829
+ private :locate_tagged_elements
1830
+
1831
+ #
1832
+ # Description:
1833
+ # Gets the length of elements of same tag and type found on the page.
1834
+ #
1835
+ # Ouput:
1836
+ # Count of elements found on the page.
1837
+ #
1838
+ def length
1839
+ return @element_objects.length
1840
+ end
1841
+
1842
+ #
1843
+ # Description:
1844
+ # Iterate over the elements of same tag and type found on the page.
1845
+ #
1846
+ def each
1847
+ for i in 0..@element_objects.length - 1
1848
+ yield @element_objects[i]
1849
+ end
1850
+ end
1851
+
1852
+ #
1853
+ # Description:
1854
+ # Accesses nth element of same tag and type found on the page.
1855
+ #
1856
+ # Input:
1857
+ # n - index of element (1 based)
1858
+ #
1859
+ def [](n)
1860
+ return @element_objects[n-1]
1861
+ end
1862
+
1863
+ end