firewatir 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MozillaBaseElement.rb +1775 -1778
- data/container.rb +16 -5
- data/firewatir.rb +122 -57
- data/firewatir/winClicker.rb +122 -0
- data/firewatir/x11.rb +192 -0
- data/htmlelements.rb +2281 -2277
- data/unittests/html/textfields1.html +1 -1
- data/unittests/mozilla_all_tests.rb +2 -1
- data/unittests/setup.rb +1 -1
- metadata +43 -34
data/MozillaBaseElement.rb
CHANGED
@@ -1,1778 +1,1775 @@
|
|
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
|
-
|
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
|
-
|
118
|
-
rowsLength = read_socket()
|
119
|
-
if(rowsLength != 0 || rowsLength != "")
|
120
|
-
|
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
|
-
|
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
|
-
|
140
|
-
#puts jssh_command
|
141
|
-
return_value = read_socket()
|
142
|
-
end
|
143
|
-
if(attribute_name == "value")
|
144
|
-
|
145
|
-
tagName = read_socket().downcase
|
146
|
-
|
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
|
-
|
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
|
-
|
218
|
-
@original_color = read_socket()
|
219
|
-
|
220
|
-
else # BUG: assumes is :clear, but could actually be anything
|
221
|
-
begin
|
222
|
-
|
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
|
-
|
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 =
|
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
|
-
|
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
|
-
jssh_command = "var frameset = #{WINDOW_VAR}.frames;
|
509
|
-
var elements_frames = new Array();
|
510
|
-
for(var i = 0; i < frameset.length; i++)
|
511
|
-
{
|
512
|
-
var frames = frameset[i].frames;
|
513
|
-
for(var j = 0; j < frames.length; j++)
|
514
|
-
{
|
515
|
-
elements_frames.push(frames[j].frameElement);
|
516
|
-
}
|
517
|
-
}"
|
518
|
-
else
|
519
|
-
jssh_command = "var frames = #{@container.element_name}.contentWindow.frames;
|
520
|
-
var elements_frames_#{@@current_level} = new Array();
|
521
|
-
for(var i = 0; i < frames.length; i++)
|
522
|
-
{
|
523
|
-
elements_frames_#{@@current_level}.push(frames[i].frameElement);
|
524
|
-
}"
|
525
|
-
end
|
526
|
-
|
527
|
-
jssh_command +=" var element_name = ''; var object_index = 1;var attribute = '';
|
528
|
-
var element = '';"
|
529
|
-
if(@container.class == FireWatir::Firefox)
|
530
|
-
jssh_command += "for(var i = 0; i < elements_frames.length; i++)
|
531
|
-
{
|
532
|
-
element = elements_frames[i];"
|
533
|
-
else
|
534
|
-
jssh_command += "for(var i = 0; i < elements_frames_#{@@current_level}.length; i++)
|
535
|
-
{
|
536
|
-
element = elements_frames_#{@@current_level}[i];"
|
537
|
-
end
|
538
|
-
jssh_command += " if(\"index\" == \"#{how}\")
|
539
|
-
{
|
540
|
-
attribute = object_index; object_index += 1;
|
541
|
-
}
|
542
|
-
else
|
543
|
-
{
|
544
|
-
attribute = element.getAttribute(\"#{how}\");
|
545
|
-
if(attribute == \"\" || attribute == null)
|
546
|
-
{
|
547
|
-
attribute = element.#{how};
|
548
|
-
}
|
549
|
-
}
|
550
|
-
var found = false;"
|
551
|
-
if(what.class == Regexp)
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
jssh_command += "
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
#{
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
#
|
623
|
-
#
|
624
|
-
#
|
625
|
-
#
|
626
|
-
#
|
627
|
-
#
|
628
|
-
#
|
629
|
-
#
|
630
|
-
#
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
#
|
646
|
-
#
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
#
|
656
|
-
#
|
657
|
-
#
|
658
|
-
#
|
659
|
-
#
|
660
|
-
#
|
661
|
-
#
|
662
|
-
#
|
663
|
-
#
|
664
|
-
#
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
#
|
687
|
-
|
688
|
-
#puts
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
#
|
703
|
-
#
|
704
|
-
#
|
705
|
-
#
|
706
|
-
#
|
707
|
-
#
|
708
|
-
#
|
709
|
-
#
|
710
|
-
#
|
711
|
-
#
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
result
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
@@current_level
|
726
|
-
return
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
#
|
734
|
-
#
|
735
|
-
#
|
736
|
-
#
|
737
|
-
#
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
#
|
743
|
-
|
744
|
-
#
|
745
|
-
#
|
746
|
-
|
747
|
-
#
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
#
|
755
|
-
#
|
756
|
-
#
|
757
|
-
#
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
#
|
783
|
-
|
784
|
-
#
|
785
|
-
#
|
786
|
-
#
|
787
|
-
#
|
788
|
-
#
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
event =
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
#
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
dom_event_type = '
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
#
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
dom_event_init = "
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
#
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
#
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
#
|
874
|
-
#
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
#
|
885
|
-
#
|
886
|
-
#
|
887
|
-
#
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
value
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
#
|
902
|
-
#
|
903
|
-
#
|
904
|
-
#
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
#
|
934
|
-
#
|
935
|
-
#
|
936
|
-
#
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
#
|
951
|
-
|
952
|
-
#
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
# Returns
|
966
|
-
def_wrap :
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
#
|
989
|
-
|
990
|
-
|
991
|
-
#
|
992
|
-
#
|
993
|
-
#
|
994
|
-
#
|
995
|
-
#
|
996
|
-
#
|
997
|
-
#
|
998
|
-
#
|
999
|
-
#
|
1000
|
-
#
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
#
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
jssh_command
|
1035
|
-
|
1036
|
-
|
1037
|
-
jssh_command
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
read_socket()
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
#
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
#
|
1071
|
-
#
|
1072
|
-
#
|
1073
|
-
#
|
1074
|
-
#
|
1075
|
-
#
|
1076
|
-
#
|
1077
|
-
#
|
1078
|
-
#
|
1079
|
-
#
|
1080
|
-
#
|
1081
|
-
#
|
1082
|
-
#
|
1083
|
-
#
|
1084
|
-
#
|
1085
|
-
#
|
1086
|
-
#
|
1087
|
-
#
|
1088
|
-
|
1089
|
-
#
|
1090
|
-
#
|
1091
|
-
|
1092
|
-
#
|
1093
|
-
#
|
1094
|
-
#
|
1095
|
-
#
|
1096
|
-
#
|
1097
|
-
#
|
1098
|
-
#
|
1099
|
-
#
|
1100
|
-
#
|
1101
|
-
#
|
1102
|
-
#
|
1103
|
-
#
|
1104
|
-
#
|
1105
|
-
# jssh_command += "var popuptext = '';win.alert = function(param) {popuptext = param; return
|
1106
|
-
# win.confirm = function(param) {popuptext = param; return
|
1107
|
-
#
|
1108
|
-
#
|
1109
|
-
#
|
1110
|
-
#
|
1111
|
-
#
|
1112
|
-
#
|
1113
|
-
# read_socket()
|
1114
|
-
#
|
1115
|
-
#
|
1116
|
-
#
|
1117
|
-
|
1118
|
-
#
|
1119
|
-
#
|
1120
|
-
|
1121
|
-
#
|
1122
|
-
#
|
1123
|
-
#
|
1124
|
-
#
|
1125
|
-
#
|
1126
|
-
#
|
1127
|
-
#
|
1128
|
-
# $
|
1129
|
-
#
|
1130
|
-
#
|
1131
|
-
#
|
1132
|
-
#
|
1133
|
-
#
|
1134
|
-
#
|
1135
|
-
#
|
1136
|
-
#
|
1137
|
-
#
|
1138
|
-
#
|
1139
|
-
#
|
1140
|
-
#
|
1141
|
-
#
|
1142
|
-
# jssh_command
|
1143
|
-
#
|
1144
|
-
# jssh_command
|
1145
|
-
#
|
1146
|
-
#
|
1147
|
-
#
|
1148
|
-
#
|
1149
|
-
#
|
1150
|
-
#
|
1151
|
-
#
|
1152
|
-
#
|
1153
|
-
#
|
1154
|
-
#
|
1155
|
-
# jssh_command
|
1156
|
-
#
|
1157
|
-
#
|
1158
|
-
#
|
1159
|
-
#
|
1160
|
-
# read_socket()
|
1161
|
-
#
|
1162
|
-
#
|
1163
|
-
#
|
1164
|
-
#
|
1165
|
-
#
|
1166
|
-
#
|
1167
|
-
#
|
1168
|
-
#
|
1169
|
-
#
|
1170
|
-
#
|
1171
|
-
#
|
1172
|
-
|
1173
|
-
#
|
1174
|
-
#
|
1175
|
-
|
1176
|
-
#
|
1177
|
-
#
|
1178
|
-
#
|
1179
|
-
#
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
length
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
#
|
1196
|
-
#
|
1197
|
-
#
|
1198
|
-
#
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
#
|
1209
|
-
#
|
1210
|
-
#
|
1211
|
-
#
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
#
|
1222
|
-
#
|
1223
|
-
#
|
1224
|
-
#
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
#
|
1235
|
-
#
|
1236
|
-
#
|
1237
|
-
#
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
#
|
1250
|
-
#
|
1251
|
-
#
|
1252
|
-
#
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
return
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
#
|
1275
|
-
#
|
1276
|
-
#
|
1277
|
-
#
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
#
|
1283
|
-
#
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
#jssh_command
|
1291
|
-
#
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
#
|
1301
|
-
#
|
1302
|
-
#
|
1303
|
-
#
|
1304
|
-
#
|
1305
|
-
#
|
1306
|
-
#
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
#puts "#{
|
1322
|
-
|
1323
|
-
|
1324
|
-
#
|
1325
|
-
|
1326
|
-
|
1327
|
-
#
|
1328
|
-
#
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
temp
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
args[0]
|
1349
|
-
|
1350
|
-
args[0]
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
#puts "
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
return
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
#
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
#
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
@
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
#
|
1432
|
-
#
|
1433
|
-
#
|
1434
|
-
#
|
1435
|
-
#
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
@
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
#
|
1468
|
-
|
1469
|
-
#puts
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
#
|
1486
|
-
#
|
1487
|
-
#
|
1488
|
-
#
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
#
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
#
|
1507
|
-
#
|
1508
|
-
#
|
1509
|
-
#
|
1510
|
-
#
|
1511
|
-
#
|
1512
|
-
#
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
#
|
1521
|
-
#
|
1522
|
-
#
|
1523
|
-
#
|
1524
|
-
#
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
length
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
#
|
1540
|
-
#
|
1541
|
-
#
|
1542
|
-
#
|
1543
|
-
#
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
#
|
1552
|
-
#
|
1553
|
-
#
|
1554
|
-
#
|
1555
|
-
#
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
#
|
1564
|
-
#
|
1565
|
-
#
|
1566
|
-
#
|
1567
|
-
#
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
#
|
1576
|
-
#
|
1577
|
-
#
|
1578
|
-
#
|
1579
|
-
#
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
#
|
1588
|
-
#
|
1589
|
-
#
|
1590
|
-
#
|
1591
|
-
#
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
#
|
1600
|
-
#
|
1601
|
-
#
|
1602
|
-
#
|
1603
|
-
#
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
#
|
1612
|
-
#
|
1613
|
-
#
|
1614
|
-
#
|
1615
|
-
#
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
#
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
#
|
1632
|
-
#
|
1633
|
-
#
|
1634
|
-
#
|
1635
|
-
#
|
1636
|
-
#
|
1637
|
-
#
|
1638
|
-
#
|
1639
|
-
#
|
1640
|
-
#
|
1641
|
-
#
|
1642
|
-
#
|
1643
|
-
# @
|
1644
|
-
#
|
1645
|
-
|
1646
|
-
#
|
1647
|
-
#
|
1648
|
-
|
1649
|
-
#
|
1650
|
-
#
|
1651
|
-
#
|
1652
|
-
#
|
1653
|
-
#
|
1654
|
-
#
|
1655
|
-
#
|
1656
|
-
#
|
1657
|
-
|
1658
|
-
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
|
1665
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
jssh_command += "
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
1704
|
-
|
1705
|
-
|
1706
|
-
|
1707
|
-
|
1708
|
-
|
1709
|
-
|
1710
|
-
|
1711
|
-
|
1712
|
-
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1716
|
-
|
1717
|
-
|
1718
|
-
|
1719
|
-
|
1720
|
-
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
#
|
1730
|
-
|
1731
|
-
#puts
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
#
|
1746
|
-
#
|
1747
|
-
#
|
1748
|
-
#
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
#
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1767
|
-
#
|
1768
|
-
#
|
1769
|
-
#
|
1770
|
-
#
|
1771
|
-
|
1772
|
-
|
1773
|
-
|
1774
|
-
|
1775
|
-
|
1776
|
-
end
|
1777
|
-
|
1778
|
-
end
|
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
|
+
jssh_command = "var frameset = #{WINDOW_VAR}.frames;
|
509
|
+
var elements_frames = new Array();
|
510
|
+
for(var i = 0; i < frameset.length; i++)
|
511
|
+
{
|
512
|
+
var frames = frameset[i].frames;
|
513
|
+
for(var j = 0; j < frames.length; j++)
|
514
|
+
{
|
515
|
+
elements_frames.push(frames[j].frameElement);
|
516
|
+
}
|
517
|
+
}"
|
518
|
+
else
|
519
|
+
jssh_command = "var frames = #{@container.element_name}.contentWindow.frames;
|
520
|
+
var elements_frames_#{@@current_level} = new Array();
|
521
|
+
for(var i = 0; i < frames.length; i++)
|
522
|
+
{
|
523
|
+
elements_frames_#{@@current_level}.push(frames[i].frameElement);
|
524
|
+
}"
|
525
|
+
end
|
526
|
+
|
527
|
+
jssh_command +=" var element_name = ''; var object_index = 1;var attribute = '';
|
528
|
+
var element = '';"
|
529
|
+
if(@container.class == FireWatir::Firefox)
|
530
|
+
jssh_command += "for(var i = 0; i < elements_frames.length; i++)
|
531
|
+
{
|
532
|
+
element = elements_frames[i];"
|
533
|
+
else
|
534
|
+
jssh_command += "for(var i = 0; i < elements_frames_#{@@current_level}.length; i++)
|
535
|
+
{
|
536
|
+
element = elements_frames_#{@@current_level}[i];"
|
537
|
+
end
|
538
|
+
jssh_command += " if(\"index\" == \"#{how}\")
|
539
|
+
{
|
540
|
+
attribute = object_index; object_index += 1;
|
541
|
+
}
|
542
|
+
else
|
543
|
+
{
|
544
|
+
attribute = element.getAttribute(\"#{how}\");
|
545
|
+
if(attribute == \"\" || attribute == null)
|
546
|
+
{
|
547
|
+
attribute = element.#{how};
|
548
|
+
}
|
549
|
+
}
|
550
|
+
var found = false;"
|
551
|
+
if(what.class == Regexp)
|
552
|
+
oldRegExp = what.to_s
|
553
|
+
newRegExp = "/" + what.source + "/"
|
554
|
+
flags = oldRegExp.slice(2, oldRegExp.index(':') - 2)
|
555
|
+
|
556
|
+
for i in 0..flags.length do
|
557
|
+
flag = flags[i, 1]
|
558
|
+
if(flag == '-')
|
559
|
+
break;
|
560
|
+
else
|
561
|
+
newRegExp << flag
|
562
|
+
end
|
563
|
+
end
|
564
|
+
#puts "old reg ex is #{what} new reg ex is #{newRegExp}"
|
565
|
+
jssh_command += " var regExp = new RegExp(#{newRegExp});
|
566
|
+
found = regExp.test(attribute);"
|
567
|
+
elsif(how == :index)
|
568
|
+
jssh_command += " found = (attribute == #{what});"
|
569
|
+
else
|
570
|
+
jssh_command += " found = (attribute == \"#{what}\");"
|
571
|
+
end
|
572
|
+
|
573
|
+
jssh_command += " if(found)
|
574
|
+
{"
|
575
|
+
if(@container.class == FireWatir::Firefox)
|
576
|
+
jssh_command += " element_name = \"elements_frames[\" + i + \"]\";
|
577
|
+
#{DOCUMENT_VAR} = elements_frames[i].contentDocument;
|
578
|
+
#{BODY_VAR} = #{DOCUMENT_VAR}.body;"
|
579
|
+
else
|
580
|
+
jssh_command += " element_name = \"elements_frames_#{@@current_level}[\" + i + \"]\";
|
581
|
+
#{DOCUMENT_VAR} = elements_frames_#{@@current_level}[i].contentDocument;
|
582
|
+
#{BODY_VAR} = #{DOCUMENT_VAR}.body;"
|
583
|
+
end
|
584
|
+
jssh_command += " break;
|
585
|
+
}
|
586
|
+
}
|
587
|
+
element_name;"
|
588
|
+
|
589
|
+
jssh_command.gsub!("\n", "")
|
590
|
+
#puts "jssh_command for finding frame is : #{jssh_command}"
|
591
|
+
|
592
|
+
jssh_socket.send("#{jssh_command};\n", 0)
|
593
|
+
element_name = read_socket()
|
594
|
+
@@current_level = @@current_level + 1
|
595
|
+
#puts "element_name for frame is : #{element_name}"
|
596
|
+
|
597
|
+
if(element_name != "")
|
598
|
+
return element_name
|
599
|
+
else
|
600
|
+
return nil
|
601
|
+
end
|
602
|
+
end
|
603
|
+
|
604
|
+
def get_frame_html
|
605
|
+
jssh_socket.send("var htmlelem = #{DOCUMENT_VAR}.getElementsByTagName('html')[0]; htmlelem.innerHTML;\n", 0)
|
606
|
+
#jssh_socket.send("#{BODY_VAR}.innerHTML;\n", 0)
|
607
|
+
result = read_socket()
|
608
|
+
return "<html>" + result + "</html>"
|
609
|
+
end
|
610
|
+
|
611
|
+
def submit_form
|
612
|
+
#puts "form name is : #{element_object}"
|
613
|
+
jssh_socket.send("#{element_object}.submit();\n" , 0)
|
614
|
+
read_socket()
|
615
|
+
end
|
616
|
+
|
617
|
+
public
|
618
|
+
|
619
|
+
#
|
620
|
+
#
|
621
|
+
# Description:
|
622
|
+
# Matches the given text with the current text shown in the browser for that particular element.
|
623
|
+
#
|
624
|
+
# Input:
|
625
|
+
# target - Text to match. Can be a string or regex
|
626
|
+
#
|
627
|
+
# Output:
|
628
|
+
# Returns the index if the specified text was found.
|
629
|
+
# Returns matchdata object if the specified regexp was found.
|
630
|
+
#
|
631
|
+
def contains_text(target)
|
632
|
+
#puts "Text to match is : #{match_text}"
|
633
|
+
#puts "Html is : #{self.text}"
|
634
|
+
if target.kind_of? Regexp
|
635
|
+
self.text.match(target)
|
636
|
+
elsif target.kind_of? String
|
637
|
+
self.text.index(target)
|
638
|
+
else
|
639
|
+
raise ArgumentError, "Argument #{target} should be a string or regexp."
|
640
|
+
end
|
641
|
+
end
|
642
|
+
#
|
643
|
+
# Description:
|
644
|
+
# Method for inspecting the object. Defined here because IRB was not able to locate the object.
|
645
|
+
# TODO: Need to find out why IRB is unable to find object though both (this and IRB) are executing same statements
|
646
|
+
#
|
647
|
+
def inspect
|
648
|
+
assert_exists
|
649
|
+
puts self.to_s
|
650
|
+
end
|
651
|
+
|
652
|
+
#
|
653
|
+
# Description:
|
654
|
+
# Returns array of elements that matches a given XPath query.
|
655
|
+
# Mozilla browser directly supports XPath query on its DOM. So no need to create the DOM tree as WATiR does for IE.
|
656
|
+
# Refer: http://developer.mozilla.org/en/docs/DOM:document.evaluate
|
657
|
+
# Used internally by Firewatir use ff.elements_by_xpath instead.
|
658
|
+
#
|
659
|
+
# Input:
|
660
|
+
# xpath - The xpath expression or query.
|
661
|
+
#
|
662
|
+
# Output:
|
663
|
+
# Array of elements that matched the xpath expression provided as parameter.
|
664
|
+
#
|
665
|
+
def elements_by_xpath(container, xpath)
|
666
|
+
rand_no = rand(1000)
|
667
|
+
#jssh_command = "var xpathResult = #{DOCUMENT_VAR}.evaluate(\"count(#{xpath})\", #{DOCUMENT_VAR}, null, #{NUMBER_TYPE}, null); xpathResult.numberValue;"
|
668
|
+
#jssh_socket.send("#{jssh_command}\n", 0);
|
669
|
+
#node_count = read_socket()
|
670
|
+
|
671
|
+
jssh_command = "var element_xpath_#{rand_no} = new Array();"
|
672
|
+
|
673
|
+
jssh_command += "var result = #{DOCUMENT_VAR}.evaluate(\"#{xpath}\", #{DOCUMENT_VAR}, null, #{ORDERED_NODE_ITERATOR_TYPE}, null);
|
674
|
+
var iterate = result.iterateNext();
|
675
|
+
while(iterate)
|
676
|
+
{
|
677
|
+
element_xpath_#{rand_no}.push(iterate);
|
678
|
+
iterate = result.iterateNext();
|
679
|
+
}
|
680
|
+
element_xpath_#{rand_no}.length;
|
681
|
+
"
|
682
|
+
|
683
|
+
# Remove \n that are there in the string as a result of pressing enter while formatting.
|
684
|
+
jssh_command.gsub!(/\n/, "")
|
685
|
+
#puts jssh_command
|
686
|
+
jssh_socket.send("#{jssh_command};\n", 0)
|
687
|
+
node_count = read_socket()
|
688
|
+
#puts "value of count is : #{node_count}"
|
689
|
+
|
690
|
+
elements = Array.new(node_count.to_i)
|
691
|
+
|
692
|
+
for i in 0..elements.length - 1 do
|
693
|
+
elements[i] = "element_xpath_#{rand_no}[#{i}]"
|
694
|
+
end
|
695
|
+
|
696
|
+
return elements;
|
697
|
+
end
|
698
|
+
|
699
|
+
#
|
700
|
+
# Description:
|
701
|
+
# Returns first element found while traversing the DOM; that matches an given XPath query.
|
702
|
+
# Mozilla browser directly supports XPath query on its DOM. So no need to create the DOM tree as WATiR does for IE.
|
703
|
+
# Refer: http://developer.mozilla.org/en/docs/DOM:document.evaluate
|
704
|
+
# Used internally by Firewatir use ff.element_by_xpath instead.
|
705
|
+
#
|
706
|
+
# Input:
|
707
|
+
# xpath - The xpath expression or query.
|
708
|
+
#
|
709
|
+
# Output:
|
710
|
+
# First element in DOM that matched the XPath expression or query.
|
711
|
+
#
|
712
|
+
def element_by_xpath(container, xpath)
|
713
|
+
#puts "here locating element by xpath"
|
714
|
+
rand_no = rand(1000)
|
715
|
+
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};"
|
716
|
+
|
717
|
+
jssh_socket.send("#{jssh_command}\n", 0)
|
718
|
+
result = read_socket()
|
719
|
+
#puts "command send to jssh is : #{jssh_command}"
|
720
|
+
#puts "result is : #{result}"
|
721
|
+
if(result == "null" || result == "" || result.include?("exception"))
|
722
|
+
@@current_level = 0
|
723
|
+
return nil
|
724
|
+
else
|
725
|
+
@@current_level += 1
|
726
|
+
return "element_xpath_#{rand_no}"
|
727
|
+
end
|
728
|
+
end
|
729
|
+
|
730
|
+
#
|
731
|
+
# Description:
|
732
|
+
# Returns the name of the element with which we can access it in JSSh.
|
733
|
+
# Used internally by Firewatir to execute methods, set properties or return property value for the element.
|
734
|
+
#
|
735
|
+
# Output:
|
736
|
+
# Name of the variable with which element is referenced in JSSh
|
737
|
+
#
|
738
|
+
def element_object
|
739
|
+
#puts caller.join("\n")
|
740
|
+
#puts "In element_object element name is : #{@element_name}"
|
741
|
+
#puts "in element_object : #{@container.class}"
|
742
|
+
#if(@container.class == FireWatir::Firefox)
|
743
|
+
return @element_name #if @element_name != nil
|
744
|
+
#else
|
745
|
+
# return @container.element_name
|
746
|
+
#end
|
747
|
+
#return @o.element_name if @o != nil
|
748
|
+
end
|
749
|
+
private :element_object
|
750
|
+
|
751
|
+
#
|
752
|
+
# Description:
|
753
|
+
# Returns the type of element. For e.g.: HTMLAnchorElement. used internally by Firewatir
|
754
|
+
#
|
755
|
+
# Output:
|
756
|
+
# Type of the element.
|
757
|
+
#
|
758
|
+
def element_type
|
759
|
+
#puts "in element_type object is : #{element_object}"
|
760
|
+
# Get the type of the element.
|
761
|
+
jssh_socket.send("#{element_object};\n", 0)
|
762
|
+
temp = read_socket()
|
763
|
+
|
764
|
+
if temp == ""
|
765
|
+
return nil
|
766
|
+
end
|
767
|
+
|
768
|
+
#puts "#{element_object} and type is #{temp}"
|
769
|
+
temp =~ /\[object\s(.*)\]/
|
770
|
+
if $1
|
771
|
+
return $1
|
772
|
+
else
|
773
|
+
# This is done because in JSSh if you write element name of anchor type
|
774
|
+
# then it displays the link to which it navigates instead of displaying
|
775
|
+
# object type. So above regex match will return nil
|
776
|
+
return "HTMLAnchorElement"
|
777
|
+
end
|
778
|
+
end
|
779
|
+
#private :element_type
|
780
|
+
|
781
|
+
#
|
782
|
+
# Description:
|
783
|
+
# Fires the provided event for an element and by default waits for the action to get completed.
|
784
|
+
#
|
785
|
+
# Input:
|
786
|
+
# event - Event to be fired like "onclick", "onchange" etc.
|
787
|
+
# wait - Whether to wait for the action to get completed or not. By default its true.
|
788
|
+
#
|
789
|
+
# TODO: Provide ability to specify event parameters like keycode for key events, and click screen
|
790
|
+
# coordinates for mouse events.
|
791
|
+
def fire_event(event, wait = true)
|
792
|
+
assert_exists()
|
793
|
+
event = event.to_s # in case event was given as a symbol
|
794
|
+
|
795
|
+
event = event.downcase
|
796
|
+
|
797
|
+
event =~ /on(.*)/i
|
798
|
+
event = $1 if $1
|
799
|
+
|
800
|
+
# check if we've got an old-school on-event
|
801
|
+
#jssh_socket.send("typeof(#{element_object}.#{event});\n", 0)
|
802
|
+
#is_defined = read_socket()
|
803
|
+
|
804
|
+
# info about event types harvested from:
|
805
|
+
# http://www.howtocreate.co.uk/tutorials/javascript/domevents
|
806
|
+
case event
|
807
|
+
when 'abort', 'blur', 'change', 'error', 'focus', 'load', 'reset', 'resize',
|
808
|
+
'scroll', 'select', 'submit', 'unload'
|
809
|
+
dom_event_type = 'HTMLEvents'
|
810
|
+
dom_event_init = "initEvent(\"#{event}\", true, true)"
|
811
|
+
when 'keydown', 'keypress', 'keyup'
|
812
|
+
dom_event_type = 'KeyEvents'
|
813
|
+
# Firefox has a proprietary initializer for keydown/keypress/keyup.
|
814
|
+
# Args are as follows:
|
815
|
+
# 'type', bubbles, cancelable, windowObject, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode
|
816
|
+
dom_event_init = "initKeyEvent(\"#{event}\", true, true, #{WINDOW_VAR}, false, false, false, false, 0, 0)"
|
817
|
+
when 'click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover',
|
818
|
+
'mouseup'
|
819
|
+
dom_event_type = 'MouseEvents'
|
820
|
+
# Args are as follows:
|
821
|
+
# 'type', bubbles, cancelable, windowObject, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget
|
822
|
+
dom_event_init = "initMouseEvent(\"#{event}\", true, true, #{WINDOW_VAR}, 1, 0, 0, 0, 0, false, false, false, false, 0, null)"
|
823
|
+
else
|
824
|
+
dom_event_type = 'HTMLEvents'
|
825
|
+
dom_event_init = "initEvents(\"#{event}\", true, true)"
|
826
|
+
end
|
827
|
+
|
828
|
+
if(element_type == "HTMLSelectElement")
|
829
|
+
dom_event_type = 'HTMLEvents'
|
830
|
+
dom_event_init = "initEvent(\"#{event}\", true, true)"
|
831
|
+
end
|
832
|
+
|
833
|
+
|
834
|
+
jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"#{dom_event_type}\"); "
|
835
|
+
jssh_command << "event.#{dom_event_init}; "
|
836
|
+
jssh_command << "#{element_object}.dispatchEvent(event);"
|
837
|
+
|
838
|
+
#puts "JSSH COMMAND:\n#{jssh_command}\n"
|
839
|
+
|
840
|
+
jssh_socket.send("#{jssh_command}\n", 0)
|
841
|
+
read_socket() if wait
|
842
|
+
wait() if wait
|
843
|
+
|
844
|
+
@@current_level = 0
|
845
|
+
end
|
846
|
+
alias fireEvent fire_event
|
847
|
+
|
848
|
+
#
|
849
|
+
# Description:
|
850
|
+
# Returns the value of the specified attribute of an element.
|
851
|
+
#
|
852
|
+
def attribute_value(attribute_name)
|
853
|
+
#puts attribute_name
|
854
|
+
assert_exists()
|
855
|
+
return_value = get_attribute_value(attribute_name)
|
856
|
+
@@current_level = 0
|
857
|
+
return return_value
|
858
|
+
end
|
859
|
+
|
860
|
+
#
|
861
|
+
# Description:
|
862
|
+
# Checks if element exists or not. Raises UnknownObjectException if element doesn't exists.
|
863
|
+
#
|
864
|
+
def assert_exists
|
865
|
+
unless exists?
|
866
|
+
raise UnknownObjectException.new("Unable to locate object, using #{@how} and #{@what}")
|
867
|
+
end
|
868
|
+
end
|
869
|
+
|
870
|
+
#
|
871
|
+
# Description:
|
872
|
+
# Checks if element is enabled or not. Raises ObjectDisabledException if object is disabled and
|
873
|
+
# you are trying to use the object.
|
874
|
+
#
|
875
|
+
def assert_enabled
|
876
|
+
unless enabled?
|
877
|
+
raise ObjectDisabledException, "object #{@how} and #{@what} is disabled"
|
878
|
+
end
|
879
|
+
end
|
880
|
+
|
881
|
+
#
|
882
|
+
# Description:
|
883
|
+
# First checks if element exists or not. Then checks if element is enabled or not.
|
884
|
+
#
|
885
|
+
# Output:
|
886
|
+
# Returns true if element exists and is enabled, else returns false.
|
887
|
+
#
|
888
|
+
def enabled?
|
889
|
+
assert_exists
|
890
|
+
jssh_socket.send("#{element_object}.disabled;\n", 0)
|
891
|
+
value = read_socket()
|
892
|
+
@@current_level = 0
|
893
|
+
return true if(value == "false")
|
894
|
+
return false if(value == "true")
|
895
|
+
return value
|
896
|
+
end
|
897
|
+
|
898
|
+
#
|
899
|
+
# Description:
|
900
|
+
# Checks if element exists or not. If element is not located yet then first locates the element.
|
901
|
+
#
|
902
|
+
# Output:
|
903
|
+
# True if element exists, false otherwise.
|
904
|
+
#
|
905
|
+
def exists?
|
906
|
+
#puts "element is : #{element_object}"
|
907
|
+
# If elements array has changed locate the element again. So that the element name points to correct element.
|
908
|
+
if(element_object == nil || element_object == "")
|
909
|
+
#puts "locating element"
|
910
|
+
locate if defined?(locate)
|
911
|
+
if(@element_name == nil || @element_name == "")
|
912
|
+
return false
|
913
|
+
else
|
914
|
+
#puts caller(0)
|
915
|
+
#puts "element name is : #{@element_name}"
|
916
|
+
return true
|
917
|
+
end
|
918
|
+
else
|
919
|
+
#puts "not locating the element again"
|
920
|
+
return true
|
921
|
+
end
|
922
|
+
@@current_level = 0
|
923
|
+
if(element_object == nil || element_object == "")
|
924
|
+
return false
|
925
|
+
else
|
926
|
+
return true
|
927
|
+
end
|
928
|
+
end
|
929
|
+
|
930
|
+
#
|
931
|
+
# Description:
|
932
|
+
# Returns the text of the element.
|
933
|
+
#
|
934
|
+
# Output:
|
935
|
+
# Text of the element.
|
936
|
+
#
|
937
|
+
def text()
|
938
|
+
assert_exists
|
939
|
+
|
940
|
+
if(element_type == "HTMLFrameElement")
|
941
|
+
jssh_socket.send("#{BODY_VAR}.textContent;\n", 0)
|
942
|
+
else
|
943
|
+
jssh_socket.send("#{element_object}.textContent;\n", 0)
|
944
|
+
end
|
945
|
+
|
946
|
+
return_value = read_socket().strip()
|
947
|
+
#puts "text content of element is : #{return_value}"
|
948
|
+
|
949
|
+
#if(returnType == "boolean")
|
950
|
+
# return_value = false if returnValue == "false"
|
951
|
+
# return_value = true if returnValue == "true"
|
952
|
+
#end
|
953
|
+
@@current_level = 0
|
954
|
+
return return_value
|
955
|
+
end
|
956
|
+
alias innerText text
|
957
|
+
|
958
|
+
# Returns the name of the element (as defined in html)
|
959
|
+
def_wrap :name
|
960
|
+
# Returns the id of the element
|
961
|
+
def_wrap :id
|
962
|
+
# Returns whether the element is disabled
|
963
|
+
def_wrap :disabled
|
964
|
+
alias disabled? disabled
|
965
|
+
# Returns the state of the element
|
966
|
+
def_wrap :checked
|
967
|
+
# Returns the value of the element
|
968
|
+
def_wrap :value
|
969
|
+
# Returns the title of the element
|
970
|
+
def_wrap :title
|
971
|
+
# Returns the value of 'alt' attribute in case of Image element.
|
972
|
+
def_wrap :alt
|
973
|
+
# Returns the value of 'href' attribute in case of Anchor element.
|
974
|
+
def_wrap :src
|
975
|
+
# Returns the type of the element. Use in case of Input element only.
|
976
|
+
def_wrap :type
|
977
|
+
# Returns the url the Anchor element points to.
|
978
|
+
def_wrap :href
|
979
|
+
# Return the ID of the control that this label is associated with
|
980
|
+
def_wrap :for, :htmlFor
|
981
|
+
# Returns the class name of the element
|
982
|
+
def_wrap :class_name, :className
|
983
|
+
# Return the html of the object
|
984
|
+
def_wrap :html, :innerHTML
|
985
|
+
# Return the action of form
|
986
|
+
def_wrap :action
|
987
|
+
|
988
|
+
#
|
989
|
+
# Description:
|
990
|
+
# Display basic details about the object. Sample output for a button is shown.
|
991
|
+
# Raises UnknownObjectException if the object is not found.
|
992
|
+
# name b4
|
993
|
+
# type button
|
994
|
+
# id b5
|
995
|
+
# value Disabled Button
|
996
|
+
# disabled true
|
997
|
+
#
|
998
|
+
# Output:
|
999
|
+
# Array with value of properties shown above.
|
1000
|
+
#
|
1001
|
+
def to_s(attributes=nil)
|
1002
|
+
#puts "here in to_s"
|
1003
|
+
#puts caller(0)
|
1004
|
+
assert_exists
|
1005
|
+
if(element_type == "HTMLTableCellElement")
|
1006
|
+
return text()
|
1007
|
+
else
|
1008
|
+
result = string_creator(attributes) #.join("\n")
|
1009
|
+
@@current_level = 0
|
1010
|
+
return result
|
1011
|
+
end
|
1012
|
+
end
|
1013
|
+
|
1014
|
+
#
|
1015
|
+
# Description:
|
1016
|
+
# Function to fire click event on elements.
|
1017
|
+
#
|
1018
|
+
def click
|
1019
|
+
assert_exists
|
1020
|
+
assert_enabled
|
1021
|
+
|
1022
|
+
highlight(:set)
|
1023
|
+
#puts "#{element_object} and #{element_type}"
|
1024
|
+
case element_type
|
1025
|
+
|
1026
|
+
when "HTMLAnchorElement", "HTMLImageElement"
|
1027
|
+
# Special check for link or anchor tag. Because click() doesn't work on links.
|
1028
|
+
# More info: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-48250443
|
1029
|
+
# https://bugzilla.mozilla.org/show_bug.cgi?id=148585
|
1030
|
+
|
1031
|
+
jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"MouseEvents\");"
|
1032
|
+
|
1033
|
+
# Info about initMouseEvent at: http://www.xulplanet.com/references/objref/MouseEvent.html
|
1034
|
+
jssh_command += "event.initMouseEvent('click',true,true,null,1,0,0,0,0,false,false,false,false,0,null);"
|
1035
|
+
jssh_command += "#{element_object}.dispatchEvent(event);\n"
|
1036
|
+
|
1037
|
+
#puts "jssh_command is: #{jssh_command}"
|
1038
|
+
jssh_socket.send("#{jssh_command}", 0)
|
1039
|
+
read_socket()
|
1040
|
+
else
|
1041
|
+
jssh_socket.send("typeof(#{element_object}.click);\n", 0)
|
1042
|
+
isDefined = read_socket()
|
1043
|
+
if(isDefined == "undefined")
|
1044
|
+
fire_event("onclick")
|
1045
|
+
else
|
1046
|
+
jssh_socket.send("#{element_object}.click();\n" , 0)
|
1047
|
+
read_socket()
|
1048
|
+
end
|
1049
|
+
end
|
1050
|
+
highlight(:clear)
|
1051
|
+
# Wait for firefox to reload.
|
1052
|
+
wait()
|
1053
|
+
end
|
1054
|
+
|
1055
|
+
#
|
1056
|
+
# Description:
|
1057
|
+
# Wait for the browser to get loaded, after the event is being fired.
|
1058
|
+
#
|
1059
|
+
def wait
|
1060
|
+
#ff = FireWatir::Firefox.new
|
1061
|
+
#ff.wait()
|
1062
|
+
#puts @container
|
1063
|
+
@container.wait()
|
1064
|
+
@@current_level = 0
|
1065
|
+
end
|
1066
|
+
|
1067
|
+
#
|
1068
|
+
# Description:
|
1069
|
+
# Function is used for click events that generates javascript pop up.
|
1070
|
+
# Doesn't fire the click event immediately instead, it stores the state of the object. User then tells which button
|
1071
|
+
# is to be clicked in case a javascript pop up comes after clicking the element. Depending upon the button to be clicked
|
1072
|
+
# the functions 'alert' and 'confirm' are re-defined in JavaScript to return appropriate values either true or false. Then the
|
1073
|
+
# re-defined functions are send to jssh which then fires the click event of the element using the state
|
1074
|
+
# stored above. So the click event is fired in the second statement. Therefore, if you are using this function you
|
1075
|
+
# need to call 'click_js_popup_button()' function in the next statement to actually trigger the click event.
|
1076
|
+
#
|
1077
|
+
# Typical Usage:
|
1078
|
+
# ff.button(:id, "button").click_no_wait()
|
1079
|
+
# ff.click_js_popup_button("OK")
|
1080
|
+
#
|
1081
|
+
#def click_no_wait
|
1082
|
+
# assert_exists
|
1083
|
+
# assert_enabled
|
1084
|
+
#
|
1085
|
+
# highlight(:set)
|
1086
|
+
# @@current_js_object = Element.new("#{element_object}", @container)
|
1087
|
+
#end
|
1088
|
+
|
1089
|
+
#
|
1090
|
+
# Description:
|
1091
|
+
# Function to click specified button on the javascript pop up. Currently you can only click
|
1092
|
+
# either OK or Cancel button.
|
1093
|
+
# Functions alert and confirm are redefined so that it doesn't causes the JSSH to get blocked. Also this
|
1094
|
+
# will make Firewatir cross platform.
|
1095
|
+
#
|
1096
|
+
# Input:
|
1097
|
+
# button to be clicked
|
1098
|
+
#
|
1099
|
+
#def click_js_popup(button = "OK")
|
1100
|
+
# jssh_command = "var win = #{BROWSER_VAR}.contentWindow;"
|
1101
|
+
# if(button =~ /ok/i)
|
1102
|
+
# jssh_command += "var popuptext = '';win.alert = function(param) {popuptext = param; return true; };
|
1103
|
+
# win.confirm = function(param) {popuptext = param; return true; };"
|
1104
|
+
# elsif(button =~ /cancel/i)
|
1105
|
+
# jssh_command += "var popuptext = '';win.alert = function(param) {popuptext = param; return false; };
|
1106
|
+
# win.confirm = function(param) {popuptext = param; return false; };"
|
1107
|
+
# end
|
1108
|
+
# jssh_command.gsub!(/\n/, "")
|
1109
|
+
# jssh_socket.send("#{jssh_command}\n", 0)
|
1110
|
+
# read_socket()
|
1111
|
+
# click_js_popup_creator_button()
|
1112
|
+
# #jssh_socket.send("popuptext_alert;\n", 0)
|
1113
|
+
# #read_socket()
|
1114
|
+
# jssh_socket.send("\n", 0)
|
1115
|
+
# read_socket()
|
1116
|
+
#end
|
1117
|
+
|
1118
|
+
#
|
1119
|
+
# Description:
|
1120
|
+
# Clicks on button or link or any element that triggers a javascript pop up.
|
1121
|
+
# Used internally by function click_js_popup.
|
1122
|
+
#
|
1123
|
+
#def click_js_popup_creator_button
|
1124
|
+
# #puts @@current_js_object.element_name
|
1125
|
+
# jssh_socket.send("#{@@current_js_object.element_name}\n;", 0)
|
1126
|
+
# temp = read_socket()
|
1127
|
+
# temp =~ /\[object\s(.*)\]/
|
1128
|
+
# if $1
|
1129
|
+
# type = $1
|
1130
|
+
# else
|
1131
|
+
# # This is done because in JSSh if you write element name of anchor type
|
1132
|
+
# # then it displays the link to which it navigates instead of displaying
|
1133
|
+
# # object type. So above regex match will return nil
|
1134
|
+
# type = "HTMLAnchorElement"
|
1135
|
+
# end
|
1136
|
+
# #puts type
|
1137
|
+
# case type
|
1138
|
+
# when "HTMLAnchorElement", "HTMLImageElement"
|
1139
|
+
# jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"MouseEvents\");"
|
1140
|
+
# # Info about initMouseEvent at: http://www.xulplanet.com/references/objref/MouseEvent.html
|
1141
|
+
# jssh_command += "event.initMouseEvent('click',true,true,null,1,0,0,0,0,false,false,false,false,0,null);"
|
1142
|
+
# jssh_command += "#{@@current_js_object.element_name}.dispatchEvent(event);\n"
|
1143
|
+
#
|
1144
|
+
# jssh_socket.send("#{jssh_command}", 0)
|
1145
|
+
# read_socket()
|
1146
|
+
# when "HTMLDivElement", "HTMLSpanElement"
|
1147
|
+
# jssh_socket.send("typeof(#{element_object}.#{event.downcase});\n", 0)
|
1148
|
+
# isDefined = read_socket()
|
1149
|
+
# #puts "is method there : #{isDefined}"
|
1150
|
+
# if(isDefined != "undefined")
|
1151
|
+
# if(element_type == "HTMLSelectElement")
|
1152
|
+
# jssh_command = "var event = #{DOCUMENT_VAR}.createEvent(\"HTMLEvents\");
|
1153
|
+
# event.initEvent(\"click\", true, true);
|
1154
|
+
# #{element_object}.dispatchEvent(event);"
|
1155
|
+
# jssh_command.gsub!(/\n/, "")
|
1156
|
+
# jssh_socket.send("#{jssh_command}\n", 0)
|
1157
|
+
# read_socket()
|
1158
|
+
# else
|
1159
|
+
# jssh_socket.send("#{element_object}.#{event.downcase}();\n", 0)
|
1160
|
+
# read_socket()
|
1161
|
+
# end
|
1162
|
+
# end
|
1163
|
+
# else
|
1164
|
+
# jssh_command = "#{@@current_js_object.element_name}.click();\n";
|
1165
|
+
# jssh_socket.send("#{jssh_command}", 0)
|
1166
|
+
# read_socket()
|
1167
|
+
# end
|
1168
|
+
# @@current_level = 0
|
1169
|
+
# @@current_js_object = nil
|
1170
|
+
#end
|
1171
|
+
#private :click_js_popup_creator_button
|
1172
|
+
|
1173
|
+
#
|
1174
|
+
# Description:
|
1175
|
+
# Gets all the options of the select list element.
|
1176
|
+
#
|
1177
|
+
# Output:
|
1178
|
+
# Array of option elements.
|
1179
|
+
#
|
1180
|
+
def options
|
1181
|
+
jssh_socket.send("#{element_object}.options.length;\n", 0)
|
1182
|
+
length = read_socket().to_i
|
1183
|
+
#puts "options length is : #{length}"
|
1184
|
+
arr_options = Array.new(length)
|
1185
|
+
for i in 0..length - 1
|
1186
|
+
arr_options[i] = "#{element_object}.options[#{i}]"
|
1187
|
+
end
|
1188
|
+
return arr_options
|
1189
|
+
end
|
1190
|
+
private :options
|
1191
|
+
|
1192
|
+
#
|
1193
|
+
# Description:
|
1194
|
+
# Used to get class name for option element. Used internally by Firewatir use ff.select_list(...).option(..).class
|
1195
|
+
#
|
1196
|
+
# Output:
|
1197
|
+
# Class name of option element.
|
1198
|
+
#
|
1199
|
+
def option_class_name
|
1200
|
+
jssh_socket.send("#{element_object}.className;\n", 0)
|
1201
|
+
return read_socket()
|
1202
|
+
end
|
1203
|
+
private :option_class_name
|
1204
|
+
|
1205
|
+
#
|
1206
|
+
# Description:
|
1207
|
+
# Used to get text for option element. Used internally by Firewatir use ff.select_list(...).option(..).text
|
1208
|
+
#
|
1209
|
+
# Output:
|
1210
|
+
# Text of option element.
|
1211
|
+
#
|
1212
|
+
def option_text
|
1213
|
+
jssh_socket.send("#{element_object}.text;\n", 0)
|
1214
|
+
return read_socket()
|
1215
|
+
end
|
1216
|
+
private :option_text
|
1217
|
+
|
1218
|
+
#
|
1219
|
+
# Description:
|
1220
|
+
# Used to get value for option element. Used internally by Firewatir use ff.select_list(...).option(..).value
|
1221
|
+
#
|
1222
|
+
# Output:
|
1223
|
+
# Value of option element.
|
1224
|
+
#
|
1225
|
+
def option_value
|
1226
|
+
jssh_socket.send("#{element_object}.value;\n", 0)
|
1227
|
+
return read_socket()
|
1228
|
+
end
|
1229
|
+
private :option_value
|
1230
|
+
|
1231
|
+
#
|
1232
|
+
# Description:
|
1233
|
+
# Used to check if option is selected or not. Used internally by Firewatir use ff.select_list(...).option(..).selected
|
1234
|
+
#
|
1235
|
+
# Output:
|
1236
|
+
# True if option is selected, false otherwise.
|
1237
|
+
#
|
1238
|
+
def option_selected
|
1239
|
+
jssh_socket.send("#{element_object}.selected;\n", 0)
|
1240
|
+
value = read_socket()
|
1241
|
+
return true if value == "true"
|
1242
|
+
return false if value == "false"
|
1243
|
+
end
|
1244
|
+
private :option_selected
|
1245
|
+
|
1246
|
+
#
|
1247
|
+
# Description:
|
1248
|
+
# Gets all the cells of the row of a table.
|
1249
|
+
#
|
1250
|
+
# Output:
|
1251
|
+
# Array of table cell elements.
|
1252
|
+
#
|
1253
|
+
def get_cells
|
1254
|
+
assert_exists
|
1255
|
+
#puts "element name in cells is : #{element_object}"
|
1256
|
+
if(element_type == "HTMLTableRowElement")
|
1257
|
+
jssh_socket.send("#{element_object}.cells.length;\n", 0)
|
1258
|
+
length = read_socket.to_i
|
1259
|
+
return_array = Array.new(length)
|
1260
|
+
for i in 0..length - 1 do
|
1261
|
+
return_array[i] = "#{element_object}.cells[#{i}]"
|
1262
|
+
end
|
1263
|
+
return return_array
|
1264
|
+
else
|
1265
|
+
puts "The element must be of table row type to execute this function."
|
1266
|
+
return nil
|
1267
|
+
end
|
1268
|
+
end
|
1269
|
+
private :get_cells
|
1270
|
+
|
1271
|
+
#
|
1272
|
+
# Description:
|
1273
|
+
# Sets the value of file in HTMLInput file field control.
|
1274
|
+
#
|
1275
|
+
# Input:
|
1276
|
+
# setPath - location of the file to be uploaded. '|' should not be part of filename.
|
1277
|
+
#
|
1278
|
+
def setFileFieldValue(setPath)
|
1279
|
+
# As ruby converts \\ to \ and while sending name to jssh \ is ignored.
|
1280
|
+
# So replace \ with \\. Now this is even trickier you can't replace \ with \\
|
1281
|
+
# directly like this string.gsub!("\\", "\\\\") this doesn't work.
|
1282
|
+
# Work around is replace '\' with two special character and then replace
|
1283
|
+
# special character with \.
|
1284
|
+
setPath.gsub!("\\", "||")
|
1285
|
+
setPath.gsub!("|", "\\")
|
1286
|
+
|
1287
|
+
#jssh_command = "var textBox = #{DOCUMENT_VAR}.getBoxObjectFor(#{element_object}).firstChild;"
|
1288
|
+
#jssh_command += "textBox.value = \"#{setPath}\";\n";
|
1289
|
+
|
1290
|
+
#puts jssh_command
|
1291
|
+
jssh_socket.send("#{element_object}.value = \"#{setPath}\";\n", 0)
|
1292
|
+
read_socket()
|
1293
|
+
@@current_level = 0
|
1294
|
+
end
|
1295
|
+
private :setFileFieldValue
|
1296
|
+
|
1297
|
+
#
|
1298
|
+
# Description:
|
1299
|
+
# Traps all the function calls for an element that is not defined and fires them again
|
1300
|
+
# as it is to the jssh. This can be used in case the element supports properties or methods
|
1301
|
+
# that are not defined in the corresponding element class or in the base class(Element).
|
1302
|
+
#
|
1303
|
+
# Input:
|
1304
|
+
# methodId - Id of the method that is called.
|
1305
|
+
# *args - arguments sent to the methods.
|
1306
|
+
#
|
1307
|
+
def method_missing(methId, *args)
|
1308
|
+
methodName = methId.id2name
|
1309
|
+
#puts "method name is : #{methodName}"
|
1310
|
+
assert_exists
|
1311
|
+
#assert_enabled
|
1312
|
+
methodName = "colSpan" if methodName == "colspan"
|
1313
|
+
if(methodName =~ /invoke/)
|
1314
|
+
jssh_command = "#{element_object}."
|
1315
|
+
for i in args do
|
1316
|
+
jssh_command += i;
|
1317
|
+
end
|
1318
|
+
#puts "#{jssh_command}"
|
1319
|
+
jssh_socket.send("#{jssh_command};\n", 0)
|
1320
|
+
return_value = read_socket()
|
1321
|
+
#puts "return value is : #{return_value}"
|
1322
|
+
return return_value
|
1323
|
+
else
|
1324
|
+
#assert_exists
|
1325
|
+
#puts "element name is #{element_object}"
|
1326
|
+
|
1327
|
+
# We get method name with trailing '=' when we try to assign a value to a
|
1328
|
+
# property. So just remove the '=' to get the type
|
1329
|
+
temp = ""
|
1330
|
+
assigning_value = false
|
1331
|
+
if(methodName =~ /(.*)=$/)
|
1332
|
+
temp = "#{element_object}.#{$1}"
|
1333
|
+
assigning_value = true
|
1334
|
+
else
|
1335
|
+
temp = "#{element_object}.#{methodName}"
|
1336
|
+
end
|
1337
|
+
#puts "temp is : #{temp}"
|
1338
|
+
|
1339
|
+
jssh_socket.send("typeof(#{temp});\n", 0)
|
1340
|
+
method_type = read_socket()
|
1341
|
+
#puts "method_type is : #{method_type}"
|
1342
|
+
|
1343
|
+
if(assigning_value)
|
1344
|
+
if(method_type != "boolean" && args[0].class != Fixnum)
|
1345
|
+
args[0].gsub!("\\", "\\"*4)
|
1346
|
+
args[0].gsub!("\"", "\\\"")
|
1347
|
+
args[0].gsub!("\n","\\n")
|
1348
|
+
jssh_command = "#{element_object}.#{methodName}\"#{args[0]}\""
|
1349
|
+
else
|
1350
|
+
jssh_command = "#{element_object}.#{methodName}#{args[0]}"
|
1351
|
+
end
|
1352
|
+
#puts "jssh_command is : #{jssh_command}"
|
1353
|
+
jssh_socket.send("#{jssh_command};\n", 0)
|
1354
|
+
read_socket()
|
1355
|
+
return
|
1356
|
+
end
|
1357
|
+
|
1358
|
+
methodName = "#{element_object}.#{methodName}"
|
1359
|
+
if(args.length == 0)
|
1360
|
+
#puts "In if loop #{methodName}"
|
1361
|
+
if(method_type == "function")
|
1362
|
+
jssh_command = "#{methodName}();\n"
|
1363
|
+
else
|
1364
|
+
jssh_command = "#{methodName};\n"
|
1365
|
+
end
|
1366
|
+
else
|
1367
|
+
#puts "In else loop : #{methodName}"
|
1368
|
+
jssh_command = "#{methodName}("
|
1369
|
+
|
1370
|
+
count = 0
|
1371
|
+
if args != nil
|
1372
|
+
for i in args
|
1373
|
+
jssh_command += "," if count != 0
|
1374
|
+
if i.kind_of? Numeric
|
1375
|
+
jssh_command += i.to_s
|
1376
|
+
else
|
1377
|
+
jssh_command += "\"#{i.to_s.gsub(/"/,"\\\"")}\""
|
1378
|
+
end
|
1379
|
+
count = count + 1
|
1380
|
+
end
|
1381
|
+
end
|
1382
|
+
|
1383
|
+
jssh_command += ");\n"
|
1384
|
+
end
|
1385
|
+
|
1386
|
+
if(method_type == "boolean")
|
1387
|
+
jssh_command = jssh_command.gsub("\"false\"", "false")
|
1388
|
+
jssh_command = jssh_command.gsub("\"true\"", "true")
|
1389
|
+
end
|
1390
|
+
#puts "jssh_command is #{jssh_command}"
|
1391
|
+
jssh_socket.send("#{jssh_command}", 0)
|
1392
|
+
returnValue = read_socket()
|
1393
|
+
#puts "return value is : #{returnValue}"
|
1394
|
+
|
1395
|
+
@@current_level = 0
|
1396
|
+
|
1397
|
+
if(method_type == "boolean")
|
1398
|
+
return false if(returnValue == "false")
|
1399
|
+
return true if(returnValue == "true")
|
1400
|
+
elsif(method_type == "number")
|
1401
|
+
return returnValue.to_i
|
1402
|
+
else
|
1403
|
+
return returnValue
|
1404
|
+
end
|
1405
|
+
end
|
1406
|
+
end
|
1407
|
+
end
|
1408
|
+
|
1409
|
+
#
|
1410
|
+
# Description:
|
1411
|
+
# Class for returning the document element.
|
1412
|
+
#
|
1413
|
+
class Document
|
1414
|
+
include Container
|
1415
|
+
@@current_level = 0
|
1416
|
+
|
1417
|
+
#
|
1418
|
+
# Description:
|
1419
|
+
# Creates new instance of Document class.
|
1420
|
+
#
|
1421
|
+
def initialize(container)
|
1422
|
+
@length = 0
|
1423
|
+
@elements = nil
|
1424
|
+
@arr_elements = ""
|
1425
|
+
@container = container
|
1426
|
+
end
|
1427
|
+
|
1428
|
+
#
|
1429
|
+
# Description:
|
1430
|
+
# Find all the elements in the document by querying DOM.
|
1431
|
+
# Set the class variables like length and the variable name of array storing the elements in JSSH.
|
1432
|
+
#
|
1433
|
+
# Output:
|
1434
|
+
# Array of elements.
|
1435
|
+
#
|
1436
|
+
def all
|
1437
|
+
@arr_elements = "arr_coll_#{@@current_level}"
|
1438
|
+
jssh_command = "var arr_coll_#{@@current_level}=new Array(); "
|
1439
|
+
|
1440
|
+
if(@container.class == FireWatir::Firefox || @container.class == Frame)
|
1441
|
+
jssh_command +="var element_collection = null; element_collection = #{DOCUMENT_VAR}.getElementsByTagName(\"*\");
|
1442
|
+
if(element_collection != null && typeof(element_collection) != 'undefined')
|
1443
|
+
{
|
1444
|
+
for (var i = 0; i < element_collection.length; i++)
|
1445
|
+
{
|
1446
|
+
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'))
|
1447
|
+
arr_coll_#{@@current_level}.push(element_collection[i]);
|
1448
|
+
}
|
1449
|
+
}
|
1450
|
+
arr_coll_#{@@current_level}.length;"
|
1451
|
+
else
|
1452
|
+
jssh_command +="var element_collection = null; element_collection = #{@container.element_name}.getElementsByTagName(\"*\");
|
1453
|
+
if(element_collection!= null && typeof(element_collection) != 'undefined')
|
1454
|
+
{
|
1455
|
+
for (var i = 0; i < element_collection.length; i++)
|
1456
|
+
{
|
1457
|
+
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'))
|
1458
|
+
arr_coll_#{@@current_level}.push(element_collection[i]);
|
1459
|
+
}
|
1460
|
+
}
|
1461
|
+
arr_coll_#{@@current_level}.length;"
|
1462
|
+
end
|
1463
|
+
|
1464
|
+
# Remove \n that are there in the string as a result of pressing enter while formatting.
|
1465
|
+
jssh_command.gsub!(/\n/, "")
|
1466
|
+
#puts jssh_command
|
1467
|
+
jssh_socket.send("#{jssh_command};\n", 0)
|
1468
|
+
@length = read_socket().to_i;
|
1469
|
+
#puts "elements length is in locate_tagged_elements is : #{@length}"
|
1470
|
+
|
1471
|
+
elements = nil
|
1472
|
+
elements = Array.new(@length)
|
1473
|
+
for i in 0..@length - 1 do
|
1474
|
+
temp = Element.new("arr_coll_#{@@current_level}[#{i}]", @container)
|
1475
|
+
elements[i] = temp
|
1476
|
+
end
|
1477
|
+
@@current_level += 1
|
1478
|
+
return elements
|
1479
|
+
|
1480
|
+
end
|
1481
|
+
|
1482
|
+
#
|
1483
|
+
# Description:
|
1484
|
+
# Returns the count of elements in the document.
|
1485
|
+
#
|
1486
|
+
# Output:
|
1487
|
+
# Count of elements found in the document.
|
1488
|
+
#
|
1489
|
+
def length
|
1490
|
+
return @length
|
1491
|
+
end
|
1492
|
+
|
1493
|
+
#
|
1494
|
+
# Description:
|
1495
|
+
# Iterates over elements in the document.
|
1496
|
+
#
|
1497
|
+
def each
|
1498
|
+
for i in 0..@length - 1
|
1499
|
+
yield Element.new("#{@arr_elements}[#{i}]", @container)
|
1500
|
+
end
|
1501
|
+
end
|
1502
|
+
|
1503
|
+
#
|
1504
|
+
# Description:
|
1505
|
+
# Gets the element at the nth index in the array of the elements.
|
1506
|
+
#
|
1507
|
+
# Input:
|
1508
|
+
# n - Index of element you want to access. Index is 1 based.
|
1509
|
+
#
|
1510
|
+
# Output:
|
1511
|
+
# Element at the nth index.
|
1512
|
+
#
|
1513
|
+
def [](n)
|
1514
|
+
return Element.new("#{@arr_elements}[#{n-1}]", @container)
|
1515
|
+
end
|
1516
|
+
|
1517
|
+
#
|
1518
|
+
# Description:
|
1519
|
+
# Get all forms available on the page.
|
1520
|
+
# Used internally by Firewatir use ff.show_forms instead.
|
1521
|
+
#
|
1522
|
+
# Output:
|
1523
|
+
# Array containing Form elements
|
1524
|
+
#
|
1525
|
+
def get_forms()
|
1526
|
+
jssh_socket.send("var element_forms = #{DOCUMENT_VAR}.forms; element_forms.length;\n", 0)
|
1527
|
+
length = read_socket().to_i
|
1528
|
+
forms = Array.new(length)
|
1529
|
+
|
1530
|
+
for i in 0..length - 1 do
|
1531
|
+
forms[i] = Form.new(@container, :jssh_name, "element_forms[#{i}]")
|
1532
|
+
end
|
1533
|
+
return forms
|
1534
|
+
end
|
1535
|
+
|
1536
|
+
#
|
1537
|
+
# Description:
|
1538
|
+
# Get all images available on the page.
|
1539
|
+
# Used internally by Firewatir use ff.show_images instead.
|
1540
|
+
#
|
1541
|
+
# Output:
|
1542
|
+
# Array containing Image elements
|
1543
|
+
#
|
1544
|
+
def get_images
|
1545
|
+
return Images.new(@container)
|
1546
|
+
end
|
1547
|
+
|
1548
|
+
#
|
1549
|
+
# Description:
|
1550
|
+
# Get all links available on the page.
|
1551
|
+
# Used internally by Firewatir use ff.show_links instead.
|
1552
|
+
#
|
1553
|
+
# Output:
|
1554
|
+
# Array containing Link elements
|
1555
|
+
#
|
1556
|
+
def get_links
|
1557
|
+
return Links.new(@container)
|
1558
|
+
end
|
1559
|
+
|
1560
|
+
#
|
1561
|
+
# Description:
|
1562
|
+
# Get all divs available on the page.
|
1563
|
+
# Used internally by Firewatir use ff.show_divs instead.
|
1564
|
+
#
|
1565
|
+
# Output:
|
1566
|
+
# Array containing Div elements
|
1567
|
+
#
|
1568
|
+
def get_divs
|
1569
|
+
return Divs.new(@container)
|
1570
|
+
end
|
1571
|
+
|
1572
|
+
#
|
1573
|
+
# Description:
|
1574
|
+
# Get all tables available on the page.
|
1575
|
+
# Used internally by Firewatir use ff.show_tables instead.
|
1576
|
+
#
|
1577
|
+
# Output:
|
1578
|
+
# Array containing Table elements
|
1579
|
+
#
|
1580
|
+
def get_tables
|
1581
|
+
return Tables.new(@container)
|
1582
|
+
end
|
1583
|
+
|
1584
|
+
#
|
1585
|
+
# Description:
|
1586
|
+
# Get all pres available on the page.
|
1587
|
+
# Used internally by Firewatir use ff.show_pres instead.
|
1588
|
+
#
|
1589
|
+
# Output:
|
1590
|
+
# Array containing Pre elements
|
1591
|
+
#
|
1592
|
+
def get_pres
|
1593
|
+
return Pres.new(@container)
|
1594
|
+
end
|
1595
|
+
|
1596
|
+
#
|
1597
|
+
# Description:
|
1598
|
+
# Get all spans available on the page.
|
1599
|
+
# Used internally by Firewatir use ff.show_spans instead.
|
1600
|
+
#
|
1601
|
+
# Output:
|
1602
|
+
# Array containing Span elements
|
1603
|
+
#
|
1604
|
+
def get_spans
|
1605
|
+
return Spans.new(@container)
|
1606
|
+
end
|
1607
|
+
|
1608
|
+
#
|
1609
|
+
# Description:
|
1610
|
+
# Get all labels available on the page.
|
1611
|
+
# Used internally by Firewatir use ff.show_labels instead.
|
1612
|
+
#
|
1613
|
+
# Output:
|
1614
|
+
# Array containing Label elements
|
1615
|
+
#
|
1616
|
+
def get_labels
|
1617
|
+
return Labels.new(@container)
|
1618
|
+
end
|
1619
|
+
end
|
1620
|
+
|
1621
|
+
#
|
1622
|
+
# Description:
|
1623
|
+
# Class for iterating over elements of common type like links, images, divs etc.
|
1624
|
+
#
|
1625
|
+
class ElementCollections
|
1626
|
+
include Container
|
1627
|
+
@@current_level = 0
|
1628
|
+
#
|
1629
|
+
# Description:
|
1630
|
+
# Initializes new instance of this class.
|
1631
|
+
#
|
1632
|
+
# Input:
|
1633
|
+
# tag - tag name of the element for which you want the iterator.
|
1634
|
+
# types - element type. used in case where same element tag has different types like input has type image, button etc.
|
1635
|
+
#
|
1636
|
+
#def initialize(container, tag , types=nil)
|
1637
|
+
# @@current_level += 1
|
1638
|
+
# @@current_element_object = Element.get_current_element
|
1639
|
+
# #puts " the current object #{@@current_element_object}"
|
1640
|
+
# @length = 0
|
1641
|
+
# @arr_name = ""
|
1642
|
+
# @container = container
|
1643
|
+
# @elements = locate_tagged_elements(tag , types)
|
1644
|
+
#end
|
1645
|
+
|
1646
|
+
#
|
1647
|
+
# Description:
|
1648
|
+
# Locate all the elements of give tag and type.
|
1649
|
+
#
|
1650
|
+
# Input:
|
1651
|
+
# tag - tag name of the element for which you want the iterator.
|
1652
|
+
# types - element type. used in case where same element tag has different types like input has type image, button etc.
|
1653
|
+
#
|
1654
|
+
# Output:
|
1655
|
+
# Elements array containing all the elements found on the page.
|
1656
|
+
#
|
1657
|
+
def locate_tagged_elements(tag , types = nil)
|
1658
|
+
jssh_command = "var arr_coll_#{tag}_#{@@current_level}=new Array();"
|
1659
|
+
@arr_name = "arr_coll_#{tag}_#{@@current_level}"
|
1660
|
+
|
1661
|
+
if(@container.class == FireWatir::Firefox || @container.class == Frame)
|
1662
|
+
jssh_command += "var elements_#{tag} = null; elements_#{tag} = #{DOCUMENT_VAR}.getElementsByTagName(\"#{tag}\");"
|
1663
|
+
if(types != nil and (types.include?("textarea") or types.include?("button")) )
|
1664
|
+
jssh_command += "elements_#{tag} = #{DOCUMENT_VAR}.body.getElementsByTagName(\"*\");"
|
1665
|
+
end
|
1666
|
+
else
|
1667
|
+
jssh_command += "var elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"#{tag}\");"
|
1668
|
+
if(types != nil and (types.include?("textarea") or types.include?("button")) )
|
1669
|
+
jssh_command += "elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"*\");"
|
1670
|
+
end
|
1671
|
+
end
|
1672
|
+
|
1673
|
+
if(types != nil)
|
1674
|
+
jssh_command += "var types = new Array("
|
1675
|
+
count = 0
|
1676
|
+
types.each do |type|
|
1677
|
+
if count == 0
|
1678
|
+
jssh_command += "\"#{type}\""
|
1679
|
+
count += 1
|
1680
|
+
else
|
1681
|
+
jssh_command += ",\"#{type}\""
|
1682
|
+
end
|
1683
|
+
end
|
1684
|
+
jssh_command += ");"
|
1685
|
+
else
|
1686
|
+
jssh_command += "var types = null;"
|
1687
|
+
end
|
1688
|
+
|
1689
|
+
if(@container.class == FireWatir::Firefox || @container.class == Frame)
|
1690
|
+
|
1691
|
+
jssh_command += "for(var i=0; i<elements_#{tag}.length; i++)
|
1692
|
+
{
|
1693
|
+
|
1694
|
+
var element = elements_#{tag}[i];"
|
1695
|
+
else
|
1696
|
+
jssh_command += "for(var i=0; i<elements_#{@@current_level}_#{tag}.length; i++)
|
1697
|
+
{
|
1698
|
+
|
1699
|
+
var element = elements_#{@@current_level}_#{tag}[i];"
|
1700
|
+
end
|
1701
|
+
|
1702
|
+
jssh_command += "
|
1703
|
+
var same_type = false;
|
1704
|
+
if(types)
|
1705
|
+
{
|
1706
|
+
for(var j=0; j<types.length; j++)
|
1707
|
+
{
|
1708
|
+
if(types[j] == element.type || types[j] == element.tagName)
|
1709
|
+
{
|
1710
|
+
same_type = true;
|
1711
|
+
break;
|
1712
|
+
}
|
1713
|
+
}
|
1714
|
+
}
|
1715
|
+
else
|
1716
|
+
{
|
1717
|
+
same_type = true;
|
1718
|
+
}
|
1719
|
+
if(same_type == true)
|
1720
|
+
{
|
1721
|
+
arr_coll_#{tag}_#{@@current_level}.push(element);
|
1722
|
+
}
|
1723
|
+
}
|
1724
|
+
arr_coll_#{tag}_#{@@current_level}.length;"
|
1725
|
+
|
1726
|
+
# Remove \n that are there in the string as a result of pressing enter while formatting.
|
1727
|
+
jssh_command.gsub!(/\n/, "")
|
1728
|
+
#puts jssh_command
|
1729
|
+
jssh_socket.send("#{jssh_command};\n", 0)
|
1730
|
+
length = read_socket().to_i;
|
1731
|
+
#puts "elements length is in locate_tagged_elements is : #{length}"
|
1732
|
+
|
1733
|
+
elements = Array.new(length)
|
1734
|
+
for i in 0..length - 1 do
|
1735
|
+
elements[i] = "arr_coll_#{tag}_#{@@current_level}[#{i}]"
|
1736
|
+
end
|
1737
|
+
@@current_level = @@current_level + 1
|
1738
|
+
return elements
|
1739
|
+
end
|
1740
|
+
private:locate_tagged_elements
|
1741
|
+
|
1742
|
+
#
|
1743
|
+
# Description:
|
1744
|
+
# Gets the length of elements of same tag and type found on the page.
|
1745
|
+
#
|
1746
|
+
# Ouput:
|
1747
|
+
# Count of elements found on the page.
|
1748
|
+
#
|
1749
|
+
def length
|
1750
|
+
#puts @element_objects.length
|
1751
|
+
return @element_objects.length
|
1752
|
+
end
|
1753
|
+
|
1754
|
+
#
|
1755
|
+
# Description:
|
1756
|
+
# Iterate over the elements of same tag and type found on the page.
|
1757
|
+
#
|
1758
|
+
def each
|
1759
|
+
for i in 0..@element_objects.length - 1
|
1760
|
+
yield @element_objects[i]
|
1761
|
+
end
|
1762
|
+
end
|
1763
|
+
|
1764
|
+
#
|
1765
|
+
# Description:
|
1766
|
+
# Accesses nth element of same tag and type found on the page.
|
1767
|
+
#
|
1768
|
+
# Input:
|
1769
|
+
# n - index of element (1 based)
|
1770
|
+
#
|
1771
|
+
def [](n)
|
1772
|
+
return @element_objects[n-1]
|
1773
|
+
end
|
1774
|
+
|
1775
|
+
end
|