vendor/gems/gems/firewatir-1.2.1/MozillaBaseElement.rb
#
# Description:
#   Locates the element on the page depending upon the parameters passed. Logic for locating the element is written
#   in JavaScript and then send to JSSh; so that we don't make small round-trips via socket to JSSh. This is done to
#   improve the performance for locating the element.
#
# Input:
#   tag - Tag name of the element to be located like "input", "a" etc. This is case insensitive.
#   how - The attribute by which you want to locate the element like id, name etc. You can use any attribute-value pair
#         that uniquely identifies that element on the page. If there are more that one element that have identical
#         attribute-value pair then first element that is found while traversing the DOM will be returned.
#   what - The value of the attribute specified by how.
#   types - Used if that HTML element to be located has different type like input can be of type image, button etc.
#           Default value is nil
#   value - This is used only in case of radio buttons where they have same name but different value.
#
# Output:
#   Returns nil if unable to locate the element, else return the element.
#

Locates the element on the page depending upon the parameters passed. Logic for locating the element is written
in JavaScript and then send to JSSh; so that we don't make small round-trips via socket to JSSh. This is done to
improve the performance for locating the element.

Tag name of the element to be located like "input", "a" etc. This is case insensitive.
how - The attribute by which you want to locate the element like id, name etc. You can use any attribute-value pair
that uniquely identifies that element on the page. If there are more that one element that have identical
attribute-value pair then first element that is found while traversing the DOM will be returned.
what - The value of the attribute specified by how.
types - Used if that HTML element to be located has different type like input can be of type image, button etc.
Default value is nil
value - This is used only in case of radio buttons where they have same name but different value.

