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