Returns nil if unable to locate the element, else return the element.
Locates the element on the page depending upon the parameters passed . Logic for locating the element is written in JavaScript and then send to JSSh ; so that we don ' t make small round - trips via socket to JSSh . This is done to improve the performance for locating the element . Tag name of the element to be located like " input " " a " etc . This is case insensitive . how - The attribute by which you want to locate the element like id name etc . You can use any attribute - value pair that uniquely identifies that element on the page . If there are more that one element that have identical attribute - value pair then first element that is found while traversing the DOM will be returned . what - The value of the attribute specified by how . types - Used if that HTML element to be located has different type like input can be of type image button etc . Default value is nil value - This is used only in case of radio buttons where they have same name but different value . Returns nil if unable to locate the element else return the element .
def locate_tagged_element(tag, how, what, types = nil, value = nil)
            how = :value if how == :caption
            how = :href if how == :url
            if(types != nil and types.include?("button"))
                jssh_command = "var isButtonElement = true;"
            else
                jssh_command = "var isButtonElement = false;"
            end
            if(@container.class == FireWatir::Firefox || @container.class == Frame)
                jssh_command += "var elements_#{tag} = null; elements_#{tag} = #{DOCUMENT_VAR}.getElementsByTagName(\"#{tag}\");"
                if(types != nil and (types.include?("textarea") or types.include?("button")) )
                    jssh_command += "elements_#{tag} = #{DOCUMENT_VAR}.body.getElementsByTagName(\"*\");"
                end
            else
                jssh_command += "var elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"#{tag}\");"
                if(types != nil and (types.include?("textarea") or types.include?("button") ) )
                    jssh_command += "elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"*\");"
                end
            end
            if(types != nil)
                jssh_command += "var types = new Array("
                count = 0
                types.each do |type|
                    if count == 0
                        jssh_command += "\"#{type}\""
                        count += 1
                    else
                        jssh_command += ",\"#{type}\""
                    end
                end
                jssh_command += ");"
            else
                jssh_command += "var types = null;"
            end
            jssh_command += "var object_index = 1; var o = null; var element_name = '';"
            if(value == nil)
                jssh_command += "var value = null;"
            else
                jssh_command += "var value = \"#{value}\";"
            end
            if(@container.class == FireWatir::Firefox || @container.class == Frame)
                jssh_command += "for(var i=0; i<elements_#{tag}.length; i++)
                                 {
                                    if(element_name != \"\") break;
                                    var element = elements_#{tag}[i];"
            else
                jssh_command += "for(var i=0; i<elements_#{@@current_level}_#{tag}.length; i++)
                                 {
                                    if(element_name != \"\") break;
                                    var element = elements_#{@@current_level}_#{tag}[i];"
            end
            jssh_command += "   var attribute = '';
                                var same_type = false;
                                if(types)
                                {
                                    for(var j=0; j<types.length; j++)
                                    {
                                        if(types[j] == element.type || types[j] == element.tagName)
                                        {
                                            same_type = true;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    same_type = true;
                                }
                                if(same_type == true)
                                {
                                    if(\"index\" == \"#{how}\")
                                    {
                                        attribute = object_index; object_index += 1;
                                    }
                                    else
                                    {
                                        if(\"text\" == \"#{how}\")
                                        {
                                            attribute = element.textContent;
                                        }
                                        else
                                        {
                                            if(\"#{how}\" == \"href\" || \"#{how}\" == \"src\" || \"#{how}\" == \"action\" || \"#{how}\" == \"name\")
                                            {
                                                attribute = element.getAttribute(\"#{how}\");
                                            }
                                            else
                                            {
                                                if(element.#{how} != undefined)
                                                    attribute = element.#{how};
                                                else
                                                    attribute = element.getAttribute(\"#{how}\");
                                            }
                                        }
                                        if(\"value\" == \"#{how}\" && isButtonElement && (attribute == null || attribute == ''))
                                        {
                                            attribute = element.innerHTML;
                                        }
                                    }
                                    if(attribute == \"\") o = 'NoMethodError';
                                    var found = false;"
            if(what.class == Regexp)
                oldRegExp = what.to_s
                newRegExp = what.inspect
                flags = oldRegExp.slice(2, oldRegExp.index(':') - 2)
                for i in 0..flags.length do
                    flag = flags[i, 1]
                    if(flag == '-')
                        break;
                    else
                        newRegExp << flag
                    end
                end
                jssh_command += "   var regExp = new RegExp(#{newRegExp});
                                    found = regExp.test(attribute);"
            elsif(how == :index)
                jssh_command += "   found = (attribute == #{what});"
            else
                jssh_command += "   found = (attribute == \"#{what}\");"
            end
            if(@container.class == FireWatir::Firefox || @container.class == Frame)
                jssh_command += "   if(found)
                                    {
                                        if(value)
                                        {
                                            if(element.value == \"#{value}\")
                                            {
                                                o = element;
                                                element_name = \"elements_#{tag}[\" + i + \"]\";
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            o = element;
                                            element_name = \"elements_#{tag}[\" + i + \"]\";
                                            break;
                                        }
                                    }"
            else
                jssh_command += "   if(found)
                                    {
                                        if(value)
                                        {
                                            if(element.value == \"#{value}\")
                                            {
                                                o = element;
                                                element_name = \"elements_#{@@current_level}_#{tag}[\" + i + \"]\";
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            o = element;
                                            element_name = \"elements_#{@@current_level}_#{tag}[\" + i + \"]\";
                                            break;
                                        }
                                    }"
            end
            jssh_command +="     }
                             }
                            element_name;"
            jssh_command.gsub!(/\n/, "")
            jssh_socket.send("#{jssh_command};\n", 0)
            element_name = read_socket();
            @@current_level = @@current_level + 1
            if(element_name != "")
                return element_name 
            else
                return nil
            end
        end
def locate_tagged_element ( tag , how , what , types = nil , value = nil ) how = :value if how == :caption how = :href if how == :url if ( types != nil and types . include? ( "button" ) ) jssh_command = "var isButtonElement = true;" else jssh_command = "var isButtonElement = false;" end if ( @container . class == FireWatir :: Firefox || @container . class == Frame ) jssh_command += "var elements_#{tag} = null; elements_#{tag} = #{DOCUMENT_VAR}.getElementsByTagName(\"#{tag}\");" if ( types != nil and ( types . include? ( "textarea" ) or types . include? ( "button" ) ) ) jssh_command += "elements_#{tag} = #{DOCUMENT_VAR}.body.getElementsByTagName(\"*\");" end else jssh_command += "var elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"#{tag}\");" if ( types != nil and ( types . include? ( "textarea" ) or types . include? ( "button" ) ) ) jssh_command += "elements_#{@@current_level}_#{tag} = #{@container.element_name}.getElementsByTagName(\"*\");" end end if ( types != nil ) jssh_command += "var types = new Array(" count = 0 types . each do | type | if count == 0 jssh_command += "\"#{type}\"" count += 1 else jssh_command += ",\"#{type}\"" end end jssh_command += ");" else jssh_command += "var types = null;" end jssh_command += "var object_index = 1; var o = null; var element_name = '';" if ( value == nil ) jssh_command += "var value = null;" else jssh_command += "var value = \"#{value}\";" end if ( @container . class == FireWatir :: Firefox || @container . class == Frame ) jssh_command += "for(var i=0; i<elements_#{tag}.length; i++)
                                 {
                                    if(element_name != \"\") break;
                                    var element = elements_#{tag}[i];" else jssh_command += "for(var i=0; i<elements_#{@@current_level}_#{tag}.length; i++)
                                 {
                                    if(element_name != \"\") break;
                                    var element = elements_#{@@current_level}_#{tag}[i];" end jssh_command += "   var attribute = '';
                                var same_type = false;
                                if(types)
                                {
                                    for(var j=0; j<types.length; j++)
                                    {
                                        if(types[j] == element.type || types[j] == element.tagName)
                                        {
                                            same_type = true;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    same_type = true;
                                }
                                if(same_type == true)
                                {
                                    if(\"index\" == \"#{how}\")
                                    {
                                        attribute = object_index; object_index += 1;
                                    }
                                    else
                                    {
                                        if(\"text\" == \"#{how}\")
                                        {
                                            attribute = element.textContent;
                                        }
                                        else
                                        {
                                            if(\"#{how}\" == \"href\" || \"#{how}\" == \"src\" || \"#{how}\" == \"action\" || \"#{how}\" == \"name\")
                                            {
                                                attribute = element.getAttribute(\"#{how}\");
                                            }
                                            else
                                            {
                                                if(element.#{how} != undefined)
                                                    attribute = element.#{how};
                                                else
                                                    attribute = element.getAttribute(\"#{how}\");
                                            }
                                        }
                                        if(\"value\" == \"#{how}\" && isButtonElement && (attribute == null || attribute == ''))
                                        {
                                            attribute = element.innerHTML;
                                        }
                                    }
                                    if(attribute == \"\") o = 'NoMethodError';
                                    var found = false;" if ( what . class == Regexp ) oldRegExp = what . to_s newRegExp = what . inspect flags = oldRegExp . slice ( 2 , oldRegExp . index ( ':' ) - 2 ) for i in 0 .. flags . length do flag = flags [ i , 1 ] if ( flag == '-' ) break ; else newRegExp << flag end end jssh_command += "   var regExp = new RegExp(#{newRegExp});
                                    found = regExp.test(attribute);" elsif ( how == :index ) jssh_command += "   found = (attribute == #{what});" else jssh_command += "   found = (attribute == \"#{what}\");" end if ( @container . class == FireWatir :: Firefox || @container . class == Frame ) jssh_command += "   if(found)
                                    {
                                        if(value)
                                        {
                                            if(element.value == \"#{value}\")
                                            {
                                                o = element;
                                                element_name = \"elements_#{tag}[\" + i + \"]\";
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            o = element;
                                            element_name = \"elements_#{tag}[\" + i + \"]\";
                                            break;
                                        }
                                    }" else jssh_command += "   if(found)
                                    {
                                        if(value)
                                        {
                                            if(element.value == \"#{value}\")
                                            {
                                                o = element;
                                                element_name = \"elements_#{@@current_level}_#{tag}[\" + i + \"]\";
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            o = element;
                                            element_name = \"elements_#{@@current_level}_#{tag}[\" + i + \"]\";
                                            break;
                                        }
                                    }" end jssh_command += "     }
                             }
                            element_name;" jssh_command . gsub! ( / \n / , "" ) jssh_socket . send ( "#{jssh_command};\n" , 0 ) element_name = read_socket ( ) ; @@current_level = @@current_level + 1 if ( element_name != "" ) return element_name else return nil end end
Description:
Locates the element on the page depending upon the parameters passed.
Description : Locates the element on the page depending upon the parameters passed .
#puts caller(0)
 #puts "current element is : #{@container.class} and tag is #{tag}"
 # If there is no current element i.e. element in current context we are searching the whole DOM tree.
 # So get all the elements.
 # Because in both the below cases we need to get element with respect to document.
 # when we locate a frame document is automatically adjusted to point to HTML inside the frame
 #end
 #if(@@current_element_object == "")
 #    @@has_changed = true
 #puts "container name is: " + @container.element_name
 #locate if defined? locate
 #@container.locate
 #    @@has_changed = false
 #jssh_command += "var elements = #{element_object}.getElementsByTagName('*');"
 #jssh_command += "elements.length;"
 # Because in IE for button the value of "value" attribute also corresponds to the innerHTML if value attribute
 # is not supplied. For e.g.: <button>Sign In</button>, in this case value of "value" attribute is "Sign In"
 # though value attribute is not supplied. But for Firefox value of "value" attribute is null. So to make sure
 # script runs on both IE and Watir we are also considering innerHTML if element is of button type.
 # Construct the regular expression because we can't use it directly by converting it to string.
 # If reg ex is /Google/i then its string conversion will be (?i-mx:Google) so we can't use it.
 # Construct the regular expression again from the string conversion.
 #puts "old reg ex is #{what} new reg ex is #{newRegExp}"
 #jssh_command += "    found;"
 # Remove \n that are there in the string as a result of pressing enter while formatting.
 #puts jssh_command
 #puts "element name in find control is : #{element_name}"
 #puts @container
 #puts element_name
 #Element.new(element_name, @container)

tag
<not_specific>
how
<not_specific>
what
<not_specific>
types
<not_specific>
value
<not_specific